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 | 56 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | 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 |
||
44 | public function handle() |
||
45 | { |
||
46 | $routeFilePath = base_path($this->option('route-file')); |
||
47 | |||
48 | if (! file_exists($routeFilePath)) { |
||
49 | if ($routeFilePath !== base_path($this->backpackCustomRouteFile)) { |
||
50 | $this->info('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Please create it first.'); |
||
51 | return 1; |
||
52 | } |
||
53 | |||
54 | $createRouteFile = $this->confirm('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Should we create it?', 'yes'); |
||
55 | if ($createRouteFile === 'yes') { |
||
56 | $this->call('vendor:publish', ['--provider' => \Backpack\CRUD\BackpackServiceProvider::class, '--tag' => 'custom_routes']); |
||
57 | } else { |
||
58 | $this->info('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Please create it first.'); |
||
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 | $cleanCode = $this->cleanCodeString($code); |
||
72 | |||
73 | // if the content contains code, don't add it again. |
||
74 | if (array_search($cleanCode, $cleanContent, true) !== false) { |
||
75 | $this->closeProgressBlock('Already existed', 'yellow'); |
||
76 | |||
77 | return; |
||
78 | } |
||
79 | |||
80 | // get the last element of the array contains '}' |
||
81 | $lastLine = $this->getLastLineNumberThatContains('}', $cleanContent); |
||
82 | |||
83 | if ($lastLine === false) { |
||
84 | $this->closeProgressBlock('Could not find the last line, file '.$routeFilePath.' may be corrupted.', 'red'); |
||
85 | |||
86 | return; |
||
87 | } |
||
88 | |||
89 | // add the code to the line before the last line |
||
90 | array_splice($originalContent, $lastLine, 0, ' '.$code.PHP_EOL); |
||
91 | |||
92 | // write the new content to the file |
||
93 | if (file_put_contents($routeFilePath, implode('', $originalContent)) === false) { |
||
94 | $this->closeProgressBlock('Failed to add route. Failed writing the modified route file. Maybe check file permissions?', 'red'); |
||
95 | |||
96 | return; |
||
97 | } |
||
98 | |||
99 | $this->closeProgressBlock('done', 'green'); |
||
100 | } |
||
171 |