phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

Problem with late static binding and get_called_class() usage

Open Proxxx23 opened this issue 6 years ago • 11 comments

Bug report

Hello,

I've ran into this problem while analysing code with latest phpstan: Strict comparison using === between 'Customer\\Article\\SingleArticle' and 'Customer\\Article\\AbstractArticle' will always evaluate to false..

The case is, I use get_called_class() method, which works completly fine within a code.

Code snippet that reproduces the problem

I have abstract class called AbstractArticle with methods createFromOcArticle and isWithinSingleArticle().

I have SingleArticle class which extends AbstractArticle.

I call SingleArticle::createFromOcArticle()which calls AbstractArticle::isWithinSingleArticle().

This is how isWithinSingleArticle() looks like:

    private static function isWithinSingleArticle(): bool
    {
        return SingleArticle::class === get_called_class();
    }

I think it should know, from what context I call get_called_class().

CODE SNIPPET: https://phpstan.org/r/59bdd0e3-3491-4ee8-8314-d99b7e1dc04e

Proxxx23 avatar Nov 09 '20 14:11 Proxxx23

This bug report is missing a link to reproduction on phpstan.org. It will most likely be closed after manual review.

mergeable avatar Nov 09 '20 14:11 mergeable

Need to investigate: difference between get_class() and get_called_class() and fix the respective extensions:

  • https://github.com/phpstan/phpstan-src/blob/master/src/Type/Php/GetClassDynamicReturnTypeExtension.php
  • https://github.com/phpstan/phpstan-src/blob/master/src/Type/Php/GetCalledClassDynamicReturnTypeExtension.php

It might be possible to reuse logic from MutatingScope::getType() where $obj::class is handled...

ondrejmirtes avatar Nov 10 '20 09:11 ondrejmirtes

I have met a similar issue runing phpstan 0.12.90 on this project https://github.com/line/line-bot-sdk-php/blob/7.0.0/src/LINEBot/Narrowcast/Recipient/RecipientBuilder.php

abstract class RecipientBuilder
{
    public static function builder()
    {
        $class = \get_called_class();
        return new $class();
    }
}

The error message was:

Instantiated class LINE\LINEBot\Narrowcast\Recipient\RecipientBuilder is abstract.

However, this static method actually instantiated the class returned by get_called_class() instead of the abstract class itself.

itsuki-hayashi avatar Jul 02 '21 10:07 itsuki-hayashi

@zhiyuan-lin Please open a separate issue for this. Thanks.

ondrejmirtes avatar Jul 02 '21 12:07 ondrejmirtes

@ondrejmirtes I have created a issue here. https://github.com/phpstan/phpstan/issues/5252

itsuki-hayashi avatar Jul 02 '21 12:07 itsuki-hayashi

@Proxxx23 After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-12: Strict comparison using === between 'SingleArticle' and 'AbstractArticle' will always evaluate to false.
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

phpstan-bot avatar Sep 23 '21 08:09 phpstan-bot

Hi, I am facing the same or a very similar issue. Here is my example code, if it helps: https://phpstan.org/r/63388394-fdd2-48d3-badc-1108d372fca6

hbrecht avatar Nov 22 '21 12:11 hbrecht

I guess this is related:

Code
<?php

/**
 * @template T of Entity
 */
class Query
{
    /** @var class-string<T> */
    public $entity;

    /**
     * @param class-string<T> $entity
     */
    public function __construct($entity)
    {
        $this->entity = $entity;
    }
}

class Entity
{
    /**
     * @return Query<static>
     */
    public function query()
    {
        return new Query(get_called_class()); // ! ERROR: Method Entity::query() should return Query<static(Entity)> but returns Query<Entity>.
    }
}

Notice that if I put static::class the error goes away.

HighLiuk avatar May 10 '23 15:05 HighLiuk

@Proxxx23 After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-12: Strict comparison using === between 'SingleArticle' and 'AbstractArticle' will always evaluate to false.
+No errors

phpstan-bot avatar May 23 '23 12:05 phpstan-bot

@hbrecht After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-8: Strict comparison using !== between 'SuperClass' and 'SuperClass' will always evaluate to false.
+No errors

phpstan-bot avatar May 23 '23 12:05 phpstan-bot

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jun 24 '23 00:06 github-actions[bot]