Skip to content

Commit ed8c14b

Browse files
committed
refactor
1 parent 016134d commit ed8c14b

1 file changed

Lines changed: 15 additions & 23 deletions

File tree

src/Analyser/TypeSpecifier.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,6 @@ public function specifyTypesInCondition(
849849

850850
if ($isNonEmpty) {
851851
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);
852-
853852
$specifiedTypes = $specifiedTypes->unionWith(
854853
$this->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope),
855854
);
@@ -858,7 +857,7 @@ public function specifyTypesInCondition(
858857
$nonNullKeyType = TypeCombinator::removeNull($keyType);
859858
if (!$nonNullKeyType instanceof NeverType) {
860859
$specifiedTypes = $specifiedTypes->unionWith(
861-
$this->createArrayDimFetchConditionalExpressionHolder($expr->var, $arrayArg, $arrayType, $nonNullKeyType),
860+
$this->createArrayDimFetchConditionalExpressionHolder($expr->var, $arrayArg, $nonNullKeyType, $arrayType->getIterableValueType()),
862861
);
863862
}
864863
}
@@ -875,12 +874,12 @@ public function specifyTypesInCondition(
875874
$funcName = $expr->expr->name->toLowerString();
876875
$arrayArgIndex = null;
877876
$sentinelType = null;
878-
$needleArgIndex = null;
877+
$isStrictArraySearch = false;
879878

880879
if ($funcName === 'array_search') {
881880
$arrayArgIndex = 1;
882881
$sentinelType = new ConstantBooleanType(false);
883-
$needleArgIndex = 0;
882+
$isStrictArraySearch = count($expr->expr->getArgs()) >= 3 && $scope->getType($expr->expr->getArgs()[2]->value)->isTrue()->yes();
884883
} elseif ($funcName === 'array_find_key') {
885884
$arrayArgIndex = 0;
886885
$sentinelType = new NullType();
@@ -898,14 +897,11 @@ public function specifyTypesInCondition(
898897

899898
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);
900899

901-
$dimFetchType = $arrayType->getIterableValueType();
902-
if (
903-
$needleArgIndex !== null
904-
&& count($expr->expr->getArgs()) >= 3
905-
&& $scope->getType($expr->expr->getArgs()[2]->value)->isTrue()->yes()
906-
) {
907-
$needleType = $scope->getType($expr->expr->getArgs()[$needleArgIndex]->value);
908-
$dimFetchType = TypeCombinator::intersect($needleType, $dimFetchType);
900+
if ($isStrictArraySearch) {
901+
$needleType = $scope->getType($expr->expr->getArgs()[0]->value);
902+
$dimFetchType = TypeCombinator::intersect($needleType, $arrayType->getIterableValueType());
903+
} else {
904+
$dimFetchType = $arrayType->getIterableValueType();
909905
}
910906

911907
$specifiedTypes = $specifiedTypes->unionWith(
@@ -915,17 +911,14 @@ public function specifyTypesInCondition(
915911
$keyType = $scope->getType($expr->expr);
916912
$narrowedKeyType = TypeCombinator::remove($keyType, $sentinelType);
917913
if (!$narrowedKeyType instanceof NeverType) {
918-
$dimFetchType = null;
919-
if (
920-
$needleArgIndex !== null
921-
&& count($expr->expr->getArgs()) >= 3
922-
&& $scope->getType($expr->expr->getArgs()[2]->value)->isTrue()->yes()
923-
) {
924-
$needleType = $scope->getType($expr->expr->getArgs()[$needleArgIndex]->value);
914+
if ($isStrictArraySearch) {
915+
$needleType = $scope->getType($expr->expr->getArgs()[0]->value);
925916
$dimFetchType = TypeCombinator::intersect($needleType, $arrayType->getIterableValueType());
917+
} else {
918+
$dimFetchType = $arrayType->getIterableValueType();
926919
}
927920
$specifiedTypes = $specifiedTypes->unionWith(
928-
$this->createArrayDimFetchConditionalExpressionHolder($expr->var, $arrayArg, $arrayType, $narrowedKeyType, $dimFetchType),
921+
$this->createArrayDimFetchConditionalExpressionHolder($expr->var, $arrayArg, $narrowedKeyType, $dimFetchType),
929922
);
930923
}
931924
}
@@ -2436,9 +2429,8 @@ public function create(
24362429
private function createArrayDimFetchConditionalExpressionHolder(
24372430
Expr\Variable $keyVar,
24382431
Expr $arrayArg,
2439-
Type $arrayType,
24402432
Type $narrowedKeyType,
2441-
?Type $dimFetchType = null,
2433+
Type $dimFetchType,
24422434
): SpecifiedTypes
24432435
{
24442436
$dimFetch = new ArrayDimFetch($arrayArg, $keyVar);
@@ -2447,7 +2439,7 @@ private function createArrayDimFetchConditionalExpressionHolder(
24472439

24482440
$holder = new ConditionalExpressionHolder(
24492441
[$keyExprString => ExpressionTypeHolder::createYes($keyVar, $narrowedKeyType)],
2450-
ExpressionTypeHolder::createYes($dimFetch, $dimFetchType ?? $arrayType->getIterableValueType()),
2442+
ExpressionTypeHolder::createYes($dimFetch, $dimFetchType),
24512443
);
24522444

24532445
return (new SpecifiedTypes([], []))->setNewConditionalExpressionHolders([

0 commit comments

Comments
 (0)