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 | trait CreatesMigrations |
||
10 | { |
||
11 | public |
||
12 | $autoMigrate, |
||
13 | $filename, |
||
14 | $path; |
||
15 | |||
16 | public function __construct() |
||
21 | |||
22 | public function __destruct() |
||
35 | |||
36 | /** |
||
37 | * @param $request |
||
38 | * @return string |
||
39 | */ |
||
40 | protected function getLocalKey($request): string |
||
45 | |||
46 | /** |
||
47 | * @param $request |
||
48 | * @return string |
||
49 | */ |
||
50 | View Code Duplication | protected function getForeignKey($request): string |
|
60 | |||
61 | protected function getSecondLocalKey($request): string |
||
66 | |||
67 | View Code Duplication | protected function getSecondForeignKey($request): string |
|
77 | |||
78 | |||
79 | |||
80 | /** |
||
81 | * @param $fields |
||
82 | * @return array |
||
83 | */ |
||
84 | protected function getFields($fields): array |
||
92 | |||
93 | /** |
||
94 | * @param $field |
||
95 | * @return mixed |
||
96 | */ |
||
97 | protected function setUpChange($field) |
||
107 | |||
108 | /** |
||
109 | * @param $field |
||
110 | * @return mixed |
||
111 | */ |
||
112 | protected function setDownChange($field) |
||
127 | |||
128 | |||
129 | /** |
||
130 | * Parsing column type to migration rule |
||
131 | * |
||
132 | * @param $columnType |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function parseColumnType($columnType): string |
||
172 | |||
173 | } |
||
174 |
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.