1 | <?php |
||
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 |
||
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 |
||
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 |
||
84 | |||
85 | /** |
||
86 | * Type-safe getter for the file. |
||
87 | * |
||
88 | * @return File |
||
89 | */ |
||
90 | protected function getFile(): File |
||
94 | |||
95 | /** |
||
96 | * Type-safe getter for the stack position. |
||
97 | * |
||
98 | * @return int |
||
99 | */ |
||
100 | protected function getStackPos(): int |
||
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) |
||
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 |
||
152 | |||
153 | /** |
||
154 | * Is there something to destroy after processing the token? |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | protected function tearDown(): void |
||
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: