Skip to content

Fix build for latest PHPStan 2.1.8 #296

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

Merged
merged 2 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Rule/ClassSuffixNamingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class ClassSuffixNamingRule implements Rule
{

private ReflectionProvider $reflectionProvider;

/**
* @var array<class-string, string>
*/
Expand All @@ -35,6 +37,7 @@ public function __construct(ReflectionProvider $reflectionProvider, array $super
}
}

$this->reflectionProvider = $reflectionProvider;
$this->superclassToSuffixMapping = $superclassToSuffixMapping;
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rule/ForbidUnusedExceptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/ForbidVariableTypeOverwritingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Rule/data/ForbidUnusedMatchResultRule/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 13 additions & 2 deletions tests/Rule/data/ForbidVariableTypeOverwritingRule/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

}
Expand Down Expand Up @@ -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;
}
Expand Down