-
Notifications
You must be signed in to change notification settings - Fork 430
/
ExplodeFunctionDynamicReturnTypeExtension.php
74 lines (63 loc) · 2.21 KB
/
ExplodeFunctionDynamicReturnTypeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function count;
class ExplodeFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function __construct(private PhpVersion $phpVersion)
{
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'explode';
}
public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type
{
if (count($functionCall->getArgs()) < 2) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}
$delimiterType = $scope->getType($functionCall->getArgs()[0]->value);
$isSuperset = (new ConstantStringType(''))->isSuperTypeOf($delimiterType);
if ($isSuperset->yes()) {
if ($this->phpVersion->getVersionId() >= 80000) {
return new NeverType();
}
return new ConstantBooleanType(false);
} elseif ($isSuperset->no()) {
$arrayType = new ArrayType(new IntegerType(), new StringType());
if (
!isset($functionCall->getArgs()[2])
|| IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($scope->getType($functionCall->getArgs()[2]->value))->yes()
) {
return TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
}
return $arrayType;
}
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
if ($delimiterType instanceof MixedType) {
return TypeUtils::toBenevolentUnion($returnType);
}
return $returnType;
}
}