Repository navigation
Expand file tree
/
Copy pathArrayMapParameterClosureTypeExtension.php
More file actions
71 lines (61 loc) · 2.21 KB
/
Copy pathArrayMapParameterClosureTypeExtension.php
File metadata and controls
71 lines (61 loc) · 2.21 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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\ArrayMapCallbackParameterTypes;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Type\ClosureType;
use PHPStan\Type\FunctionParameterClosureTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use function count;
#[AutowiredService]
final class ArrayMapParameterClosureTypeExtension implements FunctionParameterClosureTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool
{
return $functionReflection->getName() === 'array_map' && $parameter->getName() === 'callback';
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope $scope): ?Type
{
$args = $functionCall->getArgs();
if (count($args) < 2) {
return null;
}
$valueTypes = [];
$arrayTypes = [];
$argCount = count($args);
for ($i = 1; $i < $argCount; $i++) {
$arg = $args[$i];
$arrayType = $scope->getType($arg->value);
if ($arg->unpack) {
$constantArrays = $arrayType->getConstantArrays();
if (count($constantArrays) === 0) {
return null;
}
foreach ($constantArrays as $constantArray) {
foreach ($constantArray->getValueTypes() as $valueType) {
$valueTypes[] = $scope->getIterableValueType($valueType);
$arrayTypes[] = $valueType;
}
}
} else {
$valueTypes[] = $scope->getIterableValueType($arrayType);
$arrayTypes[] = $arrayType;
}
}
$callbackParameters = [];
foreach (ArrayMapCallbackParameterTypes::addPaddingNull($valueTypes, $arrayTypes) as $valueType) {
$callbackParameters[] = $this->createParameter($valueType);
}
return new ClosureType($callbackParameters, new MixedType());
}
private function createParameter(Type $type): NativeParameterReflection
{
return new NativeParameterReflection('item', false, $type, PassedByReference::createNo(), false, null);
}
}