Skip to content

Commit

Permalink
Fix native type on unset
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Dec 14, 2022
1 parent ed62410 commit 21703b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/MutatingScope.php
Expand Up @@ -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)]),
Expand Down
17 changes: 15 additions & 2 deletions tests/PHPStan/Analyser/data/native-expressions.php
Expand Up @@ -3,6 +3,7 @@
namespace NativeExpressions;

use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertNativeType;

function doFoo(): string
{
Expand All @@ -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
};

/**
Expand All @@ -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<mixed~'a', mixed>", $a);
}
}

0 comments on commit 21703b3

Please sign in to comment.