forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyFetchHandler.php
More file actions
166 lines (145 loc) · 5.95 KB
/
PropertyFetchHandler.php
File metadata and controls
166 lines (145 loc) · 5.95 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\ExprHandler;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\ExprHandler\Helper\NullsafeShortCircuitingHelper;
use PHPStan\Analyser\InternalThrowPoint;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_map;
use function array_merge;
use function count;
/**
* @implements ExprHandler<PropertyFetch>
*/
#[AutowiredService]
final class PropertyFetchHandler implements ExprHandler
{
/**
* Representative property name used when resolving dynamic property access ($obj->{$expr}).
* The actual name doesn't matter — it just needs to be non-empty so that
* PropertiesClassReflectionExtensions (e.g. SimpleXMLElement) that accept
* any property name can return the correct type.
*/
private const DYNAMIC_PROPERTY_NAME = '__phpstan_dynamic_property';
public function __construct(
private PhpVersion $phpVersion,
private PropertyReflectionFinder $propertyReflectionFinder,
)
{
}
public function supports(Expr $expr): bool
{
return $expr instanceof PropertyFetch;
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$scopeBeforeVar = $scope;
$varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep());
$hasYield = $varResult->hasYield();
$throwPoints = $varResult->getThrowPoints();
$impurePoints = $varResult->getImpurePoints();
$isAlwaysTerminating = $varResult->isAlwaysTerminating();
$scope = $varResult->getScope();
if ($expr->name instanceof Identifier) {
$propertyName = $expr->name->toString();
$propertyHolderType = $scopeBeforeVar->getType($expr->var);
$propertyReflection = $scopeBeforeVar->getInstancePropertyReflection($propertyHolderType, $propertyName);
if ($propertyReflection !== null && $this->phpVersion->supportsPropertyHooks()) {
$propertyDeclaringClass = $propertyReflection->getDeclaringClass();
if ($propertyDeclaringClass->hasNativeProperty($propertyName)) {
$nativeProperty = $propertyDeclaringClass->getNativeProperty($propertyName);
$throwPoints = array_merge($throwPoints, $nodeScopeResolver->getThrowPointsFromPropertyHook($scopeBeforeVar, $expr, $nativeProperty, 'get'));
}
}
} else {
$nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep());
$hasYield = $hasYield || $nameResult->hasYield();
$throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $nameResult->isAlwaysTerminating();
$scope = $nameResult->getScope();
if ($this->phpVersion->supportsPropertyHooks()) {
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
}
}
return new ExpressionResult(
$scope,
hasYield: $hasYield,
isAlwaysTerminating: $isAlwaysTerminating,
throwPoints: $throwPoints,
impurePoints: $impurePoints,
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
);
}
public function resolveType(MutatingScope $scope, Expr $expr): Type
{
if ($expr->name instanceof Identifier) {
if ($scope->nativeTypesPromoted) {
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $scope);
if ($propertyReflection === null) {
return new ErrorType();
}
if (!$propertyReflection->hasNativeType()) {
return new MixedType();
}
$nativeType = $propertyReflection->getNativeType();
return NullsafeShortCircuitingHelper::getType($scope, $expr->var, $nativeType);
}
$returnType = $this->propertyFetchType(
$scope,
$scope->getType($expr->var),
$expr->name->name,
$expr,
);
if ($returnType === null) {
$returnType = new ErrorType();
}
return NullsafeShortCircuitingHelper::getType($scope, $expr->var, $returnType);
}
$nameType = $scope->getType($expr->name);
if (count($nameType->getConstantStrings()) > 0) {
return TypeCombinator::union(
...array_map(static fn ($constantString) => $constantString->getValue() === '' ? new ErrorType() : $scope
->filterByTruthyValue(new Expr\BinaryOp\Identical($expr->name, new String_($constantString->getValue())))
->getType(
new PropertyFetch($expr->var, new Identifier($constantString->getValue())),
), $nameType->getConstantStrings()),
);
}
if ($nameType->isString()->yes()) {
$fetchedOnType = $scope->getType($expr->var);
$returnType = $this->propertyFetchType($scope, $fetchedOnType, self::DYNAMIC_PROPERTY_NAME, $expr);
if ($returnType !== null) {
return NullsafeShortCircuitingHelper::getType($scope, $expr->var, $returnType);
}
}
return new MixedType();
}
private function propertyFetchType(MutatingScope $scope, Type $fetchedOnType, string $propertyName, PropertyFetch $propertyFetch): ?Type
{
$propertyReflection = $scope->getInstancePropertyReflection($fetchedOnType, $propertyName);
if ($propertyReflection === null) {
return null;
}
if ($scope->isInExpressionAssign($propertyFetch)) {
return $propertyReflection->getWritableType();
}
return $propertyReflection->getReadableType();
}
}