diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 79d4be017b..64e4607473 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -2869,7 +2869,7 @@ public function invalidateExpression(Expr $expressionToInvalidate, bool $require continue; } $firstExpr = $holders[array_key_first($holders)]->getTypeHolder()->getExpr(); - if ($this->shouldInvalidateExpression($exprStringToInvalidate, $expressionToInvalidate, $firstExpr, $this->getNodeKey($firstExpr), false, $invalidatingClass)) { + if ($this->shouldInvalidateExpression($exprStringToInvalidate, $expressionToInvalidate, $firstExpr, $this->getNodeKey($firstExpr), $requireMoreCharacters, $invalidatingClass)) { $invalidated = true; continue; } diff --git a/tests/PHPStan/Analyser/nsrt/bug-14545.php b/tests/PHPStan/Analyser/nsrt/bug-14545.php new file mode 100644 index 0000000000..e12b2e9df1 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-14545.php @@ -0,0 +1,112 @@ + $class_name + * @return T + */ +function getObject1(string $class_name): object { + return new $class_name; +} + +function testStoredInstanceofWithGenericMethodCall(): void { + $obj = getObject1(ObjectClass::class); + $is_interface = $obj instanceof SomeInterface; + if($is_interface) { + assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj); + $obj->test(); + } + + if($is_interface) { + assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj); + $obj->test(); + } +} + +function testStoredInstanceofWithGenericFuncCall(): void { + $obj = getObject1(ObjectClass::class); + $is_interface = $obj instanceof SomeInterface; + if($is_interface) { + var_dump($obj); + } + + if($is_interface) { + assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj); + } +} + +function testStoredInstanceofWithConcreteClass(): void { + $obj = getObject1(OtherClass::class); + $is_interface = $obj instanceof SomeInterface; + if($is_interface) { + assertType('Bug14545\OtherClass&Bug14545\SomeInterface', $obj); + $obj->test(); + } + + if($is_interface) { + assertType('Bug14545\OtherClass&Bug14545\SomeInterface', $obj); + } +} + +function getObject2(): object { + return new \stdClass(); +} + +function testStoredInstanceofWithAbstractObject(): void { + $obj = getObject2(); + $is_interface = $obj instanceof SomeInterface; + if($is_interface) { + assertType('Bug14545\SomeInterface', $obj); + $obj->test(); + } + + if($is_interface) { + assertType('Bug14545\SomeInterface', $obj); + $obj->test(); + } +} + +function testThreeConsecutiveChecks(): void { + $obj = getObject1(ObjectClass::class); + $is_interface = $obj instanceof SomeInterface; + if($is_interface) { + $obj->test(); + } + if($is_interface) { + $obj->test(); + } + if($is_interface) { + assertType('Bug14545\ObjectClass&Bug14545\SomeInterface', $obj); + } +} + +/** + * @param array $data + */ +function testStoredIsArray(array $data): void { + $value = $data['key'] ?? null; + $isArray = is_array($value); + if ($isArray) { + assertType('array', $value); + var_dump($value); + } + if ($isArray) { + assertType('array', $value); + } +}