New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
/** @return static */
doesn't work properly with extended abstract class
#4396
Comments
If you run this example locally you get: And the linked blogpost explains why you see this error and what you should do: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static |
Agree. Please, take a look if that is the bug or we should always define <?php
declare(strict_types=1);
interface TheInterface
{
/** @return static */
public static function fromString(string $value);
}
abstract class ClassC implements TheInterface
{
private string $value;
final private function __construct(string $value)
{
$this->value = $value;
}
// HERE: not defined "@return static" annotation leads to an error.
final public static function fromString(string $value): self
{
return new static($value);
}
}
final class ClassB extends ClassC
{
}
final class ClassA
{
public function classB(): ClassB
{
return ClassB::fromString("any");
}
} |
Yeah, the implicit PHPDoc inheritance should probably work in this case. Until it's fixed, please use the explicit |
I belive this should be closed - when |
@melya After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-No errors
+7: Property ClassB::$value is never read, only written. Full report
|
@melya After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
+ 7: Property ClassC::$value is never read, only written.
17: Unsafe usage of new static(). Full report
|
@melya After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
+13: Property ClassC::$value is never read, only written.
34: Method ClassA::classB() should return ClassB but returns ClassC. Full report
|
@melya After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-No errors
+7: Property ClassB::$value is never read, only written. Full report
|
@melya After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
+ 7: Property ClassC::$value is never read, only written.
17: Unsafe usage of new static(). Full report
|
@melya After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
+13: Property ClassC::$value is never read, only written.
34: Method ClassA::classB() should return ClassB but returns ClassC. Full report
|
@melya After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-17: Unsafe usage of new static().
+17: Unsafe usage of new static().
+ 7: Property ClassC::$value is never read, only written. Full report
|
@melya After the latest commit in 1.8.x, PHPStan now reports different result with your code snippet: @@ @@
+PHP 8.0 (3 errors)
+==========
+
+13: Property ClassC::$value is never read, only written.
+15: Private method ClassC::__construct() cannot be final as it is never overridden by other classes.
+34: Method ClassA::classB() should return ClassB but returns ClassC.
+
+PHP 7.1 – 7.4 (2 errors)
+==========
+
+13: Property ClassC::$value is never read, only written.
34: Method ClassA::classB() should return ClassB but returns ClassC. Full reportPHP 8.0 (3 errors)
PHP 7.1 – 7.4 (2 errors)
|
@ondrejmirtes I'm not sure if it's related or should I create another issue, but <?php declare(strict_types = 1);
class HelloWorld
{
/** @return static */
public function sayHello(): self
{
return new self();
}
} https://phpstan.org/r/a6e4516b-4a9b-4d60-83d5-6392aad8f8cd Here without https://phpstan.org/r/c3800156-3cbd-4817-83db-4a9b1dcd3d01 But here with |
@luxemate Please do not hijack unrelated bug reports with your own issue. Open a discussion instead, thanks. |
@ondrejmirtes No problem. I just thought it is related because it pretty much looks so. :) |
Fixed: phpstan/phpstan-src#2803 |
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. |
Bug report
PHP Version: 7.4
Somehow
/** @return static */
doesn't work properly in provided code snippet with extra abstract class.However it works in simpler use-case like https://phpstan.org/r/18000821-8803-43f6-b17b-abd1d49c6cce
Code snippet that reproduces the problem
https://phpstan.org/r/771ab3d4-54fb-4552-8c4e-9ac0633e457c
Expected output
ClassB::fromString("any") should not generate an error "Unsafe usage of new static()."
The text was updated successfully, but these errors were encountered: