Conditions | 14 |
Paths | 34 |
Total Lines | 93 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Tests | 50 |
CRAP Score | 14 |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
232 | 21 | public function __invoke(QuerySequence $querySequence) { |
|
233 | static $availableModifiers = [ |
||
234 | T_STATIC, |
||
235 | T_PRIVATE, |
||
236 | T_PUBLIC, |
||
237 | T_ABSTRACT, |
||
238 | T_FINAL, |
||
239 | 21 | ]; |
|
240 | |||
241 | |||
242 | # detect function |
||
243 | 21 | $functionKeyword = $querySequence->strict('function'); |
|
244 | 21 | $querySequence->strict(T_WHITESPACE); |
|
245 | 21 | $querySequence->process($this->nameQuery); |
|
246 | 21 | $querySequence->section('(', ')'); |
|
247 | 21 | $querySequence->possible(T_WHITESPACE); |
|
248 | 21 | $body = $querySequence->section('{', '}'); |
|
249 | |||
250 | 21 | if (!$querySequence->isValid()) { |
|
251 | 21 | return null; |
|
252 | } |
||
253 | |||
254 | 21 | $collection = $querySequence->getCollection(); |
|
255 | 21 | $start = $collection->extractByTokens($collection->getFirst(), $functionKeyword); |
|
1 ignored issue
–
show
|
|||
256 | 21 | $start->slice(0, -1); // remove last function keyword |
|
257 | |||
258 | # start reverse search |
||
259 | 21 | $items = array_reverse($start->getItems()); |
|
260 | 21 | $startFrom = null; |
|
261 | |||
262 | 21 | $docComment = new Token(); |
|
263 | |||
264 | 21 | $modifiers = []; |
|
265 | |||
266 | |||
267 | /** @var Token[] $items */ |
||
268 | 21 | foreach ($items as $item) { |
|
269 | |||
270 | 21 | if ($item->getType() === T_WHITESPACE) { |
|
271 | 18 | $startFrom = $item; |
|
272 | 18 | continue; |
|
273 | } |
||
274 | |||
275 | 21 | if ($item->getType() === T_DOC_COMMENT and $docComment->isValid() === false) { |
|
276 | # Detect only first doc comment |
||
277 | 6 | $startFrom = $item; |
|
278 | 6 | $docComment = $item; |
|
279 | 6 | continue; |
|
280 | } |
||
281 | |||
282 | |||
283 | 21 | if (in_array($item->getType(), $availableModifiers)) { |
|
284 | 15 | $startFrom = $item; |
|
285 | 15 | $modifiers[] = $item->getValue(); |
|
286 | 15 | continue; |
|
287 | } |
||
288 | |||
289 | 21 | break; |
|
290 | 21 | } |
|
291 | |||
292 | 21 | if ($this->isValidModifiers($modifiers) === false) { |
|
293 | 3 | return null; |
|
294 | } |
||
295 | |||
296 | 21 | if ($this->isValidBody($body) === false) { |
|
297 | 3 | return null; |
|
298 | } |
||
299 | |||
300 | 21 | if ($this->isValidDocComment($docComment) === false) { |
|
301 | 3 | return null; |
|
302 | } |
||
303 | |||
304 | 21 | if (is_null($startFrom)) { |
|
305 | 3 | $startFrom = $functionKeyword; |
|
306 | 3 | } |
|
307 | |||
308 | |||
309 | 21 | if ($this->outputType === self::OUTPUT_FULL) { |
|
310 | # all conditions are ok, so extract full function |
||
311 | 3 | $fullFunction = $collection->extractByTokens($startFrom, $body->getLast()); |
|
312 | 3 | if ($fullFunction->getFirst()->getType() == T_WHITESPACE) { |
|
313 | 3 | $fullFunction->slice(1); |
|
314 | 3 | } |
|
315 | 3 | return $fullFunction; |
|
316 | |||
317 | 21 | } elseif ($this->outputType == self::OUTPUT_DOC_COMMENT) { |
|
318 | 3 | return new Collection([$docComment]); |
|
319 | } |
||
320 | |||
321 | # body by default |
||
322 | 21 | $body->slice(0, -1); |
|
323 | 21 | return $body; |
|
324 | } |
||
325 | |||
343 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.