Skip to content

Commit e634e59

Browse files
committed
Resolve WithoutImpurePoints purity transitively through calls to other effect-free callables
- Add PossiblyPureCallTransitivePurityResolver: a shared service that resolves a declaration's impure/throw points into the callable keys it depends on, and computes the transitive closure of effect-free declarations via a fixpoint (base case = no dependencies). Implicit throw points are ignored, mirroring how noop expression statements are detected in NodeScopeResolver; explicit throw points and non-call impure points still mark a declaration as having effects. - ConstructorWithoutImpurePointsCollector, FunctionWithoutImpurePointsCollector and MethodWithoutImpurePointsCollector no longer require zero impure/throw points; they now record the declaration's dependency keys (the possibly-pure callables it calls) so purity can be inferred transitively. - CallToConstructor/Function/Method/StaticMethodStatementWithoutImpurePointsRule now filter collected declarations through the transitive pure set before reporting, so a standalone call to a callable whose body only performs other possibly-pure calls is reported as having no effect (e.g. `Foo::doBar();`). - Fix a latent bug in the constructor and function rules where per-file collector data was destructured with `as [$x]`, only reading the first declaration per file; replaced with proper nested loops now that more declarations are collected per file. - Method-call dependencies reuse the same finality guard as PossiblyPureMethodCallCollector so overridable methods do not make a caller look effect-free. - Update AnalyserIntegrationTest::testBug13982: the anonymous-class `$v->test2()` call is now correctly reported, since test2() only calls the pure private static test(). Closes phpstan/phpstan#14759
1 parent 1643f14 commit e634e59

16 files changed

Lines changed: 551 additions & 48 deletions

