Skip to content

Commit 3dd6562

Browse files
committed
Let @pure-unless-* tags take precedence over the class-level purity tags
1 parent d679562 commit 3dd6562

13 files changed

Lines changed: 498 additions & 7 deletions

‎src/Analyser/PhpDocsResolver.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,15 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
215215
$acceptsNamedArguments = $scope->getClassReflection()->acceptsNamedArguments();
216216
}
217217

218-
if ($isPure === null && $node instanceof Node\FunctionLike && $scope->isInClass()) {
218+
// A @pure-unless-* tag on the method, written or inherited, states its purity
219+
// just like @phpstan-pure does, so the class-level tags do not override it.
220+
if (
221+
$isPure === null
222+
&& $phpDocPureUnlessCallableIsImpureParameters === []
223+
&& $phpDocPureUnlessParameterPassedParameters === []
224+
&& $node instanceof Node\FunctionLike
225+
&& $scope->isInClass()
226+
) {
219227
// a set hook has no return type node of its own, but it always returns
220228
// void - the class-level @phpstan-pure must not make it pure
221229
$isSetHook = $node instanceof Node\PropertyHook && $node->name->toLowerString() === 'set';

‎src/Reflection/Php/PhpClassReflectionExtension.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,9 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
983983
}
984984
}
985985

986-
if ($isPure === null) {
986+
// A @pure-unless-* tag on the method, written or inherited, states its purity
987+
// just like @phpstan-pure does, so the class-level tags do not override it.
988+
if ($isPure === null && $pureUnlessCallableIsImpureParameters === [] && $pureUnlessParameterPassedParameters === []) {
987989
$classResolvedPhpDoc = $phpDocBlockClassReflection->getResolvedPhpDoc();
988990
if ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsPure()) {
989991
if (

‎tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ public function testPureUnlessCallableIsImpureOverride(): void
656656
'Impure method MethodSignaturePureUnlessCallable\ImpureChild::run() overrides method MethodSignaturePureUnlessCallable\PureUnlessParent::run() marked @pure-unless-callable-is-impure.',
657657
21,
658658
],
659+
[
660+
'Impure method MethodSignaturePureUnlessCallable\ImpureChildOfAllMethodsPureParent::run() overrides method MethodSignaturePureUnlessCallable\AllMethodsPureParent::run() marked @pure-unless-callable-is-impure.',
661+
62,
662+
],
659663
]);
660664
}
661665

@@ -670,8 +674,8 @@ public function testPureUnlessParameterPassedOverride(): void
670674
22,
671675
],
672676
[
673-
'Impure method MethodSignaturePureUnlessParameterPassed\AllMethodsImpureChild::replace() overrides method MethodSignaturePureUnlessParameterPassed\PureUnlessParent::replace() marked @pure-unless-parameter-passed.',
674-
76,
677+
'Impure method MethodSignaturePureUnlessParameterPassed\ImpureChildOfAllMethodsPureParent::replace() overrides method MethodSignaturePureUnlessParameterPassed\AllMethodsPureParent::replace() marked @pure-unless-parameter-passed.',
678+
114,
675679
],
676680
]);
677681
}

‎tests/PHPStan/Rules/Methods/data/method-signature-pure-unless-callable.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,34 @@ public function run(callable $cb): int
3636
}
3737

3838
}
39+
40+
/**
41+
* @phpstan-all-methods-pure
42+
*/
43+
class AllMethodsPureParent
44+
{
45+
46+
/**
47+
* @pure-unless-callable-is-impure $cb
48+
*/
49+
public function run(callable $cb): int
50+
{
51+
return $cb(1);
52+
}
53+
54+
}
55+
56+
class ImpureChildOfAllMethodsPureParent extends AllMethodsPureParent
57+
{
58+
59+
/**
60+
* @phpstan-impure
61+
*/
62+
public function run(callable $cb): int
63+
{
64+
echo 'side effect';
65+
66+
return $cb(1);
67+
}
68+
69+
}

‎tests/PHPStan/Rules/Methods/data/method-signature-pure-unless-parameter-passed.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public function replace(string $subject, int &$count = 0): string
6868
}
6969

