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 ObjectUtils extends UtilsBase |
||
6 | { |
||
7 | const SHALLOW = false; |
||
8 | const DEEP = true; |
||
9 | |||
10 | /** @var ArrayUtils */ |
||
11 | private $arrayUtils; |
||
12 | |||
13 | protected function __construct() |
||
17 | |||
18 | /** |
||
19 | * @param object $objectToParse |
||
20 | * @param bool $deep |
||
21 | * |
||
22 | * @return \SimpleXMLElement |
||
23 | */ |
||
24 | public function toXml($objectToParse, $deep = self::SHALLOW) |
||
25 | { |
||
26 | return simplexml_load_string($this->toXmlString($objectToParse, $deep)); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param object $object |
||
31 | * @param bool $deep |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public function toXmlString($object, $deep = self::SHALLOW) |
||
39 | |||
40 | /** |
||
41 | * @param object $object |
||
42 | * @param bool $deep |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | public function toArray($object, $deep = self::SHALLOW) |
||
50 | |||
51 | View Code Duplication | private function toDeepArray($object) |
|
73 | |||
74 | private function isDateTimeExtensionObject($object) |
||
80 | |||
81 | private function parseDateTimeExtensionObject($object) |
||
93 | |||
94 | private function getAllObjectVarsOrArrayProperties($object) |
||
103 | |||
104 | private function getAllObjectVars($object) |
||
119 | |||
120 | View Code Duplication | private function toShallowArray($object) |
|
140 | } |
||
141 |
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.