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 |
||
21 | class store implements \Serializable, \JsonSerializable { |
||
22 | |||
23 | protected $color_spaces = array(); |
||
24 | |||
25 | /** |
||
26 | * All the valid color spaces types |
||
27 | * @var array |
||
28 | */ |
||
29 | protected static $valid_color_spaces = array( |
||
30 | 'rgb', |
||
31 | 'hsl', |
||
32 | 'hsb', |
||
33 | 'cmyk' |
||
34 | ); |
||
35 | |||
36 | /** |
||
37 | * All the valid importable types |
||
38 | * @var array |
||
39 | */ |
||
40 | protected static $valid_imports = array( |
||
41 | 'object', |
||
42 | 'hex', |
||
43 | 'int', |
||
44 | 'css' |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * All the valid types |
||
49 | * @var array |
||
50 | */ |
||
51 | protected static $valid_types = array( |
||
52 | 'rgb', |
||
53 | 'hsl', |
||
54 | 'hsb', |
||
55 | 'cmyk', |
||
56 | 'object', |
||
57 | 'hex', |
||
58 | 'int', |
||
59 | 'css' |
||
60 | ); |
||
61 | |||
62 | private $current_space = 'rgb'; |
||
63 | |||
64 | /** |
||
65 | * $the current colors alpha channel (Opacity) |
||
66 | * @var float |
||
67 | */ |
||
68 | protected $alpha = 100.0; |
||
69 | |||
70 | /** |
||
71 | * Takes an input color and imports it as its respective type. |
||
72 | * |
||
73 | * @param mixed $color An RGB (array), HSL (array), Hex (string), Integer (hex), or CMYK (array) representation of a color. |
||
74 | * @param string $type (optional) The type to try to import it as |
||
75 | */ |
||
76 | public function __construct($color, string $type = '') { |
||
98 | |||
99 | public function __get($key) { |
||
112 | |||
113 | public function __set($key, $value) { |
||
125 | |||
126 | public function __isset($key) { |
||
129 | |||
130 | public function __unset($key) { |
||
141 | |||
142 | protected static function _get_convert(array $color_spaces, string $current, string $to) { |
||
154 | |||
155 | public function clear_cache() { |
||
163 | |||
164 | protected function _clear() { |
||
167 | |||
168 | protected function get_space() { |
||
171 | |||
172 | protected function set_space(string $color_space) :string { |
||
178 | |||
179 | /** |
||
180 | * Serializes this object. |
||
181 | * |
||
182 | * @return string The serialized object |
||
183 | */ |
||
184 | public function serialize() :string { |
||
187 | |||
188 | /** |
||
189 | * Unserializes this object. |
||
190 | * |
||
191 | * @param string $serialized The object as a serialized string |
||
192 | * @return void |
||
193 | */ |
||
194 | public function unserialize($serialized) { |
||
199 | |||
200 | /** |
||
201 | * Serializes this object into JSON. |
||
202 | * |
||
203 | * @return string The serialized object |
||
204 | */ |
||
205 | public function jsonSerialize() :array { |
||
208 | |||
209 | /** |
||
210 | * Handles general errors when importing, and forces the input to be valid. |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | View Code Duplication | protected function import_error() { |
|
221 | |||
222 | /** |
||
223 | * Import the alpha channel from a color array, or create one if it doesn't exist |
||
224 | * |
||
225 | * @param array $color The color array to check |
||
226 | * @return void |
||
227 | */ |
||
228 | View Code Duplication | protected function import_alpha(array $color) { |
|
235 | |||
236 | /** |
||
237 | * Handles importing of another instance of color |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | protected function import_object(store $color) { |
||
250 | |||
251 | /** |
||
252 | * Imports a RGB array. |
||
253 | * |
||
254 | * @param array $color Array with offsets 'r', 'g', 'b' |
||
255 | * @return void |
||
256 | */ |
||
257 | public function import_rgb(array $color) { |
||
262 | |||
263 | /** |
||
264 | * Imports a hsl array. |
||
265 | * |
||
266 | * @param array $color Array with offsets 'h', 's', 'l' |
||
267 | * @return void |
||
268 | */ |
||
269 | public function import_hsl(array $color) { |
||
274 | |||
275 | /** |
||
276 | * Imports a hsb array. |
||
277 | * |
||
278 | * @param array $color Array with offsets 'h', 's', 'b' |
||
279 | * @return void |
||
280 | */ |
||
281 | public function import_hsb(array $color) { |
||
286 | |||
287 | /** |
||
288 | * Imports a CMYK array |
||
289 | * |
||
290 | * @param array $color Array with offsets 'c', 'm', 'y', 'k' |
||
291 | * @return void |
||
292 | */ |
||
293 | public function import_cmyk(array $color) { |
||
298 | |||
299 | /** |
||
300 | * Converts a hexadecimal string into an RGB array and imports it. |
||
301 | * |
||
302 | * @param string $color Hexadecimal string |
||
303 | * @return void |
||
304 | */ |
||
305 | public function import_hex(string $color) { |
||
308 | |||
309 | /** |
||
310 | * Converts a hexadecimal string into an RGB array and imports it. |
||
311 | * |
||
312 | * @param string $color Hexadecimal string |
||
313 | * @return void |
||
314 | */ |
||
315 | public function import_css(string $color) { |
||
319 | |||
320 | /** |
||
321 | * Converts an integer to a hexadecimal string and imports it. |
||
322 | * |
||
323 | * @param int $color An integer |
||
324 | * @return void |
||
325 | */ |
||
326 | public function import_int(int $color) { |
||
330 | } |
||
331 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.