From 21703b31eaef64e79872fc5fd29381116aa019ab Mon Sep 17 00:00:00 2001 From: Yohta Kimura <38206553+rajyan@users.noreply.github.com> Date: Wed, 14 Dec 2022 13:57:06 +0900 Subject: [PATCH] Fix native type on unset --- src/Analyser/MutatingScope.php | 4 ++-- .../Analyser/data/native-expressions.php | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 9bdf036528..8c0f92c3b9 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3352,8 +3352,8 @@ public function unsetExpression(Expr $expr): self $exprVarType = $scope->getType($expr->var); $dimType = $scope->getType($expr->dim); $unsetType = $exprVarType->unsetOffset($dimType); - $exprVarNativeType = $scope->getType($expr->var); - $dimNativeType = $scope->getType($expr->dim); + $exprVarNativeType = $scope->getNativeType($expr->var); + $dimNativeType = $scope->getNativeType($expr->dim); $unsetNativeType = $exprVarNativeType->unsetOffset($dimNativeType); $scope = $scope->assignExpression($expr->var, $unsetType, $unsetNativeType)->invalidateExpression( new FuncCall(new FullyQualified('count'), [new Arg($expr->var)]), diff --git a/tests/PHPStan/Analyser/data/native-expressions.php b/tests/PHPStan/Analyser/data/native-expressions.php index a4e7b0a369..97e8789655 100644 --- a/tests/PHPStan/Analyser/data/native-expressions.php +++ b/tests/PHPStan/Analyser/data/native-expressions.php @@ -3,6 +3,7 @@ namespace NativeExpressions; use function PHPStan\Testing\assertType; +use function PHPStan\Testing\assertNativeType; function doFoo(): string { @@ -12,7 +13,7 @@ function (): void { /** @var non-empty-string $a */ $a = doFoo(); assertType('non-empty-string', $a); - assertNativeType('string', $a); + assertNativeType('mixed', $a); // could be fixed }; /** @@ -34,10 +35,22 @@ public function __construct( private array $array ){ assertType('non-empty-array', $this->array); - assertNativeType('array', $this->array); + assertNativeType('non-empty-array', $this->array); // could be fixed issue https://github.com/phpstan/phpstan/issues/6260 if(count($array) === 0){ throw new \InvalidArgumentException(); } } + + /** + * @param array{a: 'b'} $a + * @return void + */ + public function doUnset(array $a){ + assertType("array{a: 'b'}", $a); + assertNativeType('array', $a); + unset($a['a']); + assertType("array{}", $a); + assertNativeType("array", $a); + } }