Skip to content

Commit 0fffab2

Browse files
github-actions[bot]phpstan-bot
authored andcommitted
Fix phpstan/phpstan#12062: False positive "has no effect" on static:: call to overridable method
- Skip collecting static:: calls in PossiblyPureStaticCallCollector when the method can be overridden - A subclass may override the method with side effects, so the call cannot be assumed pure - Updated existing test expectations for static:: calls in non-final classes - New regression test in tests/PHPStan/Rules/DeadCode/data/bug-12062.php
1 parent 4c6ef6e commit 0fffab2

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

src/Rules/DeadCode/PossiblyPureStaticCallCollector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function processNode(Node $node, Scope $scope)
6363
return null;
6464
}
6565

66+
if (
67+
$expr->class->toLowerString() === 'static'
68+
&& $scope->isInClass()
69+
&& !$scope->getClassReflection()->isFinal()
70+
&& !$methodReflection->isFinal()->yes()
71+
&& !$methodReflection->isPrivate()
72+
) {
73+
return null;
74+
}
75+
6676
return [$methodReflection->getDeclaringClass()->getName(), $methodReflection->getName(), $node->getStartLine()];
6777
}
6878

tests/PHPStan/Rules/DeadCode/CallToStaticMethodStatementWithoutImpurePointsRuleTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public function testRule(): void
4444
'Call to CallToStaticMethodWithoutImpurePoints\SubSubY::mySubSubFunc() on a separate line has no effect.',
4545
21,
4646
],
47-
[
48-
'Call to CallToStaticMethodWithoutImpurePoints\y::myFunc() on a separate line has no effect.',
49-
48,
50-
],
5147
[
5248
'Call to CallToStaticMethodWithoutImpurePoints\y::myFunc() on a separate line has no effect.',
5349
53,
@@ -59,6 +55,11 @@ public function testRule(): void
5955
]);
6056
}
6157

58+
public function testBug12062(): void
59+
{
60+
$this->analyse([__DIR__ . '/data/bug-12062.php'], []);
61+
}
62+
6263
#[RequiresPhp('>= 8.5')]
6364
public function testPipeOperator(): void
6465
{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug12062;
4+
5+
abstract class Base
6+
{
7+
public static function get(mixed $param): mixed
8+
{
9+
static::validateParam($param);
10+
return $param;
11+
}
12+
13+
public static function set(mixed $param, mixed $value): void
14+
{
15+
static::validateParam($param);
16+
}
17+
18+
public static function clear(mixed $param): void
19+
{
20+
static::validateParam($param);
21+
}
22+
23+
protected static function validateParam(mixed $param): bool
24+
{
25+
return true;
26+
}
27+
}
28+
29+
abstract class IntParam extends Base
30+
{
31+
protected static function validateParam(mixed $param): bool
32+
{
33+
if (!is_int($param)) {
34+
throw new \InvalidArgumentException('Must be int');
35+
}
36+
return true;
37+
}
38+
}

0 commit comments

Comments
 (0)