Skip to content

Commit 97ced60

Browse files
Fix
1 parent 685a17b commit 97ced60

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class ArrayCombineFunctionReturnTypeExtension implements DynamicFunctionRe
2020

2121
public function __construct(
2222
private ArrayCombineHelper $arrayCombineHelper,
23-
private PhpVersion $phpVersion
23+
private PhpVersion $phpVersion,
2424
)
2525
{
2626
}
@@ -39,25 +39,24 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3939
$firstArg = $functionCall->getArgs()[0]->value;
4040
$secondArg = $functionCall->getArgs()[1]->value;
4141

42-
[$arrayType, $hasError] = $this->arrayCombineHelper->getArrayAndThrowType($firstArg, $secondArg, $scope);
43-
44-
if ($hasError->no()) {
45-
return $arrayType;
42+
[$returnType, $hasValueError] = $this->arrayCombineHelper->getReturnAndThrowType($firstArg, $secondArg, $scope);
43+
if ($hasValueError->no()) {
44+
return $returnType;
4645
}
4746

48-
if ($hasError->yes()) {
49-
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
47+
if ($hasValueError->yes()) {
48+
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
5049
return new NeverType();
5150
}
5251

5352
return new ConstantBooleanType(false);
5453
}
5554

56-
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
57-
return $arrayType;
55+
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
56+
return $returnType;
5857
}
5958

60-
return new UnionType([$arrayType, new ConstantBooleanType(false)]);
59+
return new UnionType([$returnType, new ConstantBooleanType(false)]);
6160
}
6261

6362
}

src/Type/Php/ArrayCombineFunctionThrowTypeExtension.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
8-
use PHPStan\Php\PhpVersion;
98
use PHPStan\Reflection\FunctionReflection;
10-
use PHPStan\Type\Constant\ConstantBooleanType;
11-
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
129
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
13-
use PHPStan\Type\NeverType;
1410
use PHPStan\Type\Type;
15-
use PHPStan\Type\UnionType;
1611
use function count;
1712

1813
#[AutowiredService]
@@ -37,8 +32,8 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
3732
$firstArg = $funcCall->getArgs()[0]->value;
3833
$secondArg = $funcCall->getArgs()[1]->value;
3934

40-
$hasError = $this->arrayCombineHelper->getArrayAndThrowType($firstArg, $secondArg, $scope)[1];
41-
if (!$hasError->no()) {
35+
$hasValueError = $this->arrayCombineHelper->getReturnAndThrowType($firstArg, $secondArg, $scope)[1];
36+
if (!$hasValueError->no()) {
4237
return $functionReflection->getThrowType();
4338
}
4439

src/Type/Php/ArrayCombineHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ final class ArrayCombineHelper
2626
{
2727

2828
/**
29-
* @return array{Type, TrinaryLogic} The array result and if an error may occur.
29+
* @return array{Type, TrinaryLogic} The return type and if a ValueError may occur on PHP8 (and a warning on PHP7).
3030
*/
31-
public function getArrayAndThrowType(Expr $firstArg, Expr $secondArg, Scope $scope): array
31+
public function getReturnAndThrowType(Expr $firstArg, Expr $secondArg, Scope $scope): array
3232
{
3333
$keysParamType = $scope->getType($firstArg);
3434
$valuesParamType = $scope->getType($secondArg);
@@ -79,7 +79,7 @@ public function getArrayAndThrowType(Expr $firstArg, Expr $secondArg, Scope $sco
7979

8080
if ($itemType->isInteger()->no()) {
8181
if ($itemType->toString() instanceof ErrorType) {
82-
return [new NeverType(), TrinaryLogic::createYes()];
82+
return [new NeverType(), TrinaryLogic::createNo()];
8383
}
8484

8585
$keyType = $itemType->toString();

0 commit comments

Comments
 (0)