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 |
||
24 | abstract class AbstractRelation implements RelationInterface |
||
25 | { |
||
26 | use GetTypeTrait; |
||
27 | |||
28 | protected $entity; |
||
29 | |||
30 | protected $mediator; |
||
31 | |||
32 | protected $type; |
||
33 | protected $container; |
||
34 | |||
35 | protected $keys = []; |
||
36 | protected $in = []; |
||
37 | protected $out = []; |
||
38 | |||
39 | /** |
||
40 | * Returns container name or builds it from namespaced class name if passed name is empty string |
||
41 | * |
||
42 | * @param null|string $container |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | protected function containerName($container = null) |
||
59 | |||
60 | /** |
||
61 | * Assigns key pairs to passed container |
||
62 | * |
||
63 | * @param array $keys |
||
64 | * @param array $container |
||
65 | */ |
||
66 | protected function assignKeys(array $keys, array &$container) |
||
75 | |||
76 | /** |
||
77 | * Asserts keys (non empty array) |
||
78 | * |
||
79 | * @param $keys |
||
80 | * |
||
81 | * @throws DefinitionException |
||
82 | */ |
||
83 | protected function assertKeys($keys) |
||
90 | |||
91 | /** |
||
92 | * Asserts trough keys, must be same number in both arrays |
||
93 | * |
||
94 | * @param array $inKeys |
||
95 | * @param array $outKeys |
||
96 | * |
||
97 | * @throws DefinitionException |
||
98 | */ |
||
99 | protected function assertTroughKeys($inKeys, $outKeys) |
||
109 | |||
110 | /** |
||
111 | * Asserts field name |
||
112 | * |
||
113 | * @param string $field |
||
114 | * |
||
115 | * @throws DefinitionException |
||
116 | */ |
||
117 | protected function assertField($field) |
||
131 | |||
132 | /** |
||
133 | * Returns relation name in entity |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | public function name() |
||
141 | |||
142 | /** |
||
143 | * Returns relation mediating instance |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function mediator() |
||
151 | |||
152 | /** |
||
153 | * Returns relation type |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function type() |
||
161 | |||
162 | /** |
||
163 | * Returns relation entity class name |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function entity() |
||
171 | |||
172 | /** |
||
173 | * Returns table name |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function container() |
||
181 | |||
182 | /** |
||
183 | * Returns associative array containing local key - foreign key pairs |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | public function keys() |
||
191 | |||
192 | /** |
||
193 | * Returns array containing local keys |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public function localKeys() |
||
201 | |||
202 | /** |
||
203 | * Returns array containing foreign keys |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public function foreignKeys() |
||
211 | } |
||
212 |
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.