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 |
||
7 | class Processor |
||
8 | { |
||
9 | /** |
||
10 | * Split a string into seperate properties |
||
11 | * |
||
12 | * @param string $propertiesString |
||
13 | * @return array |
||
14 | */ |
||
15 | public function splitIntoSeparateProperties($propertiesString) |
||
41 | |||
42 | /** |
||
43 | * @param $string |
||
44 | * @return mixed|string |
||
45 | */ |
||
46 | View Code Duplication | private function cleanup($string) |
|
59 | |||
60 | /** |
||
61 | * Convert a property-string into an object |
||
62 | * |
||
63 | * @param string $property |
||
64 | * @return Property|null |
||
65 | */ |
||
66 | public function convertToObject($property, Specificity $specificity = null) |
||
83 | |||
84 | /** |
||
85 | * Convert an array of property-strings into objects |
||
86 | * |
||
87 | * @param array $properties |
||
88 | * @return Property[] |
||
89 | */ |
||
90 | public function convertArrayToObjects(array $properties, Specificity $specificity = null) |
||
105 | |||
106 | /** |
||
107 | * Build the property-string for multiple properties |
||
108 | * |
||
109 | * @param array $properties |
||
110 | * @return string |
||
111 | */ |
||
112 | public function buildPropertiesString(array $properties) |
||
122 | } |
||
123 |
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.