7070
/**
71+
* The inherited @pure-unless-parameter-passed takes precedence over the
72+
* class-level tag, as an inherited @phpstan-pure does.
73+
*
7174
* @phpstan-all-methods-impure
7275
*/
7376
class AllMethodsImpureChild implements PureUnlessParent
@@ -82,3 +85,38 @@ public function replace(string $subject, int &$count = 0): string
8285
}
8386

8487
}
88+
89+
/**
90+
* @phpstan-all-methods-pure
91+
*/
92+
class AllMethodsPureParent
93+
{
94+
95+
/**
96+
* @param-out int $count
97+
* @pure-unless-parameter-passed $count
98+
*/
99+
public function replace(string $subject, int &$count = 0): string
100+
{
101+
$count = 1;
102+
103+
return $subject;
104+
}
105+
106+
}
107+
108+
class ImpureChildOfAllMethodsPureParent extends AllMethodsPureParent
109+
{
110+
111+
/**
112+
* @phpstan-impure
113+
*/
114+
public function replace(string $subject, int &$count = 0): string
115+
{
116+
echo 'side effect';
117+
$count = 1;
118+
119+
return $subject;
120+
}
121+
122+
}

‎tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,26 @@ public function testPureUnlessParameterPassedBuiltin(): void
541541
]);
542542
}
543543

544+
public function testPureUnlessAllMethodsPure(): void
545+
{
546+
$this->analyse([__DIR__ . '/data/pure-unless-all-methods-pure.php'], [
547+
[
548+
'Impure call to method PureUnlessAllMethodsPure\InheritingReplacer::replace() in pure function PureUnlessAllMethodsPure\passingCount().',
549+
111,
550+
],
551+
[
552+
'Impure call to method PureUnlessAllMethodsPure\Replacer::replace() in pure function PureUnlessAllMethodsPure\passingCount().',
553+
111,
554+
],
555+
[
556+
'Impure call to method PureUnlessAllMethodsPure\InheritingReplacer::map() in pure function PureUnlessAllMethodsPure\impureCallback().',
557+
135,
558+
],
559+
[
560+
'Impure call to method PureUnlessAllMethodsPure\Replacer::map() in pure function PureUnlessAllMethodsPure\impureCallback().',
561+
135,
562+
],
563+
]);
564+
}
565+
544566
}

‎tests/PHPStan/Rules/Pure/PureMethodRuleTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,18 @@ public function testPureUnlessParameterPassed(): void
438438
]);
439439
}
440440

