| Conditions | 8 |
| Paths | 20 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 13 | public static function check($tokens, $absPath) |
||
| 14 | { |
||
| 15 | // we skip the very first tokens: '<?php ' |
||
| 16 | $i = 4; |
||
| 17 | // we skip the very end of the file. |
||
| 18 | $total = \count($tokens) - 3; |
||
| 19 | $calls = []; |
||
| 20 | $callsOrder = []; |
||
| 21 | $partialName = ''; |
||
| 22 | while ($i < $total) { |
||
| 23 | $index = FunctionCall::isGlobalCall('extractBlade', $tokens, $i); |
||
| 24 | |||
| 25 | if (! $index) { |
||
| 26 | $i++; |
||
| 27 | continue; |
||
| 28 | } |
||
| 29 | |||
| 30 | $params = FunctionCall::readParameters($tokens, $i); |
||
| 31 | |||
| 32 | $partialName = $params[0][0][1] ?? $partialName; |
||
| 33 | ! \in_array($partialName, $callsOrder) && $callsOrder[] = $partialName; |
||
| 34 | $calls[$partialName][] = ($params[0][0]) ?? ($tokens[$i - 1]); |
||
| 35 | |||
| 36 | $i++; |
||
| 37 | } |
||
| 38 | if (! $calls) { |
||
|
|
|||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | $file = file($absPath); |
||
| 43 | |||
| 44 | $callsOrder = array_reverse($callsOrder); |
||
| 45 | foreach ($callsOrder as $paramName) { |
||
| 46 | $call = $calls[$paramName]; |
||
| 47 | if (\count($call) < 2) { |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | $replacement = ['@include('.$call[0][1].')'."\n"]; |
||
| 51 | |||
| 52 | $start = $call[0][2] - (1); |
||
| 53 | $removedLinesNumber = ($call[1][2] - $call[0][2]) + 1; |
||
| 54 | $extracted = array_splice($file, $start, $removedLinesNumber, $replacement); |
||
| 55 | $partialPath = self::find(\trim($call[0][1], '\'\"')); |
||
| 56 | array_shift($extracted); |
||
| 57 | array_pop($extracted); |
||
| 58 | |||
| 59 | $partialPath = \str_replace(['/', '\\'], '/', $partialPath); |
||
| 60 | |||
| 61 | $spaces = Str::before($extracted[0], \trim($extracted[0])); |
||
| 62 | // add space before the @include to have proper indentation. |
||
| 63 | $file[$start] = $spaces.$file[$start]; |
||
| 64 | foreach ($extracted as $i => $line) { |
||
| 65 | // remove spaces so that the created file |
||
| 66 | // does not have irrelevant indentation. |
||
| 67 | $extracted[$i] = Str::after($extracted[$i], $spaces); |
||
| 68 | } |
||
| 69 | self::forceFilePutContents($partialPath, \implode('', $extracted)); |
||
| 70 | } |
||
| 71 | |||
| 72 | self::forceFilePutContents($absPath, \implode('', $file)); |
||
| 73 | |||
| 74 | return $tokens; |
||
| 75 | } |
||
| 76 | |||
| 148 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.