‎src/Rules/DeadCode/CallToConstructorStatementWithoutImpurePointsRule.php‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,28 @@
1919
final class CallToConstructorStatementWithoutImpurePointsRule implements Rule
2020
{
2121

22+
public function __construct(private PossiblyPureCallTransitivePurityResolver $purityResolver)
23+
{
24+
}
25+
2226
public function getNodeType(): string
2327
{
2428
return CollectedDataNode::class;
2529
}
2630

2731
public function processNode(Node $node, Scope $scope): array
2832
{
33+
$pureKeys = $this->purityResolver->getPureCallableKeys($node);
34+
2935
$classesWithConstructors = [];
30-
foreach ($node->get(ConstructorWithoutImpurePointsCollector::class) as [$class]) {
31-
$classesWithConstructors[strtolower($class)] = $class;
36+
foreach ($node->get(ConstructorWithoutImpurePointsCollector::class) as $collected) {
37+
foreach ($collected as [$class]) {
38+
if (!isset($pureKeys[PossiblyPureCallTransitivePurityResolver::methodKey($class, '__construct')])) {
39+
continue;
40+
}
41+
42+
$classesWithConstructors[strtolower($class)] = $class;
43+
}
3244
}
3345

3446
$errors = [];

‎src/Rules/DeadCode/CallToFunctionStatementWithoutImpurePointsRule.php‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,28 @@
1919
final class CallToFunctionStatementWithoutImpurePointsRule implements Rule
2020
{
2121

22+
public function __construct(private PossiblyPureCallTransitivePurityResolver $purityResolver)
23+
{
24+
}
25+
2226
public function getNodeType(): string
2327
{
2428
return CollectedDataNode::class;
2529
}
2630

2731
public function processNode(Node $node, Scope $scope): array
2832
{
33+
$pureKeys = $this->purityResolver->getPureCallableKeys($node);
34+
2935
$functions = [];
30-
foreach ($node->get(FunctionWithoutImpurePointsCollector::class) as [$functionName]) {
31-
$functions[strtolower($functionName)] = $functionName;
36+
foreach ($node->get(FunctionWithoutImpurePointsCollector::class) as $collected) {
37+
foreach ($collected as [$functionName]) {
38+
if (!isset($pureKeys[PossiblyPureCallTransitivePurityResolver::functionKey($functionName)])) {
39+
continue;
40+
}
41+
42+
$functions[strtolower($functionName)] = $functionName;
43+
}
3244
}
3345

3446
$errors = [];

‎src/Rules/DeadCode/CallToMethodStatementWithoutImpurePointsRule.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,27 @@
1919
final class CallToMethodStatementWithoutImpurePointsRule implements Rule
2020
{
2121

22+
public function __construct(private PossiblyPureCallTransitivePurityResolver $purityResolver)
23+
{
24+
}
25+
2226
public function getNodeType(): string
2327
{
2428
return CollectedDataNode::class;
2529
}
2630

2731
public function processNode(Node $node, Scope $scope): array
2832
{
33+
$pureKeys = $this->purityResolver->getPureCallableKeys($node);
34+
2935
$methods = [];
3036
foreach ($node->get(MethodWithoutImpurePointsCollector::class) as $collected) {
3137
foreach ($collected as [$className, $methodName, $classDisplayName]) {
32-
$className = strtolower($className);
33-
$methods[$className][strtolower($methodName)] = $classDisplayName . '::' . $methodName;
38+
if (!isset($pureKeys[PossiblyPureCallTransitivePurityResolver::methodKey($className, $methodName)])) {
39+
continue;
40+
}
41+
42+
$methods[strtolower($className)][strtolower($methodName)] = $classDisplayName . '::' . $methodName;
3443
}
3544
}
3645

‎src/Rules/DeadCode/CallToStaticMethodStatementWithoutImpurePointsRule.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,27 @@
1919
final class CallToStaticMethodStatementWithoutImpurePointsRule implements Rule
2020
{
2121

22+
public function __construct(private PossiblyPureCallTransitivePurityResolver $purityResolver)
23+
{
24+
}
25+
2226
public function getNodeType(): string
2327
{
2428
return CollectedDataNode::class;
2529
}
2630

2731
public function processNode(Node $node, Scope $scope): array
2832
{
33+
$pureKeys = $this->purityResolver->getPureCallableKeys($node);
34+
2935
$methods = [];
3036
foreach ($node->get(MethodWithoutImpurePointsCollector::class) as $collected) {
3137
foreach ($collected as [$className, $methodName, $classDisplayName]) {
32-
$lowerClassName = strtolower($className);
33-
$methods[$lowerClassName][strtolower($methodName)] = $classDisplayName . '::' . $methodName;
38+
if (!isset($pureKeys[PossiblyPureCallTransitivePurityResolver::methodKey($className, $methodName)])) {
39+
continue;
40+
}
41+
42+
$methods[strtolower($className)][strtolower($methodName)] = $classDisplayName . '::' . $methodName;
3443
}
3544
}
3645

‎src/Rules/DeadCode/ConstructorWithoutImpurePointsCollector.php‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
use function count;
1111

1212
/**
13-
* @implements Collector<MethodReturnStatementsNode, string>
13+
* @implements Collector<MethodReturnStatementsNode, array{string, list<string>}>
1414
*/
1515
#[RegisteredCollector(level: 4)]
1616
final class ConstructorWithoutImpurePointsCollector implements Collector
1717
{
1818

19+
public function __construct(private PossiblyPureCallTransitivePurityResolver $purityResolver)
20+
{
21+
}
22+
1923
public function getNodeType(): string
2024
{
2125
return MethodReturnStatementsNode::class;
@@ -32,14 +36,6 @@ public function processNode(Node $node, Scope $scope)
3236
return null;
3337
}
3438

35-
if (count($node->getImpurePoints()) !== 0) {
36-
return null;
37-
}
38-
39-
if (count($node->getStatementResult()->getThrowPoints()) !== 0) {
40-
return null;
41-
}
42-
4339
foreach ($method->getParameters() as $parameter) {
4440
if (!$parameter->passedByReference()->createsNewVariable()) {
4541
continue;
@@ -52,7 +48,15 @@ public function processNode(Node $node, Scope $scope)
5248
return null;
5349
}
5450

55-
return $method->getDeclaringClass()->getName();
51+
$dependencies = $this->purityResolver->resolveDependencies(
52+
$node->getImpurePoints(),
53+
$node->getStatementResult()->getThrowPoints(),
54+
);
55+
if ($dependencies === null) {
56+
return null;
57+
}
58+
59+
return [$method->getDeclaringClass()->getName(), $dependencies];
5660
}
5761

5862
}

‎src/Rules/DeadCode/FunctionWithoutImpurePointsCollector.php‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
use function count;
1111

1212
/**
13-
* @implements Collector<FunctionReturnStatementsNode, string>
13+
* @implements Collector<FunctionReturnStatementsNode, array{string, list<string>}>
1414
*/
1515
#[RegisteredCollector(level: 4)]
1616
final class FunctionWithoutImpurePointsCollector implements Collector
1717
{
1818

19+
public function __construct(private PossiblyPureCallTransitivePurityResolver $purityResolver)
20+
{
21+
}
22+
1923
public function getNodeType(): string
2024
{
2125
return FunctionReturnStatementsNode::class;
@@ -31,14 +35,6 @@ public function processNode(Node $node, Scope $scope)
3135
return null;
3236
}
3337

34-
if (count($node->getImpurePoints()) !== 0) {
35-
return null;
36-
}
37-
38-
if (count($node->getStatementResult()->getThrowPoints()) !== 0) {
39-
return null;
40-
}
41-
4238
foreach ($function->getParameters() as $parameter) {
4339
if (!$parameter->passedByReference()->createsNewVariable()) {
4440
continue;
@@ -51,7 +47,15 @@ public function processNode(Node $node, Scope $scope)
5147
return null;
5248
}
5349

54-
return $function->getName();
50+
$dependencies = $this->purityResolver->resolveDependencies(
51+
$node->getImpurePoints(),
52+
$node->getStatementResult()->getThrowPoints(),
53+
);
54+
if ($dependencies === null) {
55+
return null;
56+
}
57+
58+
return [$function->getName(), $dependencies];
5559
}
5660

5761
}

‎src/Rules/DeadCode/MethodWithoutImpurePointsCollector.php‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
use function count;
1111

1212
/**
13-
* @implements Collector<MethodReturnStatementsNode, array{class-string, string, string}>
13+
* @implements Collector<MethodReturnStatementsNode, array{class-string, string, string, list<string>}>
1414
*/
1515
#[RegisteredCollector(level: 4)]
1616
final class MethodWithoutImpurePointsCollector implements Collector
1717
{
1818

19+
public function __construct(private PossiblyPureCallTransitivePurityResolver $purityResolver)
20+
{
21+
}
22+
1923
public function getNodeType(): string
2024
{
2125
return MethodReturnStatementsNode::class;
@@ -31,14 +35,6 @@ public function processNode(Node $node, Scope $scope)
3135
return null;
3236
}
3337

34-
if (count($node->getImpurePoints()) !== 0) {
35-
return null;
36-
}
37-
38-
if (count($node->getStatementResult()->getThrowPoints()) !== 0) {
39-
return null;
40-
}
41-
4238
foreach ($method->getParameters() as $parameter) {
4339
if (!$parameter->passedByReference()->createsNewVariable()) {
4440
continue;
@@ -55,7 +51,15 @@ public function processNode(Node $node, Scope $scope)
5551
return null;
5652
}
5753

58-
return [$method->getDeclaringClass()->getName(), $method->getName(), $method->getDeclaringClass()->getDisplayName()];
54+
$dependencies = $this->purityResolver->resolveDependencies(
55+
$node->getImpurePoints(),
56+
$node->getStatementResult()->getThrowPoints(),
57+
);
58+
if ($dependencies === null) {
59+
return null;
60+
}
61+
62+
return [$method->getDeclaringClass()->getName(), $method->getName(), $method->getDeclaringClass()->getDisplayName(), $dependencies];
5963
}
6064

6165
}

0 commit comments

Comments
 (0)