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 |
||
5 | class FieldMigration |
||
6 | { |
||
7 | /** |
||
8 | * @var Field |
||
9 | */ |
||
10 | private $field; |
||
11 | |||
12 | /** |
||
13 | * @var Transform[] |
||
14 | */ |
||
15 | private $transforms = []; |
||
16 | |||
17 | /** |
||
18 | * Include the option and options pages by default, this is what |
||
19 | * ACF/ACF PRO will name an unnamed options page. |
||
20 | * @var array |
||
21 | */ |
||
22 | private $optionPages = ['option', 'options']; |
||
23 | |||
24 | /** |
||
25 | * MigrateField constructor. |
||
26 | * @param string $fieldKey ACF Field key of field to be migrated |
||
27 | */ |
||
28 | public function __construct($fieldKey) |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @param Transform $transform |
||
36 | * @return $this |
||
37 | */ |
||
38 | public function addTransform(Transform $transform) |
||
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | public function getTransforms() |
||
52 | |||
53 | /** |
||
54 | * Change the field name. |
||
55 | * @param stirng $newName |
||
56 | * @return $this |
||
57 | */ |
||
58 | public function changeName($newName) |
||
64 | |||
65 | /** |
||
66 | * Change the field key. |
||
67 | * @param stirng $newKey |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function changeKey($newKey) |
||
76 | |||
77 | /** |
||
78 | * Pefrom the migration. |
||
79 | */ |
||
80 | public function migrate() |
||
84 | |||
85 | |||
86 | /** |
||
87 | * @return Field |
||
88 | */ |
||
89 | public function getField() |
||
93 | |||
94 | |||
95 | public function includeOptionPage($optionPage) |
||
101 | |||
102 | public function excludeOptionPage($optionPage) |
||
110 | |||
111 | public function getSubjects() |
||
121 | |||
122 | private function applyTransform($transform) |
||
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | protected function getPosts() |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | View Code Duplication | protected function getTerms() |
|
165 | |||
166 | /** |
||
167 | * @return array |
||
168 | */ |
||
169 | View Code Duplication | protected function getUsers() |
|
182 | |||
183 | /** |
||
184 | * @return array |
||
185 | */ |
||
186 | View Code Duplication | protected function getComments() |
|
199 | /** |
||
200 | * @return array |
||
201 | */ |
||
202 | protected function getOptions() |
||
213 | |||
214 | /** |
||
215 | * @return array |
||
216 | */ |
||
217 | public |
||
222 | } |
||
223 |
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.