We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 12 |
Paths | 38 |
Total Lines | 99 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 7 | 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 | // in case the last line contains the last } but also the last {, we need to split them |
||
89 | // so that we can create a space between them and add the new code |
||
90 | if (strpos($cleanContent[$lastLine], '{') !== false) { |
||
91 | $lastLineContent = explode('{', $originalContent[$lastLine]); |
||
92 | $originalContent[$lastLine] = $lastLineContent[0].'{'.PHP_EOL; |
||
93 | // push all other elements one line down creating space for the new code |
||
94 | for ($i = count($originalContent) - 1; $i > $lastLine; $i--) { |
||
95 | $originalContent[$i + 1] = $originalContent[$i]; |
||
96 | } |
||
97 | $originalContent[$lastLine + 1] = $lastLineContent[1]; |
||
98 | $lastLine++; |
||
99 | } |
||
100 | |||
101 | // in case the last line contains more than one ";" it means that line closes more than one group |
||
102 | // we need to split the line and create space for the new code |
||
103 | if (substr_count($cleanContent[$lastLine], ';') > 1) { |
||
104 | $lastLineContent = explode(';', $originalContent[$lastLine]); |
||
105 | |||
106 | // find in lastLineContent array the last element that contains the } |
||
107 | $lastElement = $this->getLastLineNumberThatContains('}', $lastLineContent); |
||
108 | |||
109 | // merge the first part of the lastLineContent up to the lastElement |
||
110 | $originalContent[$lastLine] = implode(';', array_slice($lastLineContent, 0, $lastElement)).';'.PHP_EOL; |
||
111 | |||
112 | // push all other elements one line down creating space for the new code |
||
113 | for ($i = count($originalContent) - 1; $i > $lastLine; $i--) { |
||
114 | $originalContent[$i + 1] = $originalContent[$i]; |
||
115 | } |
||
116 | |||
117 | // merge the second part of the lastLineContent starting from the lastElement |
||
118 | $originalContent[$lastLine + 1] = implode(';', array_slice($lastLineContent, $lastElement)); |
||
119 | $lastLine++; |
||
120 | } |
||
121 | |||
122 | $sliceLength = 0; |
||
123 | |||
124 | // in case there is already an empty line at the end of the route file, we don't need to add another one |
||
125 | if (trim($originalContent[$lastLine - 1]) === '') { |
||
126 | $lastLine--; |
||
127 | $sliceLength = 1; |
||
128 | } |
||
129 | |||
130 | // add the code to the line before the last line |
||
131 | array_splice($originalContent, $lastLine, $sliceLength, ' '.$code.PHP_EOL); |
||
132 | |||
133 | // write the new content to the file |
||
134 | if (file_put_contents($routeFilePath, implode('', $originalContent)) === false) { |
||
135 | $this->closeProgressBlock('Failed to add route. Failed writing the modified route file. Maybe check file permissions?', 'red'); |
||
136 | |||
137 | return; |
||
138 | } |
||
139 | |||
140 | $this->closeProgressBlock('done', 'green'); |
||
141 | } |
||
189 |