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 ColorRepresentation 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 ColorRepresentation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ColorRepresentation extends Representation |
||
9 | { |
||
10 | const COLOR_NAME = 1; |
||
11 | const COLOR_HEX_3 = 2; |
||
12 | const COLOR_HEX_6 = 3; |
||
13 | const COLOR_RGB = 4; |
||
14 | const COLOR_RGBA = 5; |
||
15 | const COLOR_HSL = 6; |
||
16 | const COLOR_HSLA = 7; |
||
17 | const COLOR_HEX_4 = 8; |
||
18 | const COLOR_HEX_8 = 9; |
||
19 | |||
20 | public static $color_map = array( |
||
21 | 'aliceblue' => 'f0f8ff', |
||
22 | 'antiquewhite' => 'faebd7', |
||
23 | 'aqua' => '00ffff', |
||
24 | 'aquamarine' => '7fffd4', |
||
25 | 'azure' => 'f0ffff', |
||
26 | 'beige' => 'f5f5dc', |
||
27 | 'bisque' => 'ffe4c4', |
||
28 | 'black' => '000000', |
||
29 | 'blanchedalmond' => 'ffebcd', |
||
30 | 'blue' => '0000ff', |
||
31 | 'blueviolet' => '8a2be2', |
||
32 | 'brown' => 'a52a2a', |
||
33 | 'burlywood' => 'deb887', |
||
34 | 'cadetblue' => '5f9ea0', |
||
35 | 'chartreuse' => '7fff00', |
||
36 | 'chocolate' => 'd2691e', |
||
37 | 'coral' => 'ff7f50', |
||
38 | 'cornflowerblue' => '6495ed', |
||
39 | 'cornsilk' => 'fff8dc', |
||
40 | 'crimson' => 'dc143c', |
||
41 | 'cyan' => '00ffff', |
||
42 | 'darkblue' => '00008b', |
||
43 | 'darkcyan' => '008b8b', |
||
44 | 'darkgoldenrod' => 'b8860b', |
||
45 | 'darkgray' => 'a9a9a9', |
||
46 | 'darkgreen' => '006400', |
||
47 | 'darkgrey' => 'a9a9a9', |
||
48 | 'darkkhaki' => 'bdb76b', |
||
49 | 'darkmagenta' => '8b008b', |
||
50 | 'darkolivegreen' => '556b2f', |
||
51 | 'darkorange' => 'ff8c00', |
||
52 | 'darkorchid' => '9932cc', |
||
53 | 'darkred' => '8b0000', |
||
54 | 'darksalmon' => 'e9967a', |
||
55 | 'darkseagreen' => '8fbc8f', |
||
56 | 'darkslateblue' => '483d8b', |
||
57 | 'darkslategray' => '2f4f4f', |
||
58 | 'darkslategrey' => '2f4f4f', |
||
59 | 'darkturquoise' => '00ced1', |
||
60 | 'darkviolet' => '9400d3', |
||
61 | 'deeppink' => 'ff1493', |
||
62 | 'deepskyblue' => '00bfff', |
||
63 | 'dimgray' => '696969', |
||
64 | 'dimgrey' => '696969', |
||
65 | 'dodgerblue' => '1e90ff', |
||
66 | 'firebrick' => 'b22222', |
||
67 | 'floralwhite' => 'fffaf0', |
||
68 | 'forestgreen' => '228b22', |
||
69 | 'fuchsia' => 'ff00ff', |
||
70 | 'gainsboro' => 'dcdcdc', |
||
71 | 'ghostwhite' => 'f8f8ff', |
||
72 | 'gold' => 'ffd700', |
||
73 | 'goldenrod' => 'daa520', |
||
74 | 'gray' => '808080', |
||
75 | 'green' => '008000', |
||
76 | 'greenyellow' => 'adff2f', |
||
77 | 'grey' => '808080', |
||
78 | 'honeydew' => 'f0fff0', |
||
79 | 'hotpink' => 'ff69b4', |
||
80 | 'indianred' => 'cd5c5c', |
||
81 | 'indigo' => '4b0082', |
||
82 | 'ivory' => 'fffff0', |
||
83 | 'khaki' => 'f0e68c', |
||
84 | 'lavender' => 'e6e6fa', |
||
85 | 'lavenderblush' => 'fff0f5', |
||
86 | 'lawngreen' => '7cfc00', |
||
87 | 'lemonchiffon' => 'fffacd', |
||
88 | 'lightblue' => 'add8e6', |
||
89 | 'lightcoral' => 'f08080', |
||
90 | 'lightcyan' => 'e0ffff', |
||
91 | 'lightgoldenrodyellow' => 'fafad2', |
||
92 | 'lightgray' => 'd3d3d3', |
||
93 | 'lightgreen' => '90ee90', |
||
94 | 'lightgrey' => 'd3d3d3', |
||
95 | 'lightpink' => 'ffb6c1', |
||
96 | 'lightsalmon' => 'ffa07a', |
||
97 | 'lightseagreen' => '20b2aa', |
||
98 | 'lightskyblue' => '87cefa', |
||
99 | 'lightslategray' => '778899', |
||
100 | 'lightslategrey' => '778899', |
||
101 | 'lightsteelblue' => 'b0c4de', |
||
102 | 'lightyellow' => 'ffffe0', |
||
103 | 'lime' => '00ff00', |
||
104 | 'limegreen' => '32cd32', |
||
105 | 'linen' => 'faf0e6', |
||
106 | 'magenta' => 'ff00ff', |
||
107 | 'maroon' => '800000', |
||
108 | 'mediumaquamarine' => '66cdaa', |
||
109 | 'mediumblue' => '0000cd', |
||
110 | 'mediumorchid' => 'ba55d3', |
||
111 | 'mediumpurple' => '9370db', |
||
112 | 'mediumseagreen' => '3cb371', |
||
113 | 'mediumslateblue' => '7b68ee', |
||
114 | 'mediumspringgreen' => '00fa9a', |
||
115 | 'mediumturquoise' => '48d1cc', |
||
116 | 'mediumvioletred' => 'c71585', |
||
117 | 'midnightblue' => '191970', |
||
118 | 'mintcream' => 'f5fffa', |
||
119 | 'mistyrose' => 'ffe4e1', |
||
120 | 'moccasin' => 'ffe4b5', |
||
121 | 'navajowhite' => 'ffdead', |
||
122 | 'navy' => '000080', |
||
123 | 'oldlace' => 'fdf5e6', |
||
124 | 'olive' => '808000', |
||
125 | 'olivedrab' => '6b8e23', |
||
126 | 'orange' => 'ffa500', |
||
127 | 'orangered' => 'ff4500', |
||
128 | 'orchid' => 'da70d6', |
||
129 | 'palegoldenrod' => 'eee8aa', |
||
130 | 'palegreen' => '98fb98', |
||
131 | 'paleturquoise' => 'afeeee', |
||
132 | 'palevioletred' => 'db7093', |
||
133 | 'papayawhip' => 'ffefd5', |
||
134 | 'peachpuff' => 'ffdab9', |
||
135 | 'peru' => 'cd853f', |
||
136 | 'pink' => 'ffc0cb', |
||
137 | 'plum' => 'dda0dd', |
||
138 | 'powderblue' => 'b0e0e6', |
||
139 | 'purple' => '800080', |
||
140 | 'rebeccapurple' => '663399', |
||
141 | 'red' => 'ff0000', |
||
142 | 'rosybrown' => 'bc8f8f', |
||
143 | 'royalblue' => '4169e1', |
||
144 | 'saddlebrown' => '8b4513', |
||
145 | 'salmon' => 'fa8072', |
||
146 | 'sandybrown' => 'f4a460', |
||
147 | 'seagreen' => '2e8b57', |
||
148 | 'seashell' => 'fff5ee', |
||
149 | 'sienna' => 'a0522d', |
||
150 | 'silver' => 'c0c0c0', |
||
151 | 'skyblue' => '87ceeb', |
||
152 | 'slateblue' => '6a5acd', |
||
153 | 'slategray' => '708090', |
||
154 | 'slategrey' => '708090', |
||
155 | 'snow' => 'fffafa', |
||
156 | 'springgreen' => '00ff7f', |
||
157 | 'steelblue' => '4682b4', |
||
158 | 'tan' => 'd2b48c', |
||
159 | 'teal' => '008080', |
||
160 | 'thistle' => 'd8bfd8', |
||
161 | 'tomato' => 'ff6347', |
||
162 | 'turquoise' => '40e0d0', |
||
163 | 'violet' => 'ee82ee', |
||
164 | 'wheat' => 'f5deb3', |
||
165 | 'white' => 'ffffff', |
||
166 | 'whitesmoke' => 'f5f5f5', |
||
167 | 'yellow' => 'ffff00', |
||
168 | 'yellowgreen' => '9acd32', |
||
169 | ); |
||
170 | |||
171 | public $r = 0; |
||
172 | public $g = 0; |
||
173 | public $b = 0; |
||
174 | public $a = 1; |
||
175 | public $variant = null; |
||
176 | public $implicit_label = true; |
||
177 | public $hints = array('color'); |
||
178 | |||
179 | public function getColor($variant = null) |
||
231 | |||
232 | public function __construct($value) |
||
239 | |||
240 | public function hasAlpha($variant = null) |
||
258 | |||
259 | protected function setValues($value) |
||
388 | |||
389 | /** |
||
390 | * Turns HSL color to RGB. Black magic. |
||
391 | * |
||
392 | * @param float $h Hue |
||
393 | * @param float $s Saturation |
||
394 | * @param float $l Lightness |
||
395 | * |
||
396 | * @return array RGB array |
||
397 | */ |
||
398 | public static function hslToRgb($h, $s, $l) |
||
425 | |||
426 | /** |
||
427 | * Helper function for hslToRgb. Even blacker magic. |
||
428 | * |
||
429 | * @return float Color value |
||
430 | */ |
||
431 | private static function hueToRgb($m1, $m2, $hue) |
||
446 | |||
447 | /** |
||
448 | * Converts RGB to HSL. Color inversion of previous black magic is white magic? |
||
449 | * |
||
450 | * @param float $red Red |
||
451 | * @param float $green Green |
||
452 | * @param float $blue Blue |
||
453 | * |
||
454 | * @return array HSL array |
||
455 | */ |
||
456 | public static function rgbToHsl($red, $green, $blue) |
||
502 | } |
||
503 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.