Skip to content

Commit 024c32a

Browse files
committed
PossiblyPureMethodCallCollector - declaring class does not have to be final, but the callee class might
1 parent 11699c0 commit 024c32a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/Rules/DeadCode/PossiblyPureMethodCallCollector.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Stmt\Expression;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Collectors\Collector;
9+
use function count;
910
use function strtolower;
1011

1112
/**
@@ -37,13 +38,21 @@ public function processNode(Node $node, Scope $scope)
3738
return null;
3839
}
3940

40-
$methodReflection = $scope->getMethodReflection($scope->getType($node->expr->var), $methodName);
41+
$calledOnType = $scope->getType($node->expr->var);
42+
$methodReflection = $scope->getMethodReflection($calledOnType, $methodName);
4143
if ($methodReflection === null) {
4244
return null;
4345
}
4446
if (!$methodReflection->isFinal()->yes()) {
4547
if (!$methodReflection->getDeclaringClass()->isFinal()) {
46-
return null;
48+
$typeClassReflections = $calledOnType->getObjectClassReflections();
49+
if (count($typeClassReflections) !== 1) {
50+
return null;
51+
}
52+
53+
if (!$typeClassReflections[0]->isFinal()) {
54+
return null;
55+
}
4756
}
4857
}
4958
if (!$methodReflection->isPure()->maybe()) {

tests/PHPStan/Rules/DeadCode/CallToMethodStatementWithoutImpurePointsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function testRule(): void
3535
'Call to method CallToMethodWithoutImpurePoints\y::myFinalBaseFunc() on a separate line has no effect.',
3636
36,
3737
],
38+
[
39+
'Call to method CallToMethodWithoutImpurePoints\y::myFunc() on a separate line has no effect.',
40+
39,
41+
],
3842
[
3943
'Call to method CallToMethodWithoutImpurePoints\finalSubSubY::mySubSubFunc() on a separate line has no effect.',
4044
40,
@@ -43,6 +47,10 @@ public function testRule(): void
4347
'Call to method CallToMethodWithoutImpurePoints\y::myFinalBaseFunc() on a separate line has no effect.',
4448
41,
4549
],
50+
[
51+
'Call to method CallToMethodWithoutImpurePoints\AbstractFoo::myFunc() on a separate line has no effect.',
52+
119,
53+
],
4654
]);
4755
}
4856

tests/PHPStan/Rules/DeadCode/data/call-to-method-without-impure-points.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,20 @@ function throwingFunc()
101101
throw new \Exception();
102102
}
103103
}
104+
105+
abstract class AbstractFoo
106+
{
107+
108+
function myFunc()
109+
{
110+
}
111+
112+
}
113+
final class FinalFoo extends AbstractFoo
114+
{
115+
116+
}
117+
118+
function (FinalFoo $foo): void {
119+
$foo->myFunc();
120+
};

0 commit comments

Comments
 (0)