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\File; |
10
|
|
|
use BestIt\CodeSniffer\Helper\ExceptionHelper; |
11
|
|
|
use PHP_CodeSniffer\Files\File as BaseFile; |
12
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AbstractSniff |
16
|
|
|
* |
17
|
|
|
* @author Nick Lubisch <[email protected]> |
18
|
|
|
* @package BestIt\Sniffs |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractSniff implements Sniff |
21
|
|
|
{ |
22
|
|
|
use SuppressingTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The used file. |
26
|
|
|
* |
27
|
|
|
* @var File|void |
28
|
|
|
*/ |
29
|
|
|
protected $file; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Position of the listened token. |
33
|
|
|
* |
34
|
|
|
* @var int|void |
35
|
|
|
*/ |
36
|
|
|
protected $stackPos; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The used token. |
40
|
|
|
* |
41
|
|
|
* @var array|void |
42
|
|
|
*/ |
43
|
|
|
protected $token; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* All tokens of the class. |
47
|
|
|
* |
48
|
|
|
* @var array|void The tokens of the class. |
49
|
|
|
*/ |
50
|
|
|
protected $tokens; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns true if the requirements for this sniff are met. |
54
|
|
|
* |
55
|
|
|
* @return bool Are the requirements met and the sniff should proceed? |
56
|
|
|
*/ |
57
|
|
|
protected function areRequirementsMet(): bool |
58
|
|
|
{ |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Default method for fixing exceptions. |
64
|
|
|
* |
65
|
|
|
* @param CodeWarning $exception |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
protected function fixDefaultProblem(CodeWarning $exception): void |
70
|
|
|
{ |
71
|
|
|
// Satisfy PHP MD |
72
|
|
|
unset($exception); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns an exception handler for the sniffed file. |
77
|
|
|
* |
78
|
|
|
* @return ExceptionHelper Returns the exception helper. |
79
|
|
|
*/ |
80
|
|
|
protected function getExceptionHandler(): ExceptionHelper |
81
|
|
|
{ |
82
|
|
|
return new ExceptionHelper($this->getFile()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Type-safe getter for the file. |
87
|
|
|
* |
88
|
|
|
* @return File |
89
|
|
|
*/ |
90
|
|
|
protected function getFile(): File |
91
|
|
|
{ |
92
|
|
|
return $this->file; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Type-safe getter for the stack position. |
97
|
|
|
* |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
protected function getStackPos(): int |
101
|
|
|
{ |
102
|
|
|
return $this->stackPos; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Called when one of the token types that this sniff is listening for is found. |
107
|
|
|
* |
108
|
|
|
* @param BaseFile $phpcsFile The PHP_CodeSniffer file where the token was found. |
109
|
|
|
* @param int $stackPos The position in the PHP_CodeSniffer file's token stack where the token was found. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function process(BaseFile $phpcsFile, $stackPos) |
114
|
|
|
{ |
115
|
|
|
$this->file = new File($phpcsFile); |
116
|
|
|
$this->stackPos = $stackPos; |
117
|
|
|
$this->tokens = $this->getFile()->getTokens(); |
118
|
|
|
$this->token = $this->tokens[$stackPos]; |
119
|
|
|
|
120
|
|
|
$this->setUp(); |
121
|
|
|
|
122
|
|
|
if ($this->areRequirementsMet()) { |
123
|
|
|
try { |
124
|
|
|
$this->processToken(); |
125
|
|
|
} catch (CodeWarning | CodeError $exception) { |
126
|
|
|
$withFix = $this->getExceptionHandler()->handleException($exception); |
127
|
|
|
|
128
|
|
|
if ($withFix) { |
129
|
|
|
$this->fixDefaultProblem($exception); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->tearDown(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Processes the token. |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
abstract protected function processToken(): void; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Do you want to setup things before processing the token? |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
protected function setUp(): void |
150
|
|
|
{ |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Is there something to destroy after processing the token? |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
protected function tearDown(): void |
159
|
|
|
{ |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: