Skip to content

Commit

Permalink
Specify never for array_combine with different number of elements
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Mar 27, 2023
1 parent 891609a commit d5e1be7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

if (count($keyTypes) !== count($valueTypes)) {
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
return null;
return new NeverType();
}
return new ConstantBooleanType(false);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4555,7 +4555,7 @@ public function dataArrayFunctions(): array
'array_combine([1], [2])',
],
[
PHP_VERSION_ID < 80000 ? 'false' : 'array',
PHP_VERSION_ID < 80000 ? 'false' : '*NEVER*',
'array_combine([1, 2], [3])',
],
[
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/data/array-combine-php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ function withNonEmptyArray(array $a, array $b): void
{
assertType("non-empty-array<'bar'|'baz'|'foo', 'apple'|'avocado'|'banana'>|false", array_combine($a, $b));
}

function withDifferentNumberOfElements(): void
{
assertType('false', array_combine(['foo'], ['bar', 'baz']));
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/data/array-combine-php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ function withNonEmptyArray(array $a, array $b): void
{
assertType("non-empty-array<'bar'|'baz'|'foo', 'apple'|'avocado'|'banana'>", array_combine($a, $b));
}

function withDifferentNumberOfElements(): void
{
assertType('*NEVER*', array_combine(['foo'], ['bar', 'baz']));
}

0 comments on commit d5e1be7

Please sign in to comment.