forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetTypeFunctionTypeSpecifyingExtension.php
More file actions
112 lines (100 loc) · 2.94 KB
/
SetTypeFunctionTypeSpecifyingExtension.php
File metadata and controls
112 lines (100 loc) · 2.94 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\FuncCall;
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\BooleanType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FloatType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeCombinator;
use stdClass;
use function count;
use function strtolower;
#[AutowiredService]
final class SetTypeFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private TypeSpecifier $typeSpecifier;
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
{
return strtolower($functionReflection->getName()) === 'settype'
&& count($node->getArgs()) > 1
&& $context->null();
}
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$args = $node->getArgs();
$value = $args[0]->value;
$valueType = $scope->getType($value);
$castType = $scope->getType($args[1]->value);
$constantStrings = $castType->getConstantStrings();
if (count($constantStrings) < 1) {
return $this->typeSpecifier->create(
$value,
TypeCombinator::union(
new BooleanType(),
new IntegerType(),
new FloatType(),
new StringType(),
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(stdClass::class),
new NullType(),
),
TypeSpecifierContext::createTruthy(),
$scope,
)->setAlwaysOverwriteTypes();
}
$types = [];
foreach ($constantStrings as $constantString) {
switch ($constantString->getValue()) {
case 'bool':
case 'boolean':
$types[] = $valueType->toBoolean();
break;
case 'int':
case 'integer':
$types[] = $valueType->toInteger();
break;
case 'float':
case 'double':
$types[] = $valueType->toFloat();
break;
case 'string':
$types[] = $valueType->toString();
break;
case 'array':
$types[] = $valueType->toArray();
break;
case 'object':
$types[] = new ObjectType(stdClass::class);
break;
case 'null':
$types[] = new NullType();
break;
default:
$types[] = new ErrorType();
}
}
return $this->typeSpecifier->create(
$value,
TypeCombinator::union(...$types),
TypeSpecifierContext::createTruthy(),
$scope,
)->setAlwaysOverwriteTypes();
}
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
}