diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 8d1dd0a58cb..9c720ae9f7a 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3401,7 +3401,7 @@ public function unsetExpression(Expr $expr): self [], $this->afterExtractCall, $this->parentScope, - ); + )->invalidateExpression($expr); } elseif ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) { return $this->specifyExpressionType( $expr->var, @@ -3499,7 +3499,7 @@ public function specifyExpressionType(Expr $expr, Type $type, ?Type $nativeType $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, - ); + )->invalidateExpression($expr); } elseif ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) { $constantArrays = TypeUtils::getOldConstantArrays($this->getType($expr->var)); if (count($constantArrays) > 0) { diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 197075c1256..62a608a5d8d 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -920,6 +920,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/remember-possibly-impure-function-values.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/emptyiterator.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/collected-data.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1396.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-1396.php b/tests/PHPStan/Analyser/data/bug-1396.php new file mode 100644 index 00000000000..18d95236e92 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-1396.php @@ -0,0 +1,25 @@ +getValue() === null) { + return new SpecifiedTypes(); + } + + if ( + $constantType instanceof ConstantStringType + ) { + assertType('string', $constantType->getValue()); + } + } +}