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:
1 | <?php |
||
9 | View Code Duplication | class ModuleMakeControllerHandler |
|
|
|||
10 | { |
||
11 | /** |
||
12 | * @var \Caffeinated\Modules\Modules |
||
13 | */ |
||
14 | protected $module; |
||
15 | |||
16 | /** |
||
17 | * @var \Illuminate\Filesystem\Filesystem |
||
18 | */ |
||
19 | protected $finder; |
||
20 | |||
21 | /** |
||
22 | * @var \Illuminate\Console\Command |
||
23 | */ |
||
24 | protected $console; |
||
25 | |||
26 | /** |
||
27 | * @var string $moduleName The name of the module |
||
28 | */ |
||
29 | protected $moduleName; |
||
30 | |||
31 | /** |
||
32 | * @var string $className The name of the request class |
||
33 | */ |
||
34 | protected $className; |
||
35 | |||
36 | /** |
||
37 | * Constructor method. |
||
38 | * |
||
39 | * @param \Caffeinated\Modules\Modules $module |
||
40 | * @param \Illuminate\Filesystem\Filesystem $finder |
||
41 | */ |
||
42 | public function __construct(Modules $module, Filesystem $finder) |
||
47 | |||
48 | /** |
||
49 | * Fire off the handler. |
||
50 | * |
||
51 | * @param \Illuminate\Console\Command $console |
||
52 | * @param string $slug |
||
53 | * @param string $class |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function fire(Command $console, $slug, $class) |
||
70 | |||
71 | /** |
||
72 | * Create new migration file. |
||
73 | * |
||
74 | * @return int |
||
75 | */ |
||
76 | protected function makeFile() |
||
80 | |||
81 | /** |
||
82 | * Get file destination. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | protected function getDestinationFile() |
||
90 | |||
91 | /** |
||
92 | * Get module migration path. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | protected function getPath() |
||
102 | |||
103 | /** |
||
104 | * Get migration filename. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function getFilename() |
||
112 | |||
113 | /** |
||
114 | * Get stub content. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | protected function getStubContent() |
||
122 | |||
123 | /** |
||
124 | * Replace placeholder text with correct values. |
||
125 | * |
||
126 | * @param string $content |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function formatContent($content) |
||
137 | } |
||
138 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.