Skip to content

Commit ce4e3dd

Browse files
committed
support array-size
1 parent 42d2acd commit ce4e3dd

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,14 +2280,23 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
22802280
$leftArrayType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value);
22812281
$rightArrayType = $scope->getType($unwrappedRightExpr->getArgs()[0]->value);
22822282

2283-
if (
2284-
$leftArrayType->isArray()->yes() && $rightArrayType->isArray()->yes()
2285-
&& ($leftArrayType->isIterableAtLeastOnce()->yes() || $rightArrayType->isIterableAtLeastOnce()->yes())
2286-
) {
2287-
$arrayTypes = $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr);
2288-
return $arrayTypes->unionWith(
2289-
$this->create($unwrappedRightExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr),
2290-
);
2283+
if ($leftArrayType->isArray()->yes() && $rightArrayType->isArray()->yes()) {
2284+
$argType = $scope->getType($unwrappedRightExpr->getArgs()[0]->value);
2285+
$sizeType = $scope->getType($leftExpr);
2286+
2287+
if ($sizeType instanceof IntegerRangeType || $sizeType->isConstantScalarValue()->yes()) {
2288+
$specifiedTypes = $this->specifyTypesForCountFuncCall($unwrappedRightExpr, $argType, $sizeType, $context, $scope, $expr);
2289+
if ($specifiedTypes !== null) {
2290+
return $specifiedTypes;
2291+
}
2292+
}
2293+
2294+
if ($leftArrayType->isIterableAtLeastOnce()->yes() || $rightArrayType->isIterableAtLeastOnce()->yes()) {
2295+
$arrayTypes = $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr);
2296+
return $arrayTypes->unionWith(
2297+
$this->create($unwrappedRightExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr),
2298+
);
2299+
}
22912300
}
22922301
}
22932302

tests/PHPStan/Analyser/nsrt/list-count2.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@ public function sayUnknownArray($arrA, array $arrB): void
7878
}
7979
assertType('array<string>', $arrB);
8080
}
81+
82+
/**
83+
* @param array{int, int, int} $arrA
84+
* @param list $arrB
85+
*/
86+
function sayEqualArrayShape($arrA, array $arrB): void
87+
{
88+
if (count($arrA) == count($arrB)) {
89+
assertType('array{mixed, mixed, mixed}', $arrB);
90+
}
91+
assertType('list', $arrB);
92+
}
8193
}

0 commit comments

Comments
 (0)