Skip to content

Commit 4111d0f

Browse files
committed
StaticPropertyFetch is an impure point
1 parent 791e708 commit 4111d0f

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/Analyser/ImpurePoint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPStan\Node\VirtualNode;
77

88
/**
9-
* @phpstan-type ImpurePointIdentifier = 'echo'|'die'|'exit'|'propertyAssign'|'propertyAssignByRef'|'propertyUnset'|'methodCall'|'new'|'functionCall'|'include'|'require'|'print'|'eval'|'superglobal'|'yield'|'yieldFrom'|'static'|'global'|'betweenPhpTags'
9+
* @phpstan-type ImpurePointIdentifier = 'echo'|'die'|'exit'|'propertyAssign'|'propertyAssignByRef'|'propertyUnset'|'methodCall'|'new'|'functionCall'|'include'|'require'|'print'|'eval'|'superglobal'|'yield'|'yieldFrom'|'static'|'global'|'betweenPhpTags'|'staticPropertyAccess'
1010
* @api
1111
* @final
1212
*/

src/Analyser/NodeScopeResolver.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2908,7 +2908,15 @@ static function (): void {
29082908
} elseif ($expr instanceof StaticPropertyFetch) {
29092909
$hasYield = false;
29102910
$throwPoints = [];
2911-
$impurePoints = [];
2911+
$impurePoints = [
2912+
new ImpurePoint(
2913+
$scope,
2914+
$expr,
2915+
'staticPropertyAccess',
2916+
'static property access',
2917+
true,
2918+
),
2919+
];
29122920
if ($expr->class instanceof Expr) {
29132921
$result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep());
29142922
$hasYield = $result->hasYield();

tests/PHPStan/Rules/Pure/PureMethodRuleTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ public function testRule(): void
140140
'Possibly impure call to a callable in pure method PureMethod\MaybeCallableFromUnion::doFoo().',
141141
330,
142142
],
143+
[
144+
'Impure static property access in pure method PureMethod\StaticMethodAccessingStaticProperty::getA().',
145+
388,
146+
],
147+
[
148+
'Impure property assignment in pure method PureMethod\StaticMethodAssigningStaticProperty::getA().',
149+
409,
150+
],
143151
]);
144152
}
145153

@@ -151,6 +159,10 @@ public function testPureConstructor(): void
151159

152160
$this->treatPhpDocTypesAsCertain = true;
153161
$this->analyse([__DIR__ . '/data/pure-constructor.php'], [
162+
[
163+
'Impure static property access in pure method PureConstructor\Foo::__construct().',
164+
19,
165+
],
154166
[
155167
'Impure property assignment in pure method PureConstructor\Foo::__construct().',
156168
19,

tests/PHPStan/Rules/Pure/data/pure-method.php

+46
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,49 @@ public function assertSth($value): void
375375
}
376376

377377
}
378+
379+
class StaticMethodAccessingStaticProperty
380+
{
381+
public static int $a = 0;
382+
383+
/**
384+
* @phpstan-pure
385+
*/
386+
public static function getA(): int
387+
{
388+
return self::$a;
389+
}
390+
391+
/**
392+
* @phpstan-impure
393+
*/
394+
public static function getB(): int
395+
{
396+
return self::$a;
397+
}
398+
}
399+
400+
class StaticMethodAssigningStaticProperty
401+
{
402+
public static int $a = 0;
403+
404+
/**
405+
* @phpstan-pure
406+
*/
407+
public static function getA(): int
408+
{
409+
self::$a = 1;
410+
411+
return 1;
412+
}
413+
414+
/**
415+
* @phpstan-impure
416+
*/
417+
public static function getB(): int
418+
{
419+
self::$a = 1;
420+
421+
return 1;
422+
}
423+
}

0 commit comments

Comments
 (0)