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 |
||
10 | class Dimmensions |
||
11 | { |
||
12 | protected static $positionsKeywords = [ |
||
13 | 'top' => '0%', |
||
14 | 'left' => '0%', |
||
15 | 'middle' => '50%', |
||
16 | 'center' => '50%', |
||
17 | 'right' => '100%', |
||
18 | 'bottom' => '100%', |
||
19 | ]; |
||
20 | |||
21 | /** |
||
22 | * Calculate the dimensions of a resize. |
||
23 | * |
||
24 | * @param int $oldWidth |
||
25 | * @param int $oldHeight |
||
26 | * @param int $newWidth |
||
27 | * @param int $newHeight |
||
28 | * @param bool $cover |
||
29 | * |
||
30 | * @return array [width, height] |
||
31 | */ |
||
32 | public static function getResizeDimmensions($oldWidth, $oldHeight, $newWidth, $newHeight, $cover = false) |
||
73 | |||
74 | /** |
||
75 | * Calculate a dimension value. |
||
76 | * |
||
77 | * @param int|string $value |
||
78 | * @param int $relatedValue |
||
79 | * @param bool $position |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | public static function getIntegerValue($value, $relatedValue, $position = false) |
||
95 | |||
96 | /** |
||
97 | * Calculate a dimension value. |
||
98 | * |
||
99 | * @param int|string $value |
||
100 | * @param int $relatedValue |
||
101 | * @param bool $position |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public static function getPercentageValue($value, $relatedValue, $position = false) |
||
121 | |||
122 | /** |
||
123 | * Calculates the x/y position. |
||
124 | * |
||
125 | * @param string|int|null $position |
||
126 | * @param int $newValue |
||
127 | * @param int $oldValue |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | public static function getPositionValue($position, $newValue, $oldValue) |
||
142 | |||
143 | /** |
||
144 | * Calculates the x/y position. |
||
145 | * |
||
146 | * @param string|int|null $position |
||
147 | * @param int $original |
||
148 | * @param int $reference |
||
149 | * @param int $padding |
||
150 | * |
||
151 | * @return int |
||
152 | */ |
||
153 | public static function getPositionFromReferenceValue($position, $original, $reference, $padding = 0) |
||
173 | } |
||
174 |
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.