441+
public function testPureUnlessAllMethodsPure(): void
442+
{
443+
$this->analyse([__DIR__ . '/data/pure-unless-all-methods-pure.php'], [
444+
[
445+
'Impure echo in pure method PureUnlessAllMethodsPure\ImpureClassInheritingReplacer::replace().',
446+
78,
447+
],
448+
[
449+
'Impure echo in pure method PureUnlessAllMethodsPure\ImpureClassInheritingReplacer::map().',
450+
86,
451+
],
452+
]);
453+
}
454+
441455
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PureUnlessAllMethodsPure;
4+
5+
/**
6+
* @phpstan-all-methods-pure
7+
*/
8+
class Replacer
9+
{
10+
11+
/**
12+
* @param-out int $count
13+
* @pure-unless-parameter-passed $count
14+
*/
15+
public function replace(string $subject, int &$count = 0): string
16+
{
17+
$count = 1;
18+
19+
return $subject;
20+
}
21+
22+
/**
23+
* @param callable(string): string $cb
24+
* @pure-unless-callable-is-impure $cb
25+
*/
26+
public function map(callable $cb, string $subject): string
27+
{
28+
return $cb($subject);
29+
}
30+
31+
}
32+
33+
interface ReplacerInterface
34+
{
35+
36+
/**
37+
* @param-out int $count
38+
* @pure-unless-parameter-passed $count
39+
*/
40+
public function replace(string $subject, int &$count = 0): string;
41+
42+
/**
43+
* @param callable(string): string $cb
44+
* @pure-unless-callable-is-impure $cb
45+
*/
46+
public function map(callable $cb, string $subject): string;
47+
48+
}
49+
50+
/**
51+
* @phpstan-all-methods-pure
52+
*/
53+
class InheritingReplacer implements ReplacerInterface
54+
{
55+
56+
public function replace(string $subject, int &$count = 0): string
57+
{
58+
$count = 1;
59+
60+
return $subject;
61+
}
62+
63+
public function map(callable $cb, string $subject): string
64+
{
65+
return $cb($subject);
66+
}
67+
68+
}
69+
70+
/**
71+
* @phpstan-all-methods-impure
72+
*/
73+
class ImpureClassInheritingReplacer implements ReplacerInterface
74+
{
75+
76+
public function replace(string $subject, int &$count = 0): string
77+
{
78+
echo $subject;
79+
$count = 1;
80+
81+
return $subject;
82+
}
83+
84+
public function map(callable $cb, string $subject): string
85+
{
86+
echo $subject;
87+
88+
return $cb($subject);
89+
}
90+
91+
}
92+
93+
/**
94+
* @phpstan-pure
95+
*/
96+
function omittingCount(Replacer $r, InheritingReplacer $i, string $s): string
97+
{
98+
// $count is omitted, so the calls stay pure.
99+
return $r->replace($s) . $i->replace($s);
100+
}
101+
102+
/**
103+
* @phpstan-pure
104+
*/
105+
function passingCount(Replacer $r, InheritingReplacer $i, string $s): string
106+
{
107+
$c = 0;
108+
$d = 0;
109+
// $count is passed, so the calls are impure although the classes are marked
110+
// @phpstan-all-methods-pure.
111+
return $r->replace($s, $c) . $i->replace($s, $d);
112+
}
113+
114+
/**
115+
* @phpstan-pure
116+
*/
117+
function pureCallback(Replacer $r, InheritingReplacer $i, string $s): string
118+
{
119+
return $r->map(static fn (string $x): string => $x, $s) . $i->map(static fn (string $x): string => $x, $s);
120+
}
121+
122+
/**
123+
* @phpstan-pure
124+
*/
125+
function impureCallback(Replacer $r, InheritingReplacer $i, string $s): string
126+
{
127+
// The callback is impure, so the calls are impure although the classes are
128+
// marked @phpstan-all-methods-pure.
129+
$cb = static function (string $x): string {
130+
echo $x;
131+
132+
return $x;
133+
};
134+
135+
return $r->map($cb, $s) . $i->map($cb, $s);
136+
}

‎turbo-ext/src/PhpClassReflectionExtension.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,13 @@ class PhpClassReflectionExtension
24202420
}
24212421
}
24222422

2423-
if (isPure < 0) {
2423+
// A @pure-unless-* tag on the method, written or inherited, states its purity
2424+
// just like @phpstan-pure does, so the class-level tags do not override it.
2425+
if (
2426+
isPure < 0
2427+
&& zend_hash_num_elements(pureUnlessCallableIsImpureParameters.table()) == 0
2428+
&& zend_hash_num_elements(pureUnlessParameterPassedParameters.table()) == 0
2429+
) {
24242430
zv::Val classResolvedPhpDoc = call(phpDocBlockClassReflection.raw(), PT_LC("getresolvedphpdoc"));
24252431
if (UNEXPECTED(classResolvedPhpDoc.isUndef())) return zv::Val();
24262432
if (Z_TYPE_P(classResolvedPhpDoc.raw()) != IS_NULL) {

‎turbo-ext/src/PhpDocsResolver.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,14 @@ class PhpDocsResolver
489489
}
490490
}
491491

492-
if (isPure.isNull() && isFunctionLike) {
492+
// A @pure-unless-* tag on the method, written or inherited, states its purity
493+
// just like @phpstan-pure does, so the class-level tags do not override it.
494+
if (
495+
isPure.isNull()
496+
&& zend_hash_num_elements(phpDocPureUnlessCallableIsImpureParameters.table()) == 0
497+
&& zend_hash_num_elements(phpDocPureUnlessParameterPassedParameters.table()) == 0
498+
&& isFunctionLike
499+
) {
493500
bool stillInClass;
494501
if (UNEXPECTED(!pt_scope_is_in_class(Z_OBJ_P(scope), stillInClass))) return false;
495502
if (stillInClass && UNEXPECTED(!resolveClassPurity(scope, node, classReflection, functionName.raw(), phpDocReturnType.raw(), isPure))) return false;

0 commit comments

Comments
 (0)