Skip to content

Commit db58aea

Browse files
committed
support array-size
1 parent 732e7fd commit db58aea

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
@@ -2263,14 +2263,23 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
22632263
$leftArrayType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value);
22642264
$rightArrayType = $scope->getType($unwrappedRightExpr->getArgs()[0]->value);
22652265

2266-
if (
2267-
$leftArrayType->isArray()->yes() && $rightArrayType->isArray()->yes()
2268-
&& ($leftArrayType->isIterableAtLeastOnce()->yes() || $rightArrayType->isIterableAtLeastOnce()->yes())
2269-
) {
2270-
$arrayTypes = $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr);
2271-
return $arrayTypes->unionWith(
2272-
$this->create($unwrappedRightExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr),
2273-
);
2266+
if ($leftArrayType->isArray()->yes() && $rightArrayType->isArray()->yes()) {
2267+
$argType = $scope->getType($unwrappedRightExpr->getArgs()[0]->value);
2268+
$sizeType = $scope->getType($leftExpr);
2269+
2270+
if ($sizeType instanceof IntegerRangeType || $sizeType->isConstantScalarValue()->yes()) {
2271+
$specifiedTypes = $this->specifyTypesForCountFuncCall($unwrappedRightExpr, $argType, $sizeType, $context, $scope, $expr);
2272+
if ($specifiedTypes !== null) {
2273+
return $specifiedTypes;
2274+
}
2275+
}
2276+
2277+
if ($leftArrayType->isIterableAtLeastOnce()->yes() || $rightArrayType->isIterableAtLeastOnce()->yes()) {
2278+
$arrayTypes = $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr);
2279+
return $arrayTypes->unionWith(
2280+
$this->create($unwrappedRightExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr),
2281+
);
2282+
}
22742283
}
22752284
}
22762285

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)