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 |
||
8 | class Dimmensions |
||
9 | { |
||
10 | protected static $positionsKeywordsX = [ |
||
11 | 'left' => '0%', |
||
12 | 'center' => '50%', |
||
13 | 'right' => '100%', |
||
14 | ]; |
||
15 | |||
16 | protected static $positionsKeywordsY = [ |
||
17 | 'top' => '0%', |
||
18 | 'middle' => '50%', |
||
19 | 'bottom' => '100%', |
||
20 | ]; |
||
21 | |||
22 | /** |
||
23 | * Calculate the dimensions of a resize. |
||
24 | * |
||
25 | * @param int $oldWidth |
||
26 | * @param int $oldHeight |
||
27 | * @param int $newWidth |
||
28 | * @param int $newHeight |
||
29 | * @param bool $cover |
||
30 | * |
||
31 | * @return array [width, height] |
||
32 | */ |
||
33 | public static function getResizeDimmensions($oldWidth, $oldHeight, $newWidth, $newHeight, $cover = false) |
||
74 | |||
75 | /** |
||
76 | * Calculate a dimension value. |
||
77 | * |
||
78 | * @param string $direction |
||
79 | * @param int|string $value |
||
80 | * @param int $relatedValue |
||
81 | * @param bool $position |
||
82 | * |
||
83 | * @return int |
||
84 | */ |
||
85 | public static function getIntegerValue($direction, $value, $relatedValue, $position = false) |
||
99 | |||
100 | /** |
||
101 | * Calculates the x/y position. |
||
102 | * |
||
103 | * @param string $direction (y or x) |
||
104 | * @param string|int|null $position |
||
105 | * @param int $newValue |
||
106 | * @param int $oldValue |
||
107 | * |
||
108 | * @return int |
||
109 | */ |
||
110 | public static function getPositionValue($direction, $position, $newValue, $oldValue) |
||
137 | } |
||
138 |
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.