Skip to content

Commit c74dca6

Browse files
VincentLangletondrejmirtes
authored andcommitted
DynamicReturnType for array search call not strict
1 parent 829b674 commit c74dca6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4242
}
4343

4444
if ($argsCount < 3) {
45-
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
45+
return TypeCombinator::union($haystackArgType->getIterableKeyType(), new ConstantBooleanType(false));
4646
}
4747

4848
$strictArgType = $scope->getType($functionCall->args[2]->value);
49-
if (!($strictArgType instanceof ConstantBooleanType) || $strictArgType->getValue() === false) {
49+
if (!($strictArgType instanceof ConstantBooleanType)) {
5050
return TypeCombinator::union($haystackArgType->getIterableKeyType(), new ConstantBooleanType(false), new NullType());
51+
} elseif ($strictArgType->getValue() === false) {
52+
return TypeCombinator::union($haystackArgType->getIterableKeyType(), new ConstantBooleanType(false));
5153
}
5254

5355
$needleArgType = $scope->getType($functionCall->args[0]->value);

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5261,6 +5261,14 @@ public function dataArrayFunctions(): array
52615261
'string|false',
52625262
'array_search(9, $generalStringKeys, true)',
52635263
],
5264+
[
5265+
'string|false',
5266+
'array_search(9, $generalStringKeys, false)',
5267+
],
5268+
[
5269+
'string|false',
5270+
'array_search(9, $generalStringKeys)',
5271+
],
52645272
[
52655273
'null',
52665274
'array_search(999, $integer, true)',
@@ -5274,7 +5282,7 @@ public function dataArrayFunctions(): array
52745282
'array_search($mixed, $array, true)',
52755283
],
52765284
[
5277-
'int|string|false|null',
5285+
'int|string|false',
52785286
'array_search($mixed, $array, false)',
52795287
],
52805288
[
@@ -5298,7 +5306,7 @@ public function dataArrayFunctions(): array
52985306
'array_search($generalIntegerOrString, $clonedConditionalArray, true)',
52995307
],
53005308
[
5301-
'int|string|false|null',
5309+
'int|string|false',
53025310
'array_search($generalIntegerOrString, $generalIntegerOrStringKeys, false)',
53035311
],
53045312
[

0 commit comments

Comments
 (0)