Conditions | 32 |
Paths | 21 |
Total Lines | 163 |
Lines | 18 |
Ratio | 11.04 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
129 | public function process(File $phpcsFile, $stackPtr) |
||
130 | { |
||
131 | if ($this->supportsAbove('7.1') === false) { |
||
132 | return; |
||
133 | } |
||
134 | |||
135 | $tokens = $phpcsFile->getTokens(); |
||
136 | |||
137 | switch ($tokens[$stackPtr]['code']) { |
||
138 | case \T_FUNCTION: |
||
139 | $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
||
140 | $this->isThisUsedOutsideObjectContext($phpcsFile, $stackPtr); |
||
141 | break; |
||
142 | |||
143 | case \T_CLOSURE: |
||
144 | $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
||
145 | break; |
||
146 | |||
147 | case \T_GLOBAL: |
||
148 | /* |
||
149 | * $this can no longer be imported using the `global` keyword. |
||
150 | * This worked in PHP 7.0, though in PHP 5.x, it would throw a |
||
151 | * fatal "Cannot re-assign $this" error. |
||
152 | */ |
||
153 | $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1)); |
||
154 | if ($endOfStatement === false) { |
||
155 | // No semi-colon - live coding. |
||
156 | return; |
||
157 | } |
||
158 | |||
159 | for ($i = ($stackPtr + 1); $i < $endOfStatement; $i++) { |
||
160 | View Code Duplication | if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
|
|||
161 | continue; |
||
162 | } |
||
163 | |||
164 | $phpcsFile->addError( |
||
165 | '"$this" can no longer be used with the "global" keyword since PHP 7.1.', |
||
166 | $i, |
||
167 | 'Global' |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | break; |
||
172 | |||
173 | case \T_CATCH: |
||
174 | /* |
||
175 | * $this can no longer be used as a catch variable. |
||
176 | */ |
||
177 | View Code Duplication | if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
178 | return; |
||
179 | } |
||
180 | |||
181 | $varPtr = $phpcsFile->findNext( |
||
182 | \T_VARIABLE, |
||
183 | ($tokens[$stackPtr]['parenthesis_opener'] + 1), |
||
184 | $tokens[$stackPtr]['parenthesis_closer'] |
||
185 | ); |
||
186 | |||
187 | if ($varPtr === false || $tokens[$varPtr]['content'] !== '$this') { |
||
188 | return; |
||
189 | } |
||
190 | |||
191 | $phpcsFile->addError( |
||
192 | '"$this" can no longer be used as a catch variable since PHP 7.1.', |
||
193 | $varPtr, |
||
194 | 'Catch' |
||
195 | ); |
||
196 | |||
197 | break; |
||
198 | |||
199 | case \T_FOREACH: |
||
200 | /* |
||
201 | * $this can no longer be used as a foreach *value* variable. |
||
202 | * This worked in PHP 7.0, though in PHP 5.x, it would throw a |
||
203 | * fatal "Cannot re-assign $this" error. |
||
204 | */ |
||
205 | View Code Duplication | if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
206 | return; |
||
207 | } |
||
208 | |||
209 | $stopPtr = $phpcsFile->findPrevious( |
||
210 | array(\T_AS, \T_DOUBLE_ARROW), |
||
211 | ($tokens[$stackPtr]['parenthesis_closer'] - 1), |
||
212 | $tokens[$stackPtr]['parenthesis_opener'] |
||
213 | ); |
||
214 | if ($stopPtr === false) { |
||
215 | return; |
||
216 | } |
||
217 | |||
218 | $valueVarPtr = $phpcsFile->findNext( |
||
219 | \T_VARIABLE, |
||
220 | ($stopPtr + 1), |
||
221 | $tokens[$stackPtr]['parenthesis_closer'] |
||
222 | ); |
||
223 | if ($valueVarPtr === false || $tokens[$valueVarPtr]['content'] !== '$this') { |
||
224 | return; |
||
225 | } |
||
226 | |||
227 | $afterThis = $phpcsFile->findNext( |
||
228 | Tokens::$emptyTokens, |
||
229 | ($valueVarPtr + 1), |
||
230 | $tokens[$stackPtr]['parenthesis_closer'], |
||
231 | true |
||
232 | ); |
||
233 | |||
234 | View Code Duplication | if ($afterThis !== false |
|
235 | && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
||
236 | || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON) |
||
237 | ) { |
||
238 | return; |
||
239 | } |
||
240 | |||
241 | $phpcsFile->addError( |
||
242 | '"$this" can no longer be used as value variable in a foreach control structure since PHP 7.1.', |
||
243 | $valueVarPtr, |
||
244 | 'ForeachValueVar' |
||
245 | ); |
||
246 | |||
247 | break; |
||
248 | |||
249 | case \T_UNSET: |
||
250 | /* |
||
251 | * $this can no longer be unset. |
||
252 | */ |
||
253 | $openParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
254 | if ($openParenthesis === false |
||
255 | || $tokens[$openParenthesis]['code'] !== \T_OPEN_PARENTHESIS |
||
256 | || isset($tokens[$openParenthesis]['parenthesis_closer']) === false |
||
257 | ) { |
||
258 | return; |
||
259 | } |
||
260 | |||
261 | for ($i = ($openParenthesis + 1); $i < $tokens[$openParenthesis]['parenthesis_closer']; $i++) { |
||
262 | View Code Duplication | if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
263 | continue; |
||
264 | } |
||
265 | |||
266 | $afterThis = $phpcsFile->findNext( |
||
267 | Tokens::$emptyTokens, |
||
268 | ($i + 1), |
||
269 | $tokens[$openParenthesis]['parenthesis_closer'], |
||
270 | true |
||
271 | ); |
||
272 | |||
273 | if ($afterThis !== false |
||
274 | && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
||
275 | || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON |
||
276 | || $tokens[$afterThis]['code'] === \T_OPEN_SQUARE_BRACKET) |
||
277 | ) { |
||
278 | $i = $afterThis; |
||
279 | continue; |
||
280 | } |
||
281 | |||
282 | $phpcsFile->addError( |
||
283 | '"$this" can no longer be unset since PHP 7.1.', |
||
284 | $i, |
||
285 | 'Unset' |
||
286 | ); |
||
287 | } |
||
288 | |||
289 | break; |
||
290 | } |
||
291 | } |
||
292 | |||
426 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.