Skip to content

Commit 79692c6

Browse files
authored
Fix build for latest PHPStan 2.1.8 (#296)
1 parent 45c9ff8 commit 79692c6

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "^7.4 || ^8.0",
14-
"phpstan/phpstan": "^2.0"
14+
"phpstan/phpstan": "^2.1.8"
1515
},
1616
"require-dev": {
1717
"editorconfig-checker/editorconfig-checker": "^10.6.0",

composer.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Rule/ClassSuffixNamingRule.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
class ClassSuffixNamingRule implements Rule
2020
{
2121

22+
private ReflectionProvider $reflectionProvider;
23+
2224
/**
2325
* @var array<class-string, string>
2426
*/
@@ -35,6 +37,7 @@ public function __construct(ReflectionProvider $reflectionProvider, array $super
3537
}
3638
}
3739

40+
$this->reflectionProvider = $reflectionProvider;
3841
$this->superclassToSuffixMapping = $superclassToSuffixMapping;
3942
}
4043

@@ -63,7 +66,9 @@ public function processNode(
6366
}
6467

6568
foreach ($this->superclassToSuffixMapping as $superClass => $suffix) {
66-
if (!$classReflection->isSubclassOf($superClass)) {
69+
$superClassReflection = $this->reflectionProvider->getClass($superClass);
70+
71+
if (!$classReflection->isSubclassOfClass($superClassReflection)) {
6772
continue;
6873
}
6974

src/Rule/ForbidUnusedExceptionRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function isException(Expr $node, Scope $scope): bool
9595
$type = $scope->getType($node);
9696

9797
foreach ($type->getObjectClassReflections() as $classReflection) {
98-
if ($classReflection->isSubclassOf(Throwable::class)) {
98+
if ($classReflection->is(Throwable::class)) {
9999
return true;
100100
}
101101
}

src/Rule/ForbidVariableTypeOverwritingRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private function removeNullAccessoryAndSubtractedTypes(Type $type): Type
112112
$newInnerTypes = [];
113113

114114
foreach ($type->getTypes() as $innerType) {
115-
if ($innerType instanceof AccessoryType) { // @phpstan-ignore phpstanApi.instanceofType
115+
if ($innerType instanceof AccessoryType) {
116116
continue;
117117
}
118118

tests/Rule/data/ForbidUnusedMatchResultRule/code.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testUnused(object $class, bool $bool, int $int): void
8888
true => 1,
8989
};
9090

91-
match ($int) { // error: Unused match result detected, possible returns: Exception
91+
match ($int) { // error: Unused match result detected, possible returns: Exception|LogicException|RuntimeException
9292
0 => new LogicException(),
9393
1 => new RuntimeException(),
9494
default => new Exception(),

tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
namespace ForbidVariableTypeOverwritingRule;
44

5+
/**
6+
* Make PHPStan lose info about possible descendants (new Foo)
7+
*
8+
* @template T
9+
* @param T
10+
* @return T
11+
*/
12+
function passThru($iterable) {
13+
return $iterable;
14+
}
15+
516
interface SomeInterface {
617

718
}
@@ -39,14 +50,14 @@ function testGeneralizationAndNarrowing(
3950
ChildClass1 $childClass1,
4051
ChildClass2 $childClass2,
4152
) {
42-
$childClass1 = new ParentClass();
53+
$childClass1 = passThru(new ParentClass());
4354
$parentClass = new ChildClass2();
4455
$childClass2 = new ChildClass1(); // error: Overwriting variable $childClass2 while changing its type from ForbidVariableTypeOverwritingRule\ChildClass2 to ForbidVariableTypeOverwritingRule\ChildClass1
4556

4657
$object = new ParentClass();
4758
$intOrString1 = 1;
4859
$intOrString2 = []; // error: Overwriting variable $intOrString2 while changing its type from int|string to array{}
49-
$classWithInterface1 = new ParentClass();
60+
$classWithInterface1 = passThru(new ParentClass());
5061
$classWithInterface2 = new AnotherClassWithInterface(); // error: Overwriting variable $classWithInterface2 while changing its type from ForbidVariableTypeOverwritingRule\ParentClass&ForbidVariableTypeOverwritingRule\SomeInterface to ForbidVariableTypeOverwritingRule\AnotherClassWithInterface
5162
$classWithInterface3 = $interface;
5263
}

0 commit comments

Comments
 (0)