Conditions | 14 |
Paths | 265 |
Total Lines | 133 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 210 |
Changes | 1 | ||
Bugs | 0 | 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 declare(strict_types=1); |
||
113 | public function handle(): void |
||
114 | { |
||
115 | if ($this->laravel->environment() !== 'local') { |
||
116 | $this->error('Files should only be generated in a local environment'); |
||
117 | exit; |
||
|
|||
118 | } |
||
119 | |||
120 | if ($this->option('type') === null || !in_array($this->option('type'), ['api', 'page'])) { |
||
121 | $this->selectedType = $this->choice('What type of feature will this be?', ['api', 'page'], $this->defaultType); |
||
122 | } else { |
||
123 | $this->selectedType = $this->option('type'); |
||
124 | } |
||
125 | |||
126 | $name = Str::studly(Str::singular($this->argument('name'))); |
||
1 ignored issue
–
show
|
|||
127 | |||
128 | $controllerPath = app_path('Http/Controllers'); |
||
129 | if ($this->isApi()) { |
||
130 | $controllerPath = $controllerPath . '/Api'; |
||
131 | $controllerFile = $name . 'ApiController.php'; |
||
132 | } else { |
||
133 | $controllerFile = $name . 'Controller.php'; |
||
134 | } |
||
135 | |||
136 | $modelPath = app_path('Entities/' . $name); |
||
137 | $modelFile = $name . '.php'; |
||
138 | |||
139 | $requestPath = app_path('Http/Requests/' . $name); |
||
140 | $createRequestFile = 'Create' . $name . 'Request.php'; |
||
141 | $updateRequestFile = 'Update' . $name . 'Request.php'; |
||
142 | |||
143 | $resourcePath = app_path('Http/Resources/' . $name); |
||
144 | $resourceFile = $name . 'Resource.php'; |
||
145 | |||
146 | $stylePath = resource_path('assets/sass/pages'); |
||
147 | $styleFile = '_' . Str::plural(Str::snake($name, '-')) . '.scss'; |
||
148 | |||
149 | $languageFile = Str::plural(Str::snake($name, '-')) . '.php'; |
||
150 | |||
151 | $featureTestPath = base_path('tests/Feature/Controllers'); |
||
152 | if ($this->isApi()) { |
||
153 | $featureTestPath = $featureTestPath . '/Api'; |
||
154 | $featureTestFile = $name . 'ApiControllerTest.php'; |
||
155 | } else { |
||
156 | $featureTestFile = $name . 'ControllerTest.php'; |
||
157 | } |
||
158 | |||
159 | $unitTestPath = base_path('tests/Unit'); |
||
160 | $unitTestFile = $name . 'Test.php'; |
||
161 | |||
162 | // check for existing model; no need to create if files exist |
||
163 | if (File::exists($modelPath . '/' . $modelFile)) { |
||
164 | $this->error($name . ' already exists!'); |
||
165 | exit; |
||
166 | } |
||
167 | |||
168 | // create controller and add to Git |
||
169 | $controller = $this->buildFile($name, ($this->isApi() ? 'controller.api' : 'controller')); |
||
170 | $this->saveFile($controllerPath, $controllerFile, $controller); |
||
171 | $this->gitAdd($controllerPath . '/' . $controllerFile); |
||
172 | |||
173 | // create model and add to Git |
||
174 | $model = $this->buildFile($name, 'model'); |
||
175 | $this->saveFile($modelPath, $modelFile, $model); |
||
176 | $this->gitAdd($modelPath . '/' . $modelFile); |
||
177 | |||
178 | // create create request and add to Git |
||
179 | $createRequest = $this->buildFile($name, 'request.create'); |
||
180 | $this->saveFile($requestPath, $createRequestFile, $createRequest); |
||
181 | $this->gitAdd($requestPath . '/' . $createRequestFile); |
||
182 | |||
183 | // create update request and add to Git |
||
184 | $updateRequest = $this->buildFile($name, 'request.update'); |
||
185 | $this->saveFile($requestPath, $updateRequestFile, $updateRequest); |
||
186 | $this->gitAdd($requestPath . '/' . $updateRequestFile); |
||
187 | |||
188 | if ($this->isApi()) { |
||
189 | // create API resource and add to Git |
||
190 | $resource = $this->buildFile($name, 'resource'); |
||
191 | $this->saveFile($resourcePath, $resourceFile, $resource); |
||
192 | $this->gitAdd($resourcePath . '/' . $resourceFile); |
||
193 | } |
||
194 | |||
195 | // create stylesheet and add to Git |
||
196 | $this->saveFile($stylePath, $styleFile, ''); |
||
197 | $this->gitAdd($stylePath . '/' . $styleFile); |
||
198 | |||
199 | // create languages and add to Git |
||
200 | foreach (File::directories(resource_path('lang')) as $languagePath) { |
||
201 | $language = $this->buildfile(Str::snake($name, '-'), 'lang'); |
||
202 | $this->saveFile($languagePath, $languageFile, $language); |
||
203 | $this->gitAdd($languagePath . '/' . $languageFile); |
||
204 | } |
||
205 | |||
206 | // create feature test and add to Git |
||
207 | $featureTest = $this->buildFile($name, ($this->isApi() ? 'test.feature.controller.api' : 'test.feature.controller')); |
||
208 | $this->saveFile($featureTestPath, $featureTestFile, $featureTest); |
||
209 | $this->gitAdd($featureTestPath . '/' . $featureTestFile); |
||
210 | |||
211 | // create unit test and add to Git |
||
212 | $unitTest = $this->buildFile($name, 'test.unit'); |
||
213 | $this->saveFile($unitTestPath, $unitTestFile, $unitTest); |
||
214 | $this->gitAdd($unitTestPath . '/' . $unitTestFile); |
||
215 | |||
216 | if ($this->option('migration')) { |
||
217 | // create migration and add to Git |
||
218 | $this->buildMigration($name); |
||
219 | $this->gitAdd('$(git ls-files database/migrations --other --exclude-standard)'); |
||
220 | } |
||
221 | |||
222 | if ($this->option('seeder')) { |
||
223 | // create seeder and add to Git |
||
224 | $seederPath = database_path('seeds'); |
||
225 | $seederFile = Str::plural($name) . 'TableSeeder.php'; |
||
226 | |||
227 | $seeder = $this->buildFile($name, 'seeder'); |
||
228 | $this->saveFile($seederPath, $seederFile, $seeder); |
||
229 | $this->gitAdd($seederPath . '/' . $seederFile); |
||
230 | } |
||
231 | |||
232 | if ($this->option('factory')) { |
||
233 | // create factory and add to Git |
||
234 | $factoryPath = database_path('factories'); |
||
235 | $factoryFile = $name . 'Factory.php'; |
||
236 | |||
237 | $factory = $this->buildFile($name, 'factory'); |
||
238 | $this->saveFile($factoryPath, $factoryFile, $factory); |
||
239 | $this->gitAdd($factoryPath . '/' . $factoryFile); |
||
240 | } |
||
241 | |||
242 | // dump composer autoload so everything gets loaded properly |
||
243 | $this->composer->dumpAutoloads(); |
||
244 | |||
245 | $this->info('File generation complete!'); |
||
246 | } |
||
342 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.