-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathArrayAllFunctionTypeSpecifyingExtension.php
More file actions
112 lines (95 loc) · 3.18 KB
/
ArrayAllFunctionTypeSpecifyingExtension.php
File metadata and controls
112 lines (95 loc) · 3.18 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use function array_find;
use function count;
use function is_string;
use function strtolower;
#[AutowiredService]
final class ArrayAllFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private TypeSpecifier $typeSpecifier;
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
{
return strtolower($functionReflection->getName()) === 'array_all'
&& !$context->null();
}
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$args = $node->getArgs();
if (!$context->truthy() || count($args) < 2) {
return new SpecifiedTypes();
}
$array = $args[0]->value;
$callable = $args[1]->value;
if ($callable instanceof Expr\ArrowFunction) {
$callableExpr = $callable->expr;
} elseif (
$callable instanceof Expr\Closure &&
count($callable->stmts) === 1 &&
$callable->stmts[0] instanceof Stmt\Return_ &&
isset($callable->stmts[0]->expr)
) {
$callableExpr = $callable->stmts[0]->expr;
} else {
return new SpecifiedTypes();
}
$callableParams = $callable->params;
$specifiedTypesInFuncCall = $this->typeSpecifier->specifyTypesInCondition($scope, $callableExpr, $context)->getSureTypes();
if (
isset($callableParams[0]) &&
$callableParams[0]->var instanceof Variable &&
is_string($callableParams[0]->var->name)
) {
$valueType = $this->fetchTypeByVariable($specifiedTypesInFuncCall, $callableParams[0]->var->name);
}
if (
isset($callableParams[1]) &&
$callableParams[1]->var instanceof Variable &&
is_string($callableParams[1]->var->name)
) {
$keyType = $this->fetchTypeByVariable($specifiedTypesInFuncCall, $callableParams[1]->var->name);
}
if (isset($keyType) || isset($valueType)) {
return $this->typeSpecifier->create(
$array,
new ArrayType($keyType ?? new MixedType(), $valueType ?? new MixedType()),
$context,
$scope,
);
}
return new SpecifiedTypes();
}
/**
* @param array<string, list{Expr, Type}> $specifiedTypes
*/
private function fetchTypeByVariable(array $specifiedTypes, string $variableName): ?Type
{
$specifiedTypeOfKey = array_find(
$specifiedTypes,
static fn ($specifiedType) => $specifiedType[0] instanceof Expr\Variable && $specifiedType[0]->name === $variableName,
);
if (isset($specifiedTypeOfKey)) {
return $specifiedTypeOfKey[1];
}
return null;
}
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
}