Skip to content

Commit 70fe6a2

Browse files
committed
Use native magic dispatcher purity for annotated methods
1 parent b020813 commit 70fe6a2

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

‎src/Reflection/Annotations/AnnotationsMethodsClassReflectionExtension.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ private function findClassReflectionWithMethod(
7676
: null;
7777
$classResolvedPhpDoc = $classReflection->getResolvedPhpDoc();
7878
$isPure = null;
79-
if ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsPure()) {
79+
if ($nativeCallMethod !== null) {
80+
$nativePurity = $nativeCallMethod->isPure();
81+
$isPure = $nativePurity->yes() ? true : ($nativePurity->no() ? false : null);
82+
} elseif ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsPure()) {
8083
$isPure = true;
8184
} elseif ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsImpure()) {
8285
$isPure = false;
8386
}
84-
if ($isPure === true && $nativeCallMethod !== null && $nativeCallMethod->isPure()->no()) {
85-
$isPure = false;
86-
}
8787

8888
return new AnnotationMethodReflection(
8989
$methodName,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ public function testBug15322(): void
351351
{
352352
$this->treatPhpDocTypesAsCertain = true;
353353
$this->analyse([__DIR__ . '/data/bug-15322.php'], [
354-
['Impure call to method Bug15322\\ImpureDispatcher::magic() in pure method Bug15322\\Test::runImpureDispatcher().', 50],
355-
['Impure call to method Bug15322\\ImpureStaticDispatcher::magic() in pure method Bug15322\\Test::runImpureStaticDispatcher().', 56],
354+
['Impure call to method Bug15322\\ImpureDispatcher::magic() in pure method Bug15322\\Test::runImpureDispatcher().', 63],
355+
['Impure call to method Bug15322\\ImpureStaticDispatcher::magic() in pure method Bug15322\\Test::runImpureStaticDispatcher().', 69],
356356
]);
357357
}
358358

‎tests/PHPStan/Rules/Pure/data/bug-15322.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public static function __callStatic(string $name, array $arguments): int
3636
}
3737
}
3838

39+
/**
40+
* @phpstan-all-methods-impure
41+
* @method int magic()
42+
*/
43+
class PureDispatcher
44+
{
45+
/** @phpstan-pure */
46+
public function __call(string $name, array $arguments): int
47+
{
48+
return 1;
49+
}
50+
}
51+
3952
class Test
4053
{
4154
/** @phpstan-pure */
@@ -55,4 +68,10 @@ public function runImpureStaticDispatcher(): int
5568
{
5669
return ImpureStaticDispatcher::magic();
5770
}
71+
72+
/** @phpstan-pure */
73+
public function runPureDispatcher(PureDispatcher $object): int
74+
{
75+
return $object->magic();
76+
}
5877
}

0 commit comments

Comments
 (0)