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:
Complex classes like Kint_Parsers_Color often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Kint_Parsers_Color, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Kint_Parsers_Color extends KintParser |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private static $_css3Named = array( |
||
16 | 'aliceblue' => '#f0f8ff', |
||
17 | 'antiquewhite' => '#faebd7', |
||
18 | 'aqua' => '#00ffff', |
||
19 | 'aquamarine' => '#7fffd4', |
||
20 | 'azure' => '#f0ffff', |
||
21 | 'beige' => '#f5f5dc', |
||
22 | 'bisque' => '#ffe4c4', |
||
23 | 'black' => '#000000', |
||
24 | 'blanchedalmond' => '#ffebcd', |
||
25 | 'blue' => '#0000ff', |
||
26 | 'blueviolet' => '#8a2be2', |
||
27 | 'brown' => '#a52a2a', |
||
28 | 'burlywood' => '#deb887', |
||
29 | 'cadetblue' => '#5f9ea0', |
||
30 | 'chartreuse' => '#7fff00', |
||
31 | 'chocolate' => '#d2691e', |
||
32 | 'coral' => '#ff7f50', |
||
33 | 'cornflowerblue' => '#6495ed', |
||
34 | 'cornsilk' => '#fff8dc', |
||
35 | 'crimson' => '#dc143c', |
||
36 | 'cyan' => '#00ffff', |
||
37 | 'darkblue' => '#00008b', |
||
38 | 'darkcyan' => '#008b8b', |
||
39 | 'darkgoldenrod' => '#b8860b', |
||
40 | 'darkgray' => '#a9a9a9', |
||
41 | 'darkgrey' => '#a9a9a9', |
||
42 | 'darkgreen' => '#006400', |
||
43 | 'darkkhaki' => '#bdb76b', |
||
44 | 'darkmagenta' => '#8b008b', |
||
45 | 'darkolivegreen' => '#556b2f', |
||
46 | 'darkorange' => '#ff8c00', |
||
47 | 'darkorchid' => '#9932cc', |
||
48 | 'darkred' => '#8b0000', |
||
49 | 'darksalmon' => '#e9967a', |
||
50 | 'darkseagreen' => '#8fbc8f', |
||
51 | 'darkslateblue' => '#483d8b', |
||
52 | 'darkslategray' => '#2f4f4f', |
||
53 | 'darkslategrey' => '#2f4f4f', |
||
54 | 'darkturquoise' => '#00ced1', |
||
55 | 'darkviolet' => '#9400d3', |
||
56 | 'deeppink' => '#ff1493', |
||
57 | 'deepskyblue' => '#00bfff', |
||
58 | 'dimgray' => '#696969', |
||
59 | 'dimgrey' => '#696969', |
||
60 | 'dodgerblue' => '#1e90ff', |
||
61 | 'firebrick' => '#b22222', |
||
62 | 'floralwhite' => '#fffaf0', |
||
63 | 'forestgreen' => '#228b22', |
||
64 | 'fuchsia' => '#ff00ff', |
||
65 | 'gainsboro' => '#dcdcdc', |
||
66 | 'ghostwhite' => '#f8f8ff', |
||
67 | 'gold' => '#ffd700', |
||
68 | 'goldenrod' => '#daa520', |
||
69 | 'gray' => '#808080', |
||
70 | 'grey' => '#808080', |
||
71 | 'green' => '#008000', |
||
72 | 'greenyellow' => '#adff2f', |
||
73 | 'honeydew' => '#f0fff0', |
||
74 | 'hotpink' => '#ff69b4', |
||
75 | 'indianred' => '#cd5c5c', |
||
76 | 'indigo' => '#4b0082', |
||
77 | 'ivory' => '#fffff0', |
||
78 | 'khaki' => '#f0e68c', |
||
79 | 'lavender' => '#e6e6fa', |
||
80 | 'lavenderblush' => '#fff0f5', |
||
81 | 'lawngreen' => '#7cfc00', |
||
82 | 'lemonchiffon' => '#fffacd', |
||
83 | 'lightblue' => '#add8e6', |
||
84 | 'lightcoral' => '#f08080', |
||
85 | 'lightcyan' => '#e0ffff', |
||
86 | 'lightgoldenrodyellow' => '#fafad2', |
||
87 | 'lightgray' => '#d3d3d3', |
||
88 | 'lightgrey' => '#d3d3d3', |
||
89 | 'lightgreen' => '#90ee90', |
||
90 | 'lightpink' => '#ffb6c1', |
||
91 | 'lightsalmon' => '#ffa07a', |
||
92 | 'lightseagreen' => '#20b2aa', |
||
93 | 'lightskyblue' => '#87cefa', |
||
94 | 'lightslategray' => '#778899', |
||
95 | 'lightslategrey' => '#778899', |
||
96 | 'lightsteelblue' => '#b0c4de', |
||
97 | 'lightyellow' => '#ffffe0', |
||
98 | 'lime' => '#00ff00', |
||
99 | 'limegreen' => '#32cd32', |
||
100 | 'linen' => '#faf0e6', |
||
101 | 'magenta' => '#ff00ff', |
||
102 | 'maroon' => '#800000', |
||
103 | 'mediumaquamarine' => '#66cdaa', |
||
104 | 'mediumblue' => '#0000cd', |
||
105 | 'mediumorchid' => '#ba55d3', |
||
106 | 'mediumpurple' => '#9370d8', |
||
107 | 'mediumseagreen' => '#3cb371', |
||
108 | 'mediumslateblue' => '#7b68ee', |
||
109 | 'mediumspringgreen' => '#00fa9a', |
||
110 | 'mediumturquoise' => '#48d1cc', |
||
111 | 'mediumvioletred' => '#c71585', |
||
112 | 'midnightblue' => '#191970', |
||
113 | 'mintcream' => '#f5fffa', |
||
114 | 'mistyrose' => '#ffe4e1', |
||
115 | 'moccasin' => '#ffe4b5', |
||
116 | 'navajowhite' => '#ffdead', |
||
117 | 'navy' => '#000080', |
||
118 | 'oldlace' => '#fdf5e6', |
||
119 | 'olive' => '#808000', |
||
120 | 'olivedrab' => '#6b8e23', |
||
121 | 'orange' => '#ffa500', |
||
122 | 'orangered' => '#ff4500', |
||
123 | 'orchid' => '#da70d6', |
||
124 | 'palegoldenrod' => '#eee8aa', |
||
125 | 'palegreen' => '#98fb98', |
||
126 | 'paleturquoise' => '#afeeee', |
||
127 | 'palevioletred' => '#d87093', |
||
128 | 'papayawhip' => '#ffefd5', |
||
129 | 'peachpuff' => '#ffdab9', |
||
130 | 'peru' => '#cd853f', |
||
131 | 'pink' => '#ffc0cb', |
||
132 | 'plum' => '#dda0dd', |
||
133 | 'powderblue' => '#b0e0e6', |
||
134 | 'purple' => '#800080', |
||
135 | 'red' => '#ff0000', |
||
136 | 'rosybrown' => '#bc8f8f', |
||
137 | 'royalblue' => '#4169e1', |
||
138 | 'saddlebrown' => '#8b4513', |
||
139 | 'salmon' => '#fa8072', |
||
140 | 'sandybrown' => '#f4a460', |
||
141 | 'seagreen' => '#2e8b57', |
||
142 | 'seashell' => '#fff5ee', |
||
143 | 'sienna' => '#a0522d', |
||
144 | 'silver' => '#c0c0c0', |
||
145 | 'skyblue' => '#87ceeb', |
||
146 | 'slateblue' => '#6a5acd', |
||
147 | 'slategray' => '#708090', |
||
148 | 'slategrey' => '#708090', |
||
149 | 'snow' => '#fffafa', |
||
150 | 'springgreen' => '#00ff7f', |
||
151 | 'steelblue' => '#4682b4', |
||
152 | 'tan' => '#d2b48c', |
||
153 | 'teal' => '#008080', |
||
154 | 'thistle' => '#d8bfd8', |
||
155 | 'tomato' => '#ff6347', |
||
156 | 'turquoise' => '#40e0d0', |
||
157 | 'violet' => '#ee82ee', |
||
158 | 'wheat' => '#f5deb3', |
||
159 | 'white' => '#ffffff', |
||
160 | 'whitesmoke' => '#f5f5f5', |
||
161 | 'yellow' => '#ffff00', |
||
162 | 'yellowgreen' => '#9acd32', |
||
163 | ); |
||
164 | |||
165 | /** |
||
166 | * @param array $hsl |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | private static function _HSLtoRGB(array $hsl) |
||
182 | |||
183 | /** |
||
184 | * @param array $rgb |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | private static function _RGBtoHSL(array $rgb) |
||
234 | |||
235 | /** |
||
236 | * @param $color |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | private static function _convert(&$color) |
||
368 | |||
369 | /** |
||
370 | * @param $variable |
||
371 | * |
||
372 | * @return bool |
||
373 | */ |
||
374 | 1 | private static function _fits(&$variable) |
|
395 | |||
396 | /** |
||
397 | * Helper function for _color_hsl2rgb(). |
||
398 | * |
||
399 | * @param $m1 |
||
400 | * @param $m2 |
||
401 | * @param $h |
||
402 | * |
||
403 | * @return mixed |
||
404 | */ |
||
405 | private static function _hue2rgb($m1, $m2, $h) |
||
424 | |||
425 | /** |
||
426 | * @param mixed $variable |
||
427 | * |
||
428 | * @return bool |
||
429 | */ |
||
430 | 1 | protected function _parse(&$variable) |
|
447 | } |
||
448 |
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.