-
-
Notifications
You must be signed in to change notification settings - Fork 897
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
Prevent inheritance of class template types in static methods #2738
Comments
So I've stumbled upon this interesting example: https://phpstan.org/r/81f2efd0-b769-4685-a5aa-a8f914fda95a And it seems to work in Psalm too, although it's about class-level |
Yeah, I decided against this in the end (hence why the example now works in Psalm) because penalising a legitimate usage was unfair |
@muglug So what's the difference that this one still marks |
Maybe a better option is to allow this, but report static usages where the type can't be inferred (e.g. Class::thing() can't be inferred but $someObject::thing() should be able to). |
I have just run into this problem. Let's assume code like this (a simplified version of this /**
* @template T
*/
final class Maybe
{
private function __construct(
/** @var T */
public mixed $value = null,
) {
}
/**
* @param T $value
* @return Maybe<T>
*/
public static function just(mixed $value): self
{
return new self($value);
}
} And example use: $ms = Maybe::just('abc');
$mi = Maybe::just(123); The code of Because PHPStan didn't complain about "unknown type T" (or whatever the error message should be), I assumed that the code is correct and that the inferred type of To my surprise, I later found out that for PHPStan the return type of This is rather dangerous behaviour because PHPStan doesn't complain about anything and gives a false feeling that everything works correctly. |
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
Psalm does not support class @template in static methods. And in PHPStan it does not protect. See - vimeo/psalm#2697 - phpstan/phpstan#2738
@ondrejmirtes After the latest push in 2.0.x, PHPStan now reports different result with your code snippet: @@ @@
+28: Expression "$car === 'aaa'" on a separate line does not do anything.
28: Strict comparison using === between Car and 'aaa' will always evaluate to false. Full report
|
Instance template parameters should not be made available to static methods – this was a flaw in my implementation, now fixed. If/when generics are available at the language level this can be re-evaluated.
https://phpstan.org/r/e00cd9e1-3aba-4e6c-ab9a-8e63da122b94
Expected: some sort of error (in Psalm for now it just treats it as a missing docblock class, you may want something more helpful).
The text was updated successfully, but these errors were encountered: