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 |
||
8 | class Processor |
||
9 | { |
||
10 | /** |
||
11 | * Split a string into seperate rules |
||
12 | * |
||
13 | * @param string $rulesString |
||
14 | * @return array |
||
15 | */ |
||
16 | public function splitIntoSeparateRules($rulesString) |
||
22 | |||
23 | /** |
||
24 | * @param string $string |
||
25 | * @return string |
||
26 | */ |
||
27 | View Code Duplication | private function cleanup($string) |
|
40 | |||
41 | /** |
||
42 | * Convert a rule-string into an object |
||
43 | * |
||
44 | * @param string $rule |
||
45 | * @param int $originalOrder |
||
46 | * @return array |
||
47 | */ |
||
48 | public function convertToObjects($rule, $originalOrder) |
||
75 | |||
76 | /** |
||
77 | * Calculate the specificity based on a CSS Selector string, |
||
78 | * Based on the patterns from premailer/css_parser by Alex Dunae |
||
79 | * |
||
80 | * @see https://github.com/premailer/css_parser/blob/master/lib/css_parser/regexps.rb |
||
81 | * @param string $selector |
||
82 | * @return Specificity |
||
83 | */ |
||
84 | public function calculateSpecificityBasedOnASelector($selector) |
||
119 | |||
120 | /** |
||
121 | * @param array $rules |
||
122 | * @return Rule[] |
||
123 | */ |
||
124 | public function convertArrayToObjects(array $rules, array $objects = array()) |
||
134 | |||
135 | /** |
||
136 | * Sort an array on the specificity element |
||
137 | * |
||
138 | * @return int |
||
139 | * @param Rule $e1 The first element. |
||
140 | * @param Rule $e2 The second element. |
||
141 | */ |
||
142 | private static function sortOnSpecificity(Rule $e1, Rule $e2) |
||
154 | } |
||
155 |
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.