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 |
||
15 | class hsl implements \ArrayAccess { |
||
16 | |||
17 | /** |
||
18 | * The HSL data |
||
19 | * @var array |
||
20 | */ |
||
21 | private $hsl; |
||
22 | |||
23 | /** |
||
24 | * The amount of accuracy to use when calculating HSL |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $accuracy; |
||
28 | |||
29 | /** |
||
30 | * Import a color as an HSL value |
||
31 | * |
||
32 | * @param array $rgb_array RGB color array to import |
||
33 | * @param int $accuracy The amount of accuracy to use when calculating HSL |
||
34 | */ |
||
35 | 13 | public function __construct(array $rgb_array, int $accuracy = 3) { |
|
39 | |||
40 | /** |
||
41 | * Shortcut for return the HSL array |
||
42 | * |
||
43 | * @return array The HSL array |
||
44 | */ |
||
45 | 3 | public function __invoke() { |
|
48 | |||
49 | /** |
||
50 | * Check if offset exists in data array |
||
51 | * |
||
52 | * @param string $offset The offset to check |
||
53 | * @return bool TRUE if the offset exist, FALSE otherwise |
||
54 | */ |
||
55 | 4 | public function offsetExists($offset) :bool { |
|
58 | |||
59 | /** |
||
60 | * Get an offset if it exists |
||
61 | * |
||
62 | * @param string $offset The offset to get |
||
63 | * @return mixed The value of the offset |
||
64 | */ |
||
65 | 2 | View Code Duplication | public function offsetGet($offset) { |
75 | |||
76 | /** |
||
77 | * Set a value in the data array |
||
78 | * |
||
79 | * @param string $offset The offset to set |
||
80 | * @param mixed $value The value to set it to |
||
81 | * @return mixed The result |
||
82 | */ |
||
83 | 2 | View Code Duplication | public function offsetSet($offset, $value) { |
93 | |||
94 | /** |
||
95 | * Reset an offset to 0 |
||
96 | * |
||
97 | * @param string $offset The offset to unset |
||
98 | * @return mixed The result |
||
99 | */ |
||
100 | 1 | public function offsetUnset($offset) { |
|
105 | } |
||
106 |
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.