| Conditions | 13 |
| Paths | 70 |
| Total Lines | 44 |
| 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 |
||
| 68 | public function insertSemicolon(array $parameters = []) |
||
| 69 | { |
||
| 70 | if (strpos($parameters['filePath'], 'ignition/tests/stubs') !== false) { |
||
| 71 | $file = $parameters['filePath']; |
||
| 72 | } else { |
||
| 73 | $file = app_path().$parameters['filePath']; |
||
| 74 | } |
||
| 75 | if (! is_file($file)) { |
||
| 76 | return false; |
||
| 77 | } |
||
| 78 | $contents = file_get_contents($file); |
||
| 79 | $tokens = token_get_all($contents); |
||
| 80 | |||
| 81 | $reverseTokens = array_reverse($tokens); |
||
| 82 | |||
| 83 | $output = []; |
||
| 84 | $insertSemicolon = false; |
||
| 85 | $line = 0; |
||
| 86 | foreach ($reverseTokens as $token) { |
||
| 87 | $char = isset($token[1]) ? $token[1] : $token; |
||
| 88 | |||
| 89 | $output[] = $char; |
||
| 90 | |||
| 91 | if (isset($token[2])) { |
||
| 92 | $line = $token[2]; |
||
| 93 | } |
||
| 94 | if (is_string($token) && $line == $parameters['lineNumber'] && $char == $parameters['unexpected']) { |
||
| 95 | $insertSemicolon = true; |
||
| 96 | } |
||
| 97 | if ($insertSemicolon && isset($token[0]) && $token[0] == T_WHITESPACE) { |
||
| 98 | $insertSemicolon = false; |
||
| 99 | $output[] = ';'; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | $proposedFix = implode('', array_reverse($output)); |
||
| 103 | |||
| 104 | $result = exec(sprintf('echo %s | php -l', escapeshellarg($proposedFix)), $output, $exit); |
||
| 105 | |||
| 106 | if (Str::contains($result, 'No syntax errors')) { |
||
| 107 | return $proposedFix; |
||
| 108 | } |
||
| 109 | |||
| 110 | return false; |
||
| 111 | } |
||
| 112 | } |
||
| 113 |