Skip to content

Commit 383e106

Browse files
committed
More expr handlers
1 parent a4b453e commit 383e106

47 files changed

Lines changed: 4671 additions & 2483 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\ExprHandler;
4+
5+
use ArrayAccess;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Expr\ArrayDimFetch;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Stmt;
10+
use PHPStan\Analyser\ExpressionContext;
11+
use PHPStan\Analyser\ExpressionResult;
12+
use PHPStan\Analyser\ExpressionResultStorage;
13+
use PHPStan\Analyser\ExprHandler;
14+
use PHPStan\Analyser\MutatingScope;
15+
use PHPStan\Analyser\NodeScopeResolver;
16+
use PHPStan\Analyser\NoopNodeCallback;
17+
use PHPStan\DependencyInjection\AutowiredService;
18+
use PHPStan\Type\ObjectType;
19+
use function array_merge;
20+
21+
/**
22+
* @implements ExprHandler<ArrayDimFetch>
23+
*/
24+
#[AutowiredService]
25+
final class ArrayDimFetchHandler implements ExprHandler
26+
{
27+
28+
public function __construct(
29+
private NodeScopeResolver $nodeScopeResolver,
30+
)
31+
{
32+
}
33+
34+
public function supports(Expr $expr): bool
35+
{
36+
return $expr instanceof ArrayDimFetch;
37+
}
38+
39+
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
40+
{
41+
$hasYield = false;
42+
$throwPoints = [];
43+
$impurePoints = [];
44+
$isAlwaysTerminating = false;
45+
if ($expr->dim !== null) {
46+
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->dim, $scope, $storage, $nodeCallback, $context->enterDeep());
47+
$hasYield = $result->hasYield();
48+
$throwPoints = $result->getThrowPoints();
49+
$impurePoints = $result->getImpurePoints();
50+
$isAlwaysTerminating = $result->isAlwaysTerminating();
51+
$scope = $result->getScope();
52+
}
53+
54+
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep());
55+
$hasYield = $hasYield || $result->hasYield();
56+
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
57+
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
58+
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
59+
$scope = $result->getScope();
60+
61+
$varType = $scope->getType($expr->var);
62+
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
63+
$throwPoints = array_merge($throwPoints, $this->nodeScopeResolver->processExprNode(
64+
$stmt,
65+
new MethodCall($expr->var, 'offsetGet'),
66+
$scope,
67+
$storage,
68+
new NoopNodeCallback(),
69+
$context,
70+
)->getThrowPoints());
71+
}
72+
73+
return new ExpressionResult(
74+
$scope,
75+
hasYield: $hasYield,
76+
isAlwaysTerminating: $isAlwaysTerminating,
77+
throwPoints: $throwPoints,
78+
impurePoints: $impurePoints,
79+
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
80+
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
81+
);
82+
}
83+
84+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\ExprHandler;
4+
5+
use PhpParser\Node\Expr;
6+
use PhpParser\Node\Expr\Array_;
7+
use PhpParser\Node\Stmt;
8+
use PHPStan\Analyser\ExpressionContext;
9+
use PHPStan\Analyser\ExpressionResult;
10+
use PHPStan\Analyser\ExpressionResultStorage;
11+
use PHPStan\Analyser\ExprHandler;
12+
use PHPStan\Analyser\MutatingScope;
13+
use PHPStan\Analyser\NodeScopeResolver;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\Node\LiteralArrayItem;
16+
use PHPStan\Node\LiteralArrayNode;
17+
use function array_merge;
18+
19+
/**
20+
* @implements ExprHandler<Array_>
21+
*/
22+
#[AutowiredService]
23+
final class ArrayHandler implements ExprHandler
24+
{
25+
26+
public function __construct(
27+
private NodeScopeResolver $nodeScopeResolver,
28+
)
29+
{
30+
}
31+
32+
public function supports(Expr $expr): bool
33+
{
34+
return $expr instanceof Array_;
35+
}
36+
37+
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
38+
{
39+
$itemNodes = [];
40+
$hasYield = false;
41+
$throwPoints = [];
42+
$impurePoints = [];
43+
$isAlwaysTerminating = false;
44+
foreach ($expr->items as $arrayItem) {
45+
$itemNodes[] = new LiteralArrayItem($scope, $arrayItem);
46+
$this->nodeScopeResolver->callNodeCallback($nodeCallback, $arrayItem, $scope, $storage);
47+
if ($arrayItem->key !== null) {
48+
$keyResult = $this->nodeScopeResolver->processExprNode($stmt, $arrayItem->key, $scope, $storage, $nodeCallback, $context->enterDeep());
49+
$hasYield = $hasYield || $keyResult->hasYield();
50+
$throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints());
51+
$impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints());
52+
$isAlwaysTerminating = $isAlwaysTerminating || $keyResult->isAlwaysTerminating();
53+
$scope = $keyResult->getScope();
54+
}
55+
56+
$valueResult = $this->nodeScopeResolver->processExprNode($stmt, $arrayItem->value, $scope, $storage, $nodeCallback, $context->enterDeep());
57+
$hasYield = $hasYield || $valueResult->hasYield();
58+
$throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints());
59+
$impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints());
60+
$isAlwaysTerminating = $isAlwaysTerminating || $valueResult->isAlwaysTerminating();
61+
$scope = $valueResult->getScope();
62+
}
63+
$this->nodeScopeResolver->callNodeCallback($nodeCallback, new LiteralArrayNode($expr, $itemNodes), $scope, $storage);
64+
65+
return new ExpressionResult(
66+
$scope,
67+
hasYield: $hasYield,
68+
isAlwaysTerminating: $isAlwaysTerminating,
69+
throwPoints: $throwPoints,
70+
impurePoints: $impurePoints,
71+
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
72+
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
73+
);
74+
}
75+
76+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\ExprHandler;
4+
5+
use PhpParser\Node\Expr;
6+
use PhpParser\Node\Expr\ArrayDimFetch;
7+
use PhpParser\Node\Expr\Assign;
8+
use PhpParser\Node\Expr\AssignRef;
9+
use PhpParser\Node\Expr\PropertyFetch;
10+
use PhpParser\Node\Expr\StaticPropertyFetch;
11+
use PhpParser\Node\Expr\Variable;
12+
use PhpParser\Node\Stmt;
13+
use PHPStan\Analyser\ExpressionContext;
14+
use PHPStan\Analyser\ExpressionResult;
15+
use PHPStan\Analyser\ExpressionResultStorage;
16+
use PHPStan\Analyser\ExprHandler;
17+
use PHPStan\Analyser\ImpurePoint;
18+
use PHPStan\Analyser\MutatingScope;
19+
use PHPStan\Analyser\NodeScopeResolver;
20+
use PHPStan\DependencyInjection\AutowiredService;
21+
use function array_merge;
22+
use function count;
23+
use function is_string;
24+
25+
/**
26+
* @implements ExprHandler<Assign|AssignRef>
27+
*/
28+
#[AutowiredService]
29+
final class AssignHandler implements ExprHandler
30+
{
31+
32+
public function __construct(
33+
private NodeScopeResolver $nodeScopeResolver,
34+
)
35+
{
36+
}
37+
38+
public function supports(Expr $expr): bool
39+
{
40+
return $expr instanceof Assign || $expr instanceof AssignRef;
41+
}
42+
43+
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
44+
{
45+
$nodeScopeResolver = $this->nodeScopeResolver;
46+
$result = $nodeScopeResolver->processAssignVar(
47+
$scope,
48+
$storage,
49+
$stmt,
50+
$expr->var,
51+
$expr->expr,
52+
$nodeCallback,
53+
$context,
54+
static function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $storage, $nodeScopeResolver): ExpressionResult {
55+
$impurePoints = [];
56+
if ($expr instanceof AssignRef) {
57+
$referencedExpr = $expr->expr;
58+
while ($referencedExpr instanceof ArrayDimFetch) {
59+
$referencedExpr = $referencedExpr->var;
60+
}
61+
62+
if ($referencedExpr instanceof PropertyFetch || $referencedExpr instanceof StaticPropertyFetch) {
63+
$impurePoints[] = new ImpurePoint(
64+
$scope,
65+
$expr,
66+
'propertyAssignByRef',
67+
'property assignment by reference',
68+
false,
69+
);
70+
}
71+
72+
$scope = $scope->enterExpressionAssign($expr->expr);
73+
}
74+
75+
if ($expr->var instanceof Variable && is_string($expr->var->name)) {
76+
$context = $context->enterRightSideAssign(
77+
$expr->var->name,
78+
$expr->expr,
79+
);
80+
}
81+
82+
$nodeScopeResolver->storeBeforeScope($storage, $expr, $scope);
83+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
84+
$hasYield = $result->hasYield();
85+
$throwPoints = $result->getThrowPoints();
86+
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
87+
$isAlwaysTerminating = $result->isAlwaysTerminating();
88+
$scope = $result->getScope();
89+
90+
if ($expr instanceof AssignRef) {
91+
$scope = $scope->exitExpressionAssign($expr->expr);
92+
}
93+
94+
return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
95+
},
96+
true,
97+
);
98+
$scope = $result->getScope();
99+
$vars = $nodeScopeResolver->getAssignedVariables($expr->var);
100+
if (count($vars) > 0) {
101+
$varChangedScope = false;
102+
$scope = $nodeScopeResolver->processVarAnnotation($scope, $vars, $stmt, $varChangedScope);
103+
if (!$varChangedScope) {
104+
$scope = $nodeScopeResolver->processStmtVarAnnotation($scope, $storage, $stmt, null, $nodeCallback);
105+
}
106+
}
107+
108+
return new ExpressionResult(
109+
$scope,
110+
hasYield: $result->hasYield(),
111+
isAlwaysTerminating: $result->isAlwaysTerminating(),
112+
throwPoints: $result->getThrowPoints(),
113+
impurePoints: $result->getImpurePoints(),
114+
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
115+
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
116+
);
117+
}
118+
119+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\ExprHandler;
4+
5+
use DivisionByZeroError;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Expr\AssignOp;
8+
use PhpParser\Node\Expr\BinaryOp;
9+
use PhpParser\Node\Expr\ConstFetch;
10+
use PhpParser\Node\Name;
11+
use PhpParser\Node\Stmt;
12+
use PHPStan\Analyser\ExpressionContext;
13+
use PHPStan\Analyser\ExpressionResult;
14+
use PHPStan\Analyser\ExpressionResultStorage;
15+
use PHPStan\Analyser\ExprHandler;
16+
use PHPStan\Analyser\InternalThrowPoint;
17+
use PHPStan\Analyser\MutatingScope;
18+
use PHPStan\Analyser\NodeScopeResolver;
19+
use PHPStan\DependencyInjection\AutowiredService;
20+
use PHPStan\Type\Constant\ConstantIntegerType;
21+
use PHPStan\Type\ObjectType;
22+
23+
/**
24+
* @implements ExprHandler<AssignOp>
25+
*/
26+
#[AutowiredService]
27+
final class AssignOpHandler implements ExprHandler
28+
{
29+
30+
public function __construct(
31+
private NodeScopeResolver $nodeScopeResolver,
32+
)
33+
{
34+
}
35+
36+
public function supports(Expr $expr): bool
37+
{
38+
return $expr instanceof AssignOp;
39+
}
40+
41+
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
42+
{
43+
$nodeScopeResolver = $this->nodeScopeResolver;
44+
$result = $nodeScopeResolver->processAssignVar(
45+
$scope,
46+
$storage,
47+
$stmt,
48+
$expr->var,
49+
$expr,
50+
$nodeCallback,
51+
$context,
52+
static function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $storage, $nodeScopeResolver): ExpressionResult {
53+
$originalScope = $scope;
54+
if ($expr instanceof Expr\AssignOp\Coalesce) {
55+
$scope = $scope->filterByFalseyValue(
56+
new BinaryOp\NotIdentical($expr->var, new ConstFetch(new Name('null'))),
57+
);
58+
}
59+
60+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
61+
if ($expr instanceof Expr\AssignOp\Coalesce) {
62+
$nodeScopeResolver->storeBeforeScope($storage, $expr, $originalScope);
63+
return new ExpressionResult(
64+
$result->getScope()->mergeWith($originalScope),
65+
$result->hasYield(),
66+
$result->isAlwaysTerminating(),
67+
$result->getThrowPoints(),
68+
$result->getImpurePoints(),
69+
);
70+
}
71+
72+
return $result;
73+
},
74+
$expr instanceof Expr\AssignOp\Coalesce,
75+
);
76+
if (!$expr instanceof Expr\AssignOp\Coalesce) {
77+
$nodeScopeResolver->storeBeforeScope($storage, $expr, $scope);
78+
}
79+
$scope = $result->getScope();
80+
$throwPoints = $result->getThrowPoints();
81+
if (
82+
($expr instanceof Expr\AssignOp\Div || $expr instanceof Expr\AssignOp\Mod) &&
83+
!$scope->getType($expr->expr)->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no()
84+
) {
85+
$throwPoints[] = InternalThrowPoint::createExplicit($scope, new ObjectType(DivisionByZeroError::class), $expr, false);
86+
}
87+
88+
return new ExpressionResult(
89+
$scope,
90+
hasYield: $result->hasYield(),
91+
isAlwaysTerminating: $result->isAlwaysTerminating(),
92+
throwPoints: $throwPoints,
93+
impurePoints: $result->getImpurePoints(),
94+
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
95+
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
96+
);
97+
}
98+
99+
}

0 commit comments

Comments
 (0)