diff --git a/composer.json b/composer.json index 43a09c3..3bac617 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": "^7.4 || ^8.0", - "phpstan/phpstan": "^2.0" + "phpstan/phpstan": "^2.1.8" }, "require-dev": { "editorconfig-checker/editorconfig-checker": "^10.6.0", diff --git a/composer.lock b/composer.lock index 15dce44..baa34cd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7b537b903115752fb6a6d5bc4907f0bd", + "content-hash": "2002bdec715c06ab19202f9e94f04849", "packages": [ { "name": "phpstan/phpstan", - "version": "2.1.6", + "version": "2.1.8", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "6eaec7c6c9e90dcfe46ad1e1ffa5171e2dab641c" + "reference": "f9adff3b87c03b12cc7e46a30a524648e497758f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/6eaec7c6c9e90dcfe46ad1e1ffa5171e2dab641c", - "reference": "6eaec7c6c9e90dcfe46ad1e1ffa5171e2dab641c", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/f9adff3b87c03b12cc7e46a30a524648e497758f", + "reference": "f9adff3b87c03b12cc7e46a30a524648e497758f", "shasum": "" }, "require": { @@ -62,7 +62,7 @@ "type": "github" } ], - "time": "2025-02-19T15:46:42+00:00" + "time": "2025-03-09T09:30:48+00:00" } ], "packages-dev": [ diff --git a/src/Rule/ClassSuffixNamingRule.php b/src/Rule/ClassSuffixNamingRule.php index 82d09bc..94730d0 100644 --- a/src/Rule/ClassSuffixNamingRule.php +++ b/src/Rule/ClassSuffixNamingRule.php @@ -19,6 +19,8 @@ class ClassSuffixNamingRule implements Rule { + private ReflectionProvider $reflectionProvider; + /** * @var array */ @@ -35,6 +37,7 @@ public function __construct(ReflectionProvider $reflectionProvider, array $super } } + $this->reflectionProvider = $reflectionProvider; $this->superclassToSuffixMapping = $superclassToSuffixMapping; } @@ -63,7 +66,9 @@ public function processNode( } foreach ($this->superclassToSuffixMapping as $superClass => $suffix) { - if (!$classReflection->isSubclassOf($superClass)) { + $superClassReflection = $this->reflectionProvider->getClass($superClass); + + if (!$classReflection->isSubclassOfClass($superClassReflection)) { continue; } diff --git a/src/Rule/ForbidUnusedExceptionRule.php b/src/Rule/ForbidUnusedExceptionRule.php index e6d6a90..9e8d5e2 100644 --- a/src/Rule/ForbidUnusedExceptionRule.php +++ b/src/Rule/ForbidUnusedExceptionRule.php @@ -95,7 +95,7 @@ private function isException(Expr $node, Scope $scope): bool $type = $scope->getType($node); foreach ($type->getObjectClassReflections() as $classReflection) { - if ($classReflection->isSubclassOf(Throwable::class)) { + if ($classReflection->is(Throwable::class)) { return true; } } diff --git a/src/Rule/ForbidVariableTypeOverwritingRule.php b/src/Rule/ForbidVariableTypeOverwritingRule.php index acacbb9..fcaace3 100644 --- a/src/Rule/ForbidVariableTypeOverwritingRule.php +++ b/src/Rule/ForbidVariableTypeOverwritingRule.php @@ -112,7 +112,7 @@ private function removeNullAccessoryAndSubtractedTypes(Type $type): Type $newInnerTypes = []; foreach ($type->getTypes() as $innerType) { - if ($innerType instanceof AccessoryType) { // @phpstan-ignore phpstanApi.instanceofType + if ($innerType instanceof AccessoryType) { continue; } diff --git a/tests/Rule/data/ForbidUnusedMatchResultRule/code.php b/tests/Rule/data/ForbidUnusedMatchResultRule/code.php index 1865219..f96c8ce 100644 --- a/tests/Rule/data/ForbidUnusedMatchResultRule/code.php +++ b/tests/Rule/data/ForbidUnusedMatchResultRule/code.php @@ -88,7 +88,7 @@ public function testUnused(object $class, bool $bool, int $int): void true => 1, }; - match ($int) { // error: Unused match result detected, possible returns: Exception + match ($int) { // error: Unused match result detected, possible returns: Exception|LogicException|RuntimeException 0 => new LogicException(), 1 => new RuntimeException(), default => new Exception(), diff --git a/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php b/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php index 9131f2b..3a5bb93 100644 --- a/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php +++ b/tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php @@ -2,6 +2,17 @@ namespace ForbidVariableTypeOverwritingRule; +/** + * Make PHPStan lose info about possible descendants (new Foo) + * + * @template T + * @param T + * @return T + */ +function passThru($iterable) { + return $iterable; +} + interface SomeInterface { } @@ -39,14 +50,14 @@ function testGeneralizationAndNarrowing( ChildClass1 $childClass1, ChildClass2 $childClass2, ) { - $childClass1 = new ParentClass(); + $childClass1 = passThru(new ParentClass()); $parentClass = new ChildClass2(); $childClass2 = new ChildClass1(); // error: Overwriting variable $childClass2 while changing its type from ForbidVariableTypeOverwritingRule\ChildClass2 to ForbidVariableTypeOverwritingRule\ChildClass1 $object = new ParentClass(); $intOrString1 = 1; $intOrString2 = []; // error: Overwriting variable $intOrString2 while changing its type from int|string to array{} - $classWithInterface1 = new ParentClass(); + $classWithInterface1 = passThru(new ParentClass()); $classWithInterface2 = new AnotherClassWithInterface(); // error: Overwriting variable $classWithInterface2 while changing its type from ForbidVariableTypeOverwritingRule\ParentClass&ForbidVariableTypeOverwritingRule\SomeInterface to ForbidVariableTypeOverwritingRule\AnotherClassWithInterface $classWithInterface3 = $interface; }