Skip to content

Commit 04edd04

Browse files
staabmondrejmirtes
authored andcommitted
Fix false positive with array_combine() on php8
1 parent b4edf6b commit 04edd04

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Php\PhpVersion;
99
use PHPStan\Reflection\FunctionReflection;
10-
use PHPStan\Reflection\ParametersAcceptorSelector;
1110
use PHPStan\Type\Accessory\NonEmptyArrayType;
1211
use PHPStan\Type\ArrayType;
1312
use PHPStan\Type\Constant\ConstantArrayType;
@@ -36,10 +35,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3635
return $functionReflection->getName() === 'array_combine';
3736
}
3837

39-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
38+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4039
{
4140
if (count($functionCall->getArgs()) < 2) {
42-
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
41+
return null;
4342
}
4443

4544
$firstArg = $functionCall->getArgs()[0]->value;
@@ -56,6 +55,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5655
$valueTypes = $valuesParamType->getValueTypes();
5756

5857
if (count($keyTypes) !== count($valueTypes)) {
58+
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
59+
return null;
60+
}
5961
return new ConstantBooleanType(false);
6062
}
6163

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4555,7 +4555,7 @@ public function dataArrayFunctions(): array
45554555
'array_combine([1], [2])',
45564556
],
45574557
[
4558-
'false',
4558+
PHP_VERSION_ID < 80000 ? 'false' : 'array',
45594559
'array_combine([1, 2], [3])',
45604560
],
45614561
[

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,4 +846,19 @@ public function testBug8879(): void
846846
$this->analyse([__DIR__ . '/data/bug-8879.php'], []);
847847
}
848848

849+
public function testBug9011(): void
850+
{
851+
$errors = [];
852+
if (PHP_VERSION_ID < 80000) {
853+
$errors = [
854+
[
855+
'Method Bug9011\HelloWorld::getX() should return array<string> but returns false.',
856+
16,
857+
],
858+
];
859+
}
860+
861+
$this->analyse([__DIR__ . '/data/bug-9011.php'], $errors);
862+
}
863+
849864
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug9011;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @return string[]
11+
*/
12+
public function getX(): array
13+
{
14+
$a = array('green', 'red', 'yellow', 'y');
15+
$b = array('avocado', 'apple', 'banana');
16+
return array_combine($a, $b);
17+
}
18+
}

0 commit comments

Comments
 (0)