We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 7 |
| Paths | 10 |
| Total Lines | 57 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 3 | 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 |
||
| 42 | public function handle() |
||
| 43 | { |
||
| 44 | $routeFilePath = base_path($this->option('route-file')); |
||
| 45 | |||
| 46 | if (! file_exists($routeFilePath)) { |
||
| 47 | if ($routeFilePath !== base_path($this->backpackCustomRouteFile)) { |
||
| 48 | $this->info('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Please create it first.'); |
||
| 49 | |||
| 50 | return 1; |
||
| 51 | } |
||
| 52 | |||
| 53 | $createRouteFile = $this->confirm('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Should we create it?', 'yes'); |
||
| 54 | if ($createRouteFile === 'yes') { |
||
| 55 | $this->call('vendor:publish', ['--provider' => \Backpack\CRUD\BackpackServiceProvider::class, '--tag' => 'custom_routes']); |
||
| 56 | } else { |
||
| 57 | $this->info('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Please create it first.'); |
||
| 58 | |||
| 59 | return 1; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | $code = $this->argument('code'); |
||
| 64 | |||
| 65 | $this->progressBlock("Adding route to <fg=blue>$routeFilePath</>"); |
||
| 66 | |||
| 67 | $originalContent = file($routeFilePath); |
||
| 68 | |||
| 69 | // clean the content from comments etc |
||
| 70 | $cleanContent = $this->cleanContentArray($originalContent); |
||
| 71 | |||
| 72 | // if the content contains code, don't add it again. |
||
| 73 | if (array_search($code, $cleanContent, true) !== false) { |
||
| 74 | $this->closeProgressBlock('Already existed', 'yellow'); |
||
| 75 | |||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | // get the last element of the array contains '}' |
||
| 80 | $lastLine = $this->getLastLineNumberThatContains('}', $cleanContent); |
||
| 81 | |||
| 82 | if ($lastLine === false) { |
||
| 83 | $this->closeProgressBlock('Could not find the last line, file '.$routeFilePath.' may be corrupted.', 'red'); |
||
| 84 | |||
| 85 | return; |
||
| 86 | } |
||
| 87 | |||
| 88 | // add the code to the line before the last line |
||
| 89 | array_splice($originalContent, $lastLine, 0, ' '.$code.PHP_EOL); |
||
| 90 | |||
| 91 | // write the new content to the file |
||
| 92 | if (file_put_contents($routeFilePath, implode('', $originalContent)) === false) { |
||
| 93 | $this->closeProgressBlock('Failed to add route. Failed writing the modified route file. Maybe check file permissions?', 'red'); |
||
| 94 | |||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->closeProgressBlock('done', 'green'); |
||
| 99 | } |
||
| 147 |