1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs; |
6
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\CodeError; |
8
|
|
|
use BestIt\CodeSniffer\CodeWarning; |
9
|
|
|
use BestIt\CodeSniffer\Helper\ExceptionHelper; |
10
|
|
|
use PHP_CodeSniffer\Files\File; |
11
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractSniff |
15
|
|
|
* |
16
|
|
|
* @author Nick Lubisch <[email protected]> |
17
|
|
|
* @package BestIt\Sniffs |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractSniff implements Sniff |
20
|
|
|
{ |
21
|
|
|
use SuppressingTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The used file. |
25
|
|
|
* |
26
|
|
|
* @var File|void |
27
|
|
|
*/ |
28
|
|
|
protected $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Position of the listened token. |
32
|
|
|
* |
33
|
|
|
* @var int|void |
34
|
|
|
*/ |
35
|
|
|
protected $stackPos; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The used token. |
39
|
|
|
* |
40
|
|
|
* @var array|void |
41
|
|
|
*/ |
42
|
|
|
protected $token; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* All tokens of the class. |
46
|
|
|
* |
47
|
|
|
* @var array|void The tokens of the class. |
48
|
|
|
*/ |
49
|
|
|
protected $tokens; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Adds the pointer to all token data arrays. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
protected function addPointerToTokens(): void |
57
|
|
|
{ |
58
|
|
|
foreach ($this->tokens as $tokenPtr => &$token) { |
|
|
|
|
59
|
|
|
$token['pointer'] = $tokenPtr; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns true if the requirements for this sniff are met. |
65
|
|
|
* |
66
|
|
|
* @return bool Are the requirements met and the sniff should proceed? |
67
|
|
|
*/ |
68
|
|
|
protected function areRequirementsMet(): bool |
69
|
|
|
{ |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Default method for fixing exceptions. |
75
|
|
|
* |
76
|
|
|
* @param CodeWarning $exception |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
protected function fixDefaultProblem(CodeWarning $exception): void |
81
|
|
|
{ |
82
|
|
|
// Satisfy PHP MD |
83
|
|
|
unset($exception); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns an exception handler for the sniffed file. |
88
|
|
|
* |
89
|
|
|
* @return ExceptionHelper Returns the exception helper. |
90
|
|
|
*/ |
91
|
|
|
protected function getExceptionHandler(): ExceptionHelper |
92
|
|
|
{ |
93
|
|
|
return new ExceptionHelper($this->getFile()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Type-safe getter for the file. |
98
|
|
|
* |
99
|
|
|
* @return File |
100
|
|
|
*/ |
101
|
|
|
protected function getFile(): File |
102
|
|
|
{ |
103
|
|
|
return $this->file; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Type-safe getter for the stack position. |
108
|
|
|
* |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
protected function getStackPos(): int |
112
|
|
|
{ |
113
|
|
|
return $this->stackPos; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Called when one of the token types that this sniff is listening for is found. |
118
|
|
|
* |
119
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint |
120
|
|
|
* |
121
|
|
|
* @param File $file The PHP_CodeSniffer file where the token was found. |
122
|
|
|
* @param int $stackPos The position in the PHP_CodeSniffer file's token stack where the token was found. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function process(File $file, $stackPos): void |
127
|
|
|
{ |
128
|
|
|
$this->file = $file; |
129
|
|
|
$this->stackPos = $stackPos; |
130
|
|
|
$this->tokens = $this->getFile()->getTokens(); |
131
|
|
|
$this->token = $this->tokens[$stackPos]; |
132
|
|
|
|
133
|
|
|
$this->setUp(); |
134
|
|
|
|
135
|
|
|
if ($this->areRequirementsMet()) { |
136
|
|
|
try { |
137
|
|
|
$this->processToken(); |
138
|
|
|
} catch (CodeWarning | CodeError $exception) { |
139
|
|
|
$withFix = $this->getExceptionHandler()->handleException($exception); |
140
|
|
|
|
141
|
|
|
if ($withFix) { |
142
|
|
|
$this->fixDefaultProblem($exception); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->tearDown(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Processes the token. |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
abstract protected function processToken(): void; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Do you want to setup things before processing the token? |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
protected function setUp(): void |
163
|
|
|
{ |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Is there something to destroy after processing the token? |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
protected function tearDown(): void |
172
|
|
|
{ |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.