Problem with late static binding and get_called_class() usage
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
This bug report is missing a link to reproduction on phpstan.org. It will most likely be closed after manual review.
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...
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.
@zhiyuan-lin Please open a separate issue for this. Thanks.
@ondrejmirtes I have created a issue here. https://github.com/phpstan/phpstan/issues/5252
@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 |
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
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.
@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
@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
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.