Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MakeRoute often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MakeRoute, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class MakeRoute extends Command |
||
24 | { |
||
25 | use Controller, CreatesModels; |
||
26 | |||
27 | /** |
||
28 | * Path to web routes file. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $web_routes_path = 'routes/web.php'; |
||
33 | |||
34 | /** |
||
35 | * Path to api routes file. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $api_routes_path = 'routes/api.php'; |
||
40 | |||
41 | /** |
||
42 | * Compiler for stub file. |
||
43 | * |
||
44 | * @var StubFileCompiler |
||
45 | */ |
||
46 | protected $compiler; |
||
47 | |||
48 | /** |
||
49 | * Compiler for stub file. |
||
50 | * |
||
51 | * @var Filesystem |
||
52 | */ |
||
53 | protected $filesystem; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $lookup = [ |
||
59 | 'regular' => RegularRoute::class, |
||
60 | 'controller' => ControllerRoute::class, |
||
61 | 'resource' => ControllerResourceRoute::class, |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * The name and signature of the console command. |
||
66 | */ |
||
67 | protected $signature = 'make:route {link : The route link} {action? : View or controller to create} |
||
68 | {--t|type=regular : Type of route to create (regular,controller,resource)} {--m|method=get : HTTP method} |
||
69 | {--api : Route is an api route} {--a|createaction : Create view or controller after route} |
||
70 | {--menu : Create also menu entry using make:menu command} {--model : Create also a model using command make:model}'; |
||
71 | |||
72 | /** |
||
73 | * The console command description. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $description = 'Insert a route to routes/web.php file'; |
||
78 | |||
79 | /** |
||
80 | * AdminLTERoute constructor. |
||
81 | * |
||
82 | * @param StubFileCompiler $compiler |
||
83 | * @param Filesystem $filesystem |
||
84 | */ |
||
85 | public function __construct(StubFileCompiler $compiler, Filesystem $filesystem) |
||
91 | |||
92 | /** |
||
93 | * Execute the console command. |
||
94 | */ |
||
95 | public function handle() |
||
96 | { |
||
97 | $this->processInput(); |
||
98 | $this->warnIfRouteAlreadyExists($link = $this->argument('link')); |
||
99 | $tmpfile = $this->createTmpFileWithRoute(); |
||
100 | $path = $this->getPath($tmpfile); |
||
101 | add_file_into_file($this->mountpoint(), $path, $dstFile = $this->destinationFile()); |
||
102 | $this->info('Route ' . undot_path($link) . ' added to ' . $dstFile . '.'); |
||
103 | $this->postActions(); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Get mountpoint. |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | protected function mountpoint() |
||
118 | |||
119 | /** |
||
120 | * Destination route file. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | protected function destinationFile() |
||
131 | |||
132 | /** |
||
133 | * Warn if route already exists. |
||
134 | * |
||
135 | * @param $link |
||
136 | */ |
||
137 | protected function warnIfRouteAlreadyExists($link) |
||
146 | |||
147 | /** |
||
148 | * Check if route exists. |
||
149 | * |
||
150 | * @param $link |
||
151 | * @return mixed |
||
152 | */ |
||
153 | protected function routeExists($link) |
||
160 | |||
161 | /** |
||
162 | * Check if web route exists. |
||
163 | * |
||
164 | * @param $link |
||
165 | * @return mixed |
||
166 | */ |
||
167 | protected function webRouteExists($link) |
||
179 | |||
180 | /** |
||
181 | * Remove (if exists) trailing slash from link. |
||
182 | * |
||
183 | * @param $link |
||
184 | * @return string |
||
185 | */ |
||
186 | protected function removeTrailingSlashIfExists($link) |
||
193 | |||
194 | /** |
||
195 | * Remove duplicated trailing slashes. |
||
196 | * |
||
197 | * @param $link |
||
198 | * @return mixed |
||
199 | */ |
||
200 | protected function removeDuplicatedTrailingSlashes($link) |
||
204 | |||
205 | /** |
||
206 | * Check if api route exists. |
||
207 | * |
||
208 | * @param $link |
||
209 | * @return mixed |
||
210 | */ |
||
211 | protected function apiRouteExists($link) |
||
215 | |||
216 | /** |
||
217 | * Crete tmp file with route to add. |
||
218 | * |
||
219 | * @return mixed |
||
220 | */ |
||
221 | protected function createTmpFileWithRoute() |
||
227 | |||
228 | /** |
||
229 | * Get path from file resource. |
||
230 | * |
||
231 | * @param $tmpfile |
||
232 | * @return mixed |
||
233 | */ |
||
234 | protected function getPath($tmpfile) |
||
238 | |||
239 | /** |
||
240 | * Get route code to insert depending on type. |
||
241 | * |
||
242 | * @return mixed |
||
243 | */ |
||
244 | View Code Duplication | protected function getRouteCode() |
|
259 | |||
260 | /** |
||
261 | * Get method. |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | View Code Duplication | protected function method() |
|
272 | |||
273 | /** |
||
274 | * Get the action replacement. |
||
275 | * |
||
276 | * @return array|string |
||
277 | */ |
||
278 | protected function action() |
||
288 | |||
289 | /** |
||
290 | * Process input. |
||
291 | */ |
||
292 | protected function processInput() |
||
297 | |||
298 | /** |
||
299 | * Validate option method. |
||
300 | */ |
||
301 | protected function validateMethod() |
||
307 | |||
308 | /** |
||
309 | * Validate option type. |
||
310 | */ |
||
311 | protected function validateType() |
||
317 | |||
318 | /** |
||
319 | * Execute post actions (if exists) |
||
320 | */ |
||
321 | protected function postActions() |
||
333 | |||
334 | /** |
||
335 | * Create menu. |
||
336 | */ |
||
337 | protected function createMenu() |
||
352 | |||
353 | /** |
||
354 | * Warn if spatie menu ins not installed. |
||
355 | * |
||
356 | * @throws SpatieMenuDoesNotExists |
||
357 | */ |
||
358 | protected function warnIfSpatieMenuIsNotInstalled() |
||
364 | |||
365 | /** |
||
366 | * Create action (view|controller). |
||
367 | */ |
||
368 | protected function createAction() |
||
378 | |||
379 | /** |
||
380 | * Create View. |
||
381 | * |
||
382 | * @param null $name |
||
383 | */ |
||
384 | protected function createView($name = null) |
||
394 | |||
395 | /** |
||
396 | * Create regular controller. |
||
397 | */ |
||
398 | protected function createController() |
||
407 | |||
408 | /** |
||
409 | * Create resource controller. |
||
410 | */ |
||
411 | protected function createResourceController() |
||
420 | |||
421 | /** |
||
422 | * Add method to controller. |
||
423 | * |
||
424 | * @param $controller |
||
425 | * @param $controllerMethod * |
||
426 | */ |
||
427 | protected function addMethodToController($controller, $controllerMethod) |
||
433 | |||
434 | /** |
||
435 | * Crete tmp file with route to add. |
||
436 | * |
||
437 | * @param $controllerMethod |
||
438 | * @return mixed |
||
439 | */ |
||
440 | protected function createTmpFileWithMethod($controllerMethod) |
||
446 | |||
447 | /** |
||
448 | * Get method code. |
||
449 | * |
||
450 | * @param $controllerMethod |
||
451 | * @return mixed |
||
452 | */ |
||
453 | protected function getMethodCode($controllerMethod) |
||
463 | |||
464 | /** |
||
465 | * Get method stub path. |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | protected function getMethodStubPath() |
||
473 | } |
||
474 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.