From f3313df760041a0bd4221d7df3ee1278a1fe1681 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 8 Aug 2025 22:54:05 +0200 Subject: [PATCH] Resolve static in assert phpdoc --- .../Dummy/ChangedTypeMethodReflection.php | 3 +- ...ackUnresolvedMethodPrototypeReflection.php | 1 + ...ypeUnresolvedMethodPrototypeReflection.php | 1 + .../Rules/Methods/CallMethodsRuleTest.php | 10 +++ .../PHPStan/Rules/Methods/data/bug-12548.php | 86 +++++++++++++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-12548.php diff --git a/src/Reflection/Dummy/ChangedTypeMethodReflection.php b/src/Reflection/Dummy/ChangedTypeMethodReflection.php index fc4b21656b..9a345c22cd 100644 --- a/src/Reflection/Dummy/ChangedTypeMethodReflection.php +++ b/src/Reflection/Dummy/ChangedTypeMethodReflection.php @@ -27,6 +27,7 @@ public function __construct( private ?array $namedArgumentsVariants, private ?Type $selfOutType, private ?Type $throwType, + private Assertions $assertions, ) { } @@ -133,7 +134,7 @@ public function hasSideEffects(): TrinaryLogic public function getAsserts(): Assertions { - return $this->reflection->getAsserts(); + return $this->assertions; } public function acceptsNamedArguments(): TrinaryLogic diff --git a/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php b/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php index 2874a465fd..1fba733364 100644 --- a/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php +++ b/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php @@ -137,6 +137,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass, $namedArgumentVariants, $selfOutType, $throwType, + $method->getAsserts()->mapTypes($this->transformStaticTypeCallback), ); } diff --git a/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php b/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php index 44709d32ae..68d3200bec 100644 --- a/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php +++ b/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php @@ -132,6 +132,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass, $namedArgumentsVariants, $selfOutType, $throwType, + $method->getAsserts()->mapTypes(fn (Type $type): Type => $this->transformStaticType($type)), ); } diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index ff3ba64c16..5c3882e039 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -3608,6 +3608,16 @@ public function testBug9141(): void $this->analyse([__DIR__ . '/data/bug-9141.php'], []); } + public function testBug12548(): void + { + $this->checkThisOnly = false; + $this->checkNullables = true; + $this->checkUnionTypes = true; + $this->checkExplicitMixed = true; + + $this->analyse([__DIR__ . '/data/bug-12548.php'], []); + } + public function testBug3589(): void { $this->checkThisOnly = false; diff --git a/tests/PHPStan/Rules/Methods/data/bug-12548.php b/tests/PHPStan/Rules/Methods/data/bug-12548.php new file mode 100644 index 0000000000..2ea2544bac --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-12548.php @@ -0,0 +1,86 @@ +createStdSat(); + $o->foo(); // @phpstan-ignore method.nonObject (EXPECTED) + + $o = $this->createStdSat(); + if (StdSat::assertInstanceOf($o)) { + $o->foo(); + } + + $o = $this->createStdSat(); + StdSat::assertInstanceOf2($o); + $o->foo(); + + $o = $this->createStdSat(); + StdSat::assertInstanceOf3($o); + $o->foo(); + } +}