Skip to content

Commit df20bcb

Browse files
Fix
1 parent 685a17b commit df20bcb

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

Lines changed: 10 additions & 7 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,28 @@ 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);
42+
[$returnType, $hasError] = $this->arrayCombineHelper->getReturnAndThrowType($firstArg, $secondArg, $scope);
43+
if ($returnType instanceof NeverType) {
44+
return $returnType;
45+
}
4346

4447
if ($hasError->no()) {
45-
return $arrayType;
48+
return $returnType;
4649
}
4750

4851
if ($hasError->yes()) {
49-
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
52+
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
5053
return new NeverType();
5154
}
5255

5356
return new ConstantBooleanType(false);
5457
}
5558

56-
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
57-
return $arrayType;
59+
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
60+
return $returnType;
5861
}
5962

60-
return new UnionType([$arrayType, new ConstantBooleanType(false)]);
63+
return new UnionType([$returnType, new ConstantBooleanType(false)]);
6164
}
6265

6366
}

src/Type/Php/ArrayCombineFunctionThrowTypeExtension.php

Lines changed: 1 addition & 6 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,7 +32,7 @@ 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];
35+
$hasError = $this->arrayCombineHelper->getReturnAndThrowType($firstArg, $secondArg, $scope)[1];
4136
if (!$hasError->no()) {
4237
return $functionReflection->getThrowType();
4338
}

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)