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 |
||
23 | class XmlTransformer extends Transformer |
||
24 | { |
||
25 | const META_KEY = 'meta'; |
||
26 | const LINKS = 'links'; |
||
27 | |||
28 | /** |
||
29 | * @param mixed $value |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public function serialize($value) |
||
39 | |||
40 | /** |
||
41 | * @param $value |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | protected function serialization($value) |
||
58 | |||
59 | /** |
||
60 | * @param array $value |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | protected function serializeObject(array $value) |
||
82 | |||
83 | /** |
||
84 | * @param array $response |
||
85 | */ |
||
86 | private function setResponseMeta(array &$response) |
||
92 | |||
93 | /** |
||
94 | * @param array $response |
||
95 | */ |
||
96 | private function setResponseLinks(array &$response) |
||
110 | |||
111 | /** |
||
112 | * @param array $response |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | protected function buildSelfLink(array &$response) |
||
142 | |||
143 | /** |
||
144 | * @param \NilPortugues\Api\Mapping\Mapping[] $mappings |
||
145 | * @param $idProperties |
||
146 | * @param $idValues |
||
147 | * @param $url |
||
148 | * @param $type |
||
149 | * |
||
150 | * @return mixed |
||
151 | */ |
||
152 | protected static function buildUrl(array &$mappings, $idProperties, $idValues, $url, $type) |
||
177 | |||
178 | /** |
||
179 | * @param $idPropertyName |
||
180 | * @param $idValues |
||
181 | * @param $url |
||
182 | * |
||
183 | * @return mixed |
||
184 | */ |
||
185 | protected static function secondPassBuildUrl($idPropertyName, $idValues, $url) |
||
206 | |||
207 | /** |
||
208 | * @param $original |
||
209 | * @param $idValues |
||
210 | * @param $url |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | View Code Duplication | protected static function toCamelCase($original, $idValues, $url) |
|
222 | |||
223 | /** |
||
224 | * Converts a underscore string to camelCase. |
||
225 | * |
||
226 | * @param string $string |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | protected static function underscoreToCamelCase($string) |
||
234 | |||
235 | /** |
||
236 | * Transforms a given string from camelCase to under_score style. |
||
237 | * |
||
238 | * @param string $camel |
||
239 | * @param string $splitter |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | protected static function camelCaseToUnderscore($camel, $splitter = '_') |
||
253 | |||
254 | /** |
||
255 | * @param $original |
||
256 | * @param $idValues |
||
257 | * @param $url |
||
258 | * |
||
259 | * @return mixed |
||
260 | */ |
||
261 | protected static function toLowerFirstCamelCase($original, $idValues, $url) |
||
271 | |||
272 | /** |
||
273 | * @param $original |
||
274 | * @param $idValues |
||
275 | * @param $url |
||
276 | * |
||
277 | * @return mixed |
||
278 | */ |
||
279 | View Code Duplication | protected static function toUnderScore($original, $idValues, $url) |
|
287 | } |
||
288 |
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.