/
MaxNestingLevelSniff.php
173 lines (141 loc) · 4.23 KB
/
MaxNestingLevelSniff.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
<?php declare(strict_types=1);
namespace ObjectCalisthenics\Sniffs\Metrics;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
final class MaxNestingLevelSniff implements Sniff
{
/**
* @var string
*/
private const ERROR_MESSAGE = 'Only %d indentation level%s per function/method. Found %s levels.';
/**
* @var int
*/
public $maxNestingLevel = 2;
/**
* @var int
*/
private $position;
/**
* @var int
*/
private $nestingLevel;
/**
* @var int
*/
private $currentPtr;
/**
* @var mixed[]
*/
private $ignoredScopeStack = [];
/**
* @var File
*/
private $file;
/**
* @return int[]|string[]
*/
public function register(): array
{
return [T_FUNCTION, T_CLOSURE];
}
/**
* @param int $position
*/
public function process(File $file, $position): void
{
$this->file = $file;
$this->position = $position;
$this->nestingLevel = 0;
$this->ignoredScopeStack = [];
$tokens = $file->getTokens();
$token = $tokens[$position];
// Ignore abstract methods.
if (! isset($token['scope_opener'])) {
return;
}
$this->iterateTokens($token['scope_opener'], $token['scope_closer'], $tokens);
$this->nestingLevel = $this->subtractFunctionNestingLevel($token);
$this->handleNestingLevel($this->nestingLevel);
}
/**
* @param mixed[] $tokens
*/
private function iterateTokens(int $start, int $end, array $tokens): void
{
// Find the maximum nesting level of any token in the function.
for ($this->currentPtr = ($start + 1); $this->currentPtr < $end; ++$this->currentPtr) {
$nestedToken = $tokens[$this->currentPtr];
$this->handleToken($nestedToken);
}
}
/**
* @param mixed[] $token
*/
private function subtractFunctionNestingLevel(array $token): int
{
return $this->nestingLevel - $token['level'] - 1;
}
private function handleNestingLevel(int $nestingLevel): void
{
if ($nestingLevel > $this->maxNestingLevel) {
$levelPluralization = $this->maxNestingLevel > 1 ? 's' : '';
$error = sprintf(self::ERROR_MESSAGE, $this->maxNestingLevel, $levelPluralization, $nestingLevel);
$this->file->addError($error, $this->position, self::class);
}
}
/**
* @param mixed[] $nestedToken
*/
private function handleToken(array $nestedToken): void
{
$this->handleClosureToken($nestedToken);
$this->handleCaseToken($nestedToken);
$this->adjustNestingLevelToIgnoredScope();
// Calculate nesting level
$level = $nestedToken['level'] - count($this->ignoredScopeStack);
if ($this->nestingLevel < $level) {
$this->nestingLevel = $level;
}
}
/**
* @param mixed[] $nestedToken
*/
private function handleClosureToken(array $nestedToken): void
{
if ($nestedToken['code'] === T_CLOSURE) {
// Move index pointer in case we found a lambda function
// (another call process will deal with its check later).
$this->currentPtr = $nestedToken['scope_closer'];
return;
}
}
/**
* @param mixed[] $nestedToken
*/
private function handleCaseToken(array $nestedToken): void
{
if (in_array($nestedToken['code'], [T_CASE, T_DEFAULT], true)) {
array_push($this->ignoredScopeStack, $nestedToken);
return;
}
}
private function adjustNestingLevelToIgnoredScope(): void
{
// Iterated through ignored scope stack to find out if
// anything can be popped out and adjust nesting level.
foreach ($this->ignoredScopeStack as $key => $ignoredScope) {
$this->unsetScopeIfNotCurrent($key, $ignoredScope);
}
}
/**
* @param mixed[] $ignoredScope
*/
private function unsetScopeIfNotCurrent(int $key, array $ignoredScope): void
{
if ($ignoredScope['scope_closer'] !== $this->currentPtr) {
return;
}
unset($this->ignoredScopeStack[$key]);
}
}