forked from symplify/phpstan-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForbiddenArrayWithStringKeysRule.php
184 lines (158 loc) · 5.14 KB
/
ForbiddenArrayWithStringKeysRule.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ArrayType;
use Symplify\Astral\Naming\SimpleNameResolver;
use Symplify\Astral\NodeFinder\SimpleNodeFinder;
use Symplify\PackageBuilder\ValueObject\MethodName;
use Symplify\PHPStanRules\Naming\AssignToVariableChecker;
use Symplify\PHPStanRules\NodeAnalyzer\ArrayAnalyzer;
use Symplify\PHPStanRules\ParentGuard\ParentElementResolver\ParentMethodReturnTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\PHPStanRules\Tests\Rules\ForbiddenArrayWithStringKeysRule\ForbiddenArrayWithStringKeysRuleTest
*/
final class ForbiddenArrayWithStringKeysRule extends AbstractSymplifyRule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Array with keys is not allowed. Use value object to pass data instead';
/**
* @var string
* @see https://regex101.com/r/ddj4mB/2
*/
private const TEST_FILE_REGEX = '#(Test|TestCase)\.php$#';
/**
* @see https://regex101.com/r/TOKYyM/1
* @var string
*/
private const ARRAY_CONFIGURATION_NAMES_REGEX = '#(yaml|json|neon)#i';
public function __construct(
private ParentMethodReturnTypeResolver $parentMethodReturnTypeResolver,
private SimpleNameResolver $simpleNameResolver,
private SimpleNodeFinder $simpleNodeFinder,
private ArrayAnalyzer $arrayAnalyzer,
private AssignToVariableChecker $assignToVariableChecker
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Array_::class];
}
/**
* @param Array_ $node
* @return string[]
*/
public function process(Node $node, Scope $scope): array
{
if ($this->shouldSkipClass($scope, $node)) {
return [];
}
if ($this->shouldSkipArray($node, $scope)) {
return [];
}
if (! $this->arrayAnalyzer->isArrayWithStringKey($node)) {
return [];
}
// is return array required by parent
$parentMethodReturnType = $this->parentMethodReturnTypeResolver->resolve($scope);
if ($parentMethodReturnType instanceof ArrayType) {
return [];
}
return [self::ERROR_MESSAGE];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
return [
'name' => 'John',
'surname' => 'Dope',
];
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
return new Person('John', 'Dope');
}
}
CODE_SAMPLE
),
]);
}
private function shouldSkipArray(Array_ $array, Scope $scope): bool
{
// skip part of attribute
$parentAttribute = $this->simpleNodeFinder->findFirstParentByType($array, Attribute::class);
if ($parentAttribute instanceof Attribute) {
return true;
}
if (Strings::match($scope->getFile(), self::TEST_FILE_REGEX)) {
return true;
}
// skip examples in Rector::getDefinition() method
if (in_array($scope->getFunctionName(), ['getDefinition', MethodName::CONSTRUCTOR], true)) {
return true;
}
if ($this->assignToVariableChecker->isAssignToVariableRegex($array, self::ARRAY_CONFIGURATION_NAMES_REGEX)) {
return true;
}
return $this->isPartOfClassConstOrNew($array);
}
private function isPartOfClassConstOrNew(Array_ $array): bool
{
return (bool) $this->simpleNodeFinder->findFirstParentByTypes($array, [
ClassConst::class,
New_::class,
MethodCall::class,
StaticCall::class,
FuncCall::class,
]);
}
private function shouldSkipClass(Scope $scope, Array_ $array): bool
{
$filePath = $scope->getFile();
// php-scoper config, it return magic array by design
if (\str_contains($filePath, 'scoper')) {
return true;
}
// skip Symfony bundles.php
if (\str_ends_with($filePath, 'bundles.php')) {
return true;
}
$classMethod = $this->simpleNodeFinder->findFirstParentByType($array, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return false;
}
/** @var string $classMethodName */
$classMethodName = $this->simpleNameResolver->getName($classMethod);
$match = Strings::match($classMethodName, self::ARRAY_CONFIGURATION_NAMES_REGEX);
return $match !== null;
}
}