Skip to content

Commit 2bef413

Browse files
authored
support non-empty-array type in [] != $arr conditions
1 parent b08e63a commit 2bef413

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPStan\Type\Accessory\HasPropertyType;
2626
use PHPStan\Type\Accessory\NonEmptyArrayType;
2727
use PHPStan\Type\BooleanType;
28+
use PHPStan\Type\Constant\ConstantArrayType;
2829
use PHPStan\Type\Constant\ConstantBooleanType;
2930
use PHPStan\Type\Constant\ConstantIntegerType;
3031
use PHPStan\Type\Constant\ConstantStringType;
@@ -374,6 +375,22 @@ public function specifyTypesInCondition(
374375
);
375376
}
376377

378+
if (
379+
$context->falsey()
380+
&& $rightType->isArray()->yes()
381+
&& $leftType instanceof ConstantArrayType && $leftType->isEmpty()
382+
) {
383+
return $this->create($expr->right, new NonEmptyArrayType(), $context->negate(), false, $scope);
384+
}
385+
386+
if (
387+
$context->falsey()
388+
&& $leftType->isArray()->yes()
389+
&& $rightType instanceof ConstantArrayType && $rightType->isEmpty()
390+
) {
391+
return $this->create($expr->left, new NonEmptyArrayType(), $context->negate(), false, $scope);
392+
}
393+
377394
if (
378395
$expr->left instanceof FuncCall
379396
&& $expr->left->name instanceof Name

tests/PHPStan/Analyser/data/sizeof.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,20 @@ function doFoo4(array $arr): string
4444
}
4545
return "";
4646
}
47+
48+
function doFoo5(array $arr): void
49+
{
50+
if ([] != $arr) {
51+
assertType('array&nonEmpty', $arr);
52+
}
53+
assertType('array', $arr);
54+
}
55+
56+
function doFoo6(array $arr): void
57+
{
58+
if ($arr != []) {
59+
assertType('array&nonEmpty', $arr);
60+
}
61+
assertType('array', $arr);
62+
}
4763
}

0 commit comments

Comments
 (0)