-
Notifications
You must be signed in to change notification settings - Fork 57
/
OneObjectOperatorPerLineSniff.php
205 lines (167 loc) · 5.46 KB
/
OneObjectOperatorPerLineSniff.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php declare(strict_types=1);
namespace ObjectCalisthenics\Sniffs\CodeAnalysis;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
final class OneObjectOperatorPerLineSniff implements Sniff
{
/**
* @var string
*/
private const ERROR_MESSAGE = 'Only one object operator per line.';
/**
* @var string[]
*/
public $variablesHoldingAFluentInterface = [
'$queryBuilder',
'$containerBuilder',
];
/**
* @var string[]
*/
public $methodsStartingAFluentInterface = [
'createQueryBuilder',
];
/**
* @var string[]
*/
public $methodsEndingAFluentInterface = [
'execute', 'getQuery',
];
/**
* @var File
*/
private $file;
/**
* @var int
*/
private $position;
/**
* @var mixed[]
*/
private $callerTokens = [];
/**
* @var mixed[]
*/
private $tokens = [];
/**
* @var string
*/
private $variableName;
/**
* @return int[]
*/
public function register(): array
{
return [T_VARIABLE];
}
/**
* @param int $position
*/
public function process(File $file, $position): void
{
$this->file = $file;
$this->position = $position;
$this->tokens = $file->getTokens();
$this->callerTokens = [];
$pointer = $this->ignoreWhitespace($position + 1);
$this->variableName = $this->tokens[$this->position]['content'];
$token = $this->tokens[$position];
$isOwnCall = ($token['content'] === '$this');
$this->handleObjectOperators($pointer, $isOwnCall);
}
private function ignoreWhitespace(int $start): int
{
$pointer = $start;
while ($this->tokens[$pointer]['code'] === T_WHITESPACE) {
++$pointer;
}
return $pointer;
}
private function handleTwoObjectOperators(bool $isOwnCall): void
{
if ($this->callerTokens && ! $isOwnCall && ! $this->isInFluentInterfaceMode()) {
$this->file->addError(self::ERROR_MESSAGE, $this->position, self::class);
}
}
/**
* @param mixed[] $tmpToken
*/
private function handleExcludedFluentInterfaces(array $tmpToken, string $tmpTokenType, bool $isOwnCall): void
{
if ((count($this->callerTokens) - (int) $isOwnCall) === 0) {
return;
}
$memberTokenCount = count($this->callerTokens);
$memberToken = end($this->callerTokens);
$memberTokenType = $memberToken['type'];
if (($memberTokenType === 'property' && $tmpTokenType === 'property')
|| ($memberTokenType === 'method' && $tmpTokenType === 'property')
|| ($memberTokenType === 'method' && $tmpTokenType === 'method'
&& $memberTokenCount > 1 && $tmpToken['content'] !== $memberToken['token']['content']
&& ! $this->isInFluentInterfaceMode())
) {
$this->file->addError(self::ERROR_MESSAGE, $this->position, self::class);
}
}
private function isInFluentInterfaceMode(): bool
{
$lastEndPoint = $this->computeLastCallOfAnyFrom($this->methodsEndingAFluentInterface);
$lastStartPoint = $this->computeLastCallOfAnyFrom($this->methodsStartingAFluentInterface);
if (in_array($this->variableName, $this->variablesHoldingAFluentInterface, true)) {
$lastStartPoint = max($lastStartPoint, -1);
}
return $lastStartPoint > -2
&& $lastStartPoint > $lastEndPoint;
}
/**
* @param string[] $methods
*
* @return int The last position of the method calls within the callerTokens
* or -2 if none of the methods has been called
*/
private function computeLastCallOfAnyFrom(array $methods): int
{
$calls = array_filter($this->callerTokens, function (array $token) use ($methods) {
return in_array($token['token']['content'], $methods, true);
});
if (count($calls) > 0) {
return array_search(end($calls), $this->callerTokens, true);
}
return -2;
}
private function handleObjectOperators(int $pointer, bool $isOwnCall): void
{
while ($this->tokens[$pointer]['code'] === T_OBJECT_OPERATOR) {
$tmpToken = $this->tokens[++$pointer];
$pointer = $this->ignoreWhitespace($pointer + 1);
$tmpTokenType = $this->getTokenType($this->tokens[$pointer]);
// Look for second object operator token on same statement
$this->handleTwoObjectOperators($isOwnCall);
$this->handleExcludedFluentInterfaces($tmpToken, $tmpTokenType, $isOwnCall);
$this->callerTokens[] = [
'token' => $tmpToken,
'type' => $tmpTokenType,
];
$pointer = $this->movePointerToNextObject($pointer);
}
}
/**
* @param mixed[] $token
*/
private function getTokenType(array $token): string
{
if ($token['code'] === T_OPEN_PARENTHESIS) {
return 'method';
}
return 'property';
}
private function movePointerToNextObject(int $pointer): int
{
$token = $this->tokens[$pointer];
// Ignore "(" ... ")" in a method call by moving pointer after close parenthesis token
if ($token['code'] === T_OPEN_PARENTHESIS) {
$pointer = $token['parenthesis_closer'] + 1;
}
return $this->ignoreWhitespace($pointer);
}
}