Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
private bool $isVariadic,
private ?Type $throwType,
private TemplateTypeMap $templateTypeMap,
private ?bool $isPure = null,
)
{
}
Expand Down Expand Up @@ -132,6 +133,9 @@ public function hasSideEffects(): TrinaryLogic
if ($this->returnType->isVoid()->yes()) {
return TrinaryLogic::createYes();
}
if ($this->isPure !== null) {
return TrinaryLogic::createFromBoolean(!$this->isPure);
}

if ((new ThisType($this->declaringClass))->isSuperTypeOf($this->returnType)->yes()) {
return TrinaryLogic::createYes();
Expand Down Expand Up @@ -172,11 +176,7 @@ public function isAbstract(): TrinaryLogic

public function isPure(): TrinaryLogic
{
if ($this->hasSideEffects()->yes()) {
return TrinaryLogic::createNo();
}

return TrinaryLogic::createMaybe();
return $this->hasSideEffects()->negate();
}

public function getPureUnlessCallableIsImpureParameters(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ private function findClassReflectionWithMethod(

$isStatic = $methodTags[$methodName]->isStatic();
$nativeCallMethodName = $isStatic ? '__callStatic' : '__call';
$nativeCallMethod = $classReflection->hasNativeMethod($nativeCallMethodName)
? $classReflection->getNativeMethod($nativeCallMethodName)
: null;
$classResolvedPhpDoc = $classReflection->getResolvedPhpDoc();
$isPure = null;
if ($nativeCallMethod !== null) {
$nativePurity = $nativeCallMethod->isPure();
$isPure = $nativePurity->yes() ? true : ($nativePurity->no() ? false : null);
} elseif ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsPure()) {
$isPure = true;
} elseif ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsImpure()) {
$isPure = false;
}

return new AnnotationMethodReflection(
$methodName,
Expand All @@ -84,10 +97,9 @@ private function findClassReflectionWithMethod(
$parameters,
$isStatic,
$this->detectMethodVariadic($parameters),
$classReflection->hasNativeMethod($nativeCallMethodName)
? $classReflection->getNativeMethod($nativeCallMethodName)->getThrowType()
: null,
$nativeCallMethod !== null ? $nativeCallMethod->getThrowType() : null,
$templateTypeMap,
$isPure,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use AnnotationsMethods\BazBaz;
use AnnotationsMethods\Foo;
use AnnotationsMethods\FooInterface;
use AnnotationsMethods\ImpureMagicMethods;
use AnnotationsMethods\PureMagicMethods;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Reflection\Php\PhpMethodReflection;
Expand Down Expand Up @@ -1032,4 +1034,19 @@ public function testOverridingNativeMethodsWithAnnotationsDoesNotBreakGetNativeM
$this->assertInstanceOf(PhpMethodReflection::class, $class->getNativeMethod('overridenMethodWithAnnotation'));
}

public function testMagicMethodsInheritClassPurity(): void
{
$reflectionProvider = self::createReflectionProvider();
foreach ([PureMagicMethods::class => true, ImpureMagicMethods::class => false] as $className => $isPure) {
$class = $reflectionProvider->getClass($className);
$scope = $this->createStub(Scope::class);
$scope->method('isInClass')->willReturn(true);
$scope->method('getClassReflection')->willReturn($class);
$scope->method('canCallMethod')->willReturn(true);
$method = $class->getMethod('magic', $scope);
$this->assertSame($isPure, $method->isPure()->yes());
$this->assertSame(!$isPure, $method->hasSideEffects()->yes());
}
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Reflection/Annotations/data/annotations-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,19 @@ trait FooTrait
{

}

/**
* @phpstan-all-methods-pure
* @method int magic()
Comment on lines +152 to +153

@staabm staabm Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my gut feeling is, that magic behaviour and purity kind of contradict each other.

phpstan cannot give guarantes for a method it will not see/know and I think people will shoot into their own foot by doing impure stuff in such magic context without seeing it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you point it out, it's indeed annoying that

  • PHPStan won't report impurity in magic
  • People cannot annotate a magic method as impure even when all are annotated as pure (which is doable with a well defined method).

*/
class PureMagicMethods
{
}

/**
* @phpstan-all-methods-impure
* @method int magic()
*/
class ImpureMagicMethods
{
}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Pure/PureMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ public function testAllMethodsArePure(): void
]);
}

public function testBug15322(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-15322.php'], [
['Impure call to method Bug15322\\ImpureDispatcher::magic() in pure method Bug15322\\Test::runImpureDispatcher().', 63],
['Impure call to method Bug15322\\ImpureStaticDispatcher::magic() in pure method Bug15322\\Test::runImpureStaticDispatcher().', 69],
]);
}

#[RequiresPhp('>= 8.0.0')]
public function testBug14138Pure(): void
{
Expand Down
77 changes: 77 additions & 0 deletions tests/PHPStan/Rules/Pure/data/bug-15322.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Bug15322;

/**
* @phpstan-all-methods-pure
* @method int magic()
*/
class MagicMethods
{
}

/**
* @phpstan-all-methods-pure
* @method int magic()
*/
class ImpureDispatcher
{
/** @phpstan-impure */
public function __call(string $name, array $arguments): int
{
return time();
}
}

/**
* @phpstan-all-methods-pure
* @method static int magic()
*/
class ImpureStaticDispatcher
{
/** @phpstan-impure */
public static function __callStatic(string $name, array $arguments): int
{
return time();
}
}

/**
* @phpstan-all-methods-impure
* @method int magic()
*/
class PureDispatcher
{
/** @phpstan-pure */
public function __call(string $name, array $arguments): int
{
return 1;
}
}

class Test
{
/** @phpstan-pure */
public function run(MagicMethods $object): int
{
return $object->magic();
}

/** @phpstan-pure */
public function runImpureDispatcher(ImpureDispatcher $object): int
{
return $object->magic();
}

/** @phpstan-pure */
public function runImpureStaticDispatcher(): int
{
return ImpureStaticDispatcher::magic();
}

/** @phpstan-pure */
public function runPureDispatcher(PureDispatcher $object): int
{
return $object->magic();
}
}
Loading