forked from symplify/phpstan-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForbiddenComplexForeachIfExprRule.php
157 lines (133 loc) · 4.44 KB
/
ForbiddenComplexForeachIfExprRule.php
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
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules\Complexity;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeFinder;
use PHPStan\Analyser\Scope;
use PHPStan\TrinaryLogic;
use PHPStan\Type\BooleanType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Symplify\Astral\Naming\SimpleNameResolver;
use Symplify\PHPStanRules\Rules\AbstractSymplifyRule;
use Symplify\PHPStanRules\TypeAnalyzer\ObjectTypeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\PHPStanRules\Tests\Rules\Complexity\ForbiddenComplexForeachIfExprRule\ForbiddenComplexForeachIfExprRuleTest
*/
final class ForbiddenComplexForeachIfExprRule extends AbstractSymplifyRule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'foreach(...), while(), for() or if(...) cannot contains a complex expression. Extract it to a new variable assign on line before';
/**
* @var array<class-string<Expr>>
*/
private const CALL_CLASS_TYPES = [MethodCall::class, StaticCall::class];
/**
* @var array<class-string>
*/
private const ALLOWED_CLASS_TYPES = [Strings::class, TrinaryLogic::class];
public function __construct(
private NodeFinder $nodeFinder,
private SimpleNameResolver $simpleNameResolver,
private ObjectTypeAnalyzer $objectTypeAnalyzer,
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Foreach_::class, If_::class, ElseIf_::class, For_::class, Do_::class, While_::class];
}
/**
* @param Foreach_|If_|ElseIf_ $node
* @return string[]
*/
public function process(Node $node, Scope $scope): array
{
$expr = $node instanceof Foreach_ ? $node->expr : $node->cond;
$assigns = $this->nodeFinder->findInstanceOf($expr, Assign::class);
if ($assigns !== []) {
return [self::ERROR_MESSAGE];
}
foreach (self::CALL_CLASS_TYPES as $expressionClassType) {
/** @var MethodCall[]|StaticCall[] $calls */
$calls = $this->nodeFinder->findInstanceOf($expr, $expressionClassType);
foreach ($calls as $call) {
if ($this->shouldSkipCall($call, $scope)) {
continue;
}
return [self::ERROR_MESSAGE];
}
}
return [];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
foreach ($this->getData($arg) as $key => $item) {
// ...
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$data = $this->getData($arg);
foreach ($arg as $key => $item) {
// ...
}
CODE_SAMPLE
),
]);
}
private function shouldSkipCall(MethodCall | StaticCall $expr, Scope $scope): bool
{
if ($expr->args === []) {
return true;
}
if ($this->isAllowedCallerType($scope, $expr)) {
return true;
}
$callType = $scope->getType($expr);
if ($this->objectTypeAnalyzer->isObjectOrUnionOfObjectTypes($callType, self::ALLOWED_CLASS_TYPES)) {
return true;
}
return $callType instanceof BooleanType;
}
private function resolveCalleeType(Scope $scope, StaticCall | MethodCall $node): Type
{
if ($node instanceof StaticCall) {
$className = $this->simpleNameResolver->getName($node->class);
if ($className === null) {
return new MixedType();
}
return new ObjectType($className);
}
return $scope->getType($node->var);
}
private function isAllowedCallerType(Scope $scope, StaticCall | MethodCall $node): bool
{
if (! $node instanceof StaticCall) {
return false;
}
$callerType = $this->resolveCalleeType($scope, $node);
return $this->objectTypeAnalyzer->isObjectOrUnionOfObjectTypes($callerType, self::ALLOWED_CLASS_TYPES);
}
}