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 |
||
26 | View Code Duplication | class CylinderMethods |
|
|
|||
27 | { |
||
28 | |||
29 | /** |
||
30 | * The cylinder primitive can define cylinders in the traditional sense like a Coca-Cola™ can, |
||
31 | * but it can also define shapes such as tubes and curved surfaces. |
||
32 | * We’ll go over some of these cylinder recipes below. |
||
33 | * 1. Traditional cylinders can be defined by using only a height and a radius: |
||
34 | * 2. Tubes can be defined by making the cylinder open-ended, which removes the top and bottom surfaces of the cylinder such that the inside is visible. |
||
35 | * A double-sided material will be needed to render properly: |
||
36 | * 3. Curved surfaces can be defined by specifying the angle via thetaLength such that the cylinder doesn’t curve all the way around, |
||
37 | * making the cylinder open-ended, and then making the material double-sided. |
||
38 | * 4. Other types of prisms can be defined by varying the number of radial segments (i.e., sides). For example, to make a hexagonal prism: |
||
39 | */ |
||
40 | const DEFAULTS = array( |
||
41 | /* Radius of the cylinder. */ |
||
42 | 'height' => 3, |
||
43 | /* Height of the cylinder. */ |
||
44 | 'radius' => 2, |
||
45 | /* Number of segmented faces around the circumference of the cylinder. */ |
||
46 | 'segmentsRadial' => 36, |
||
47 | /* Number of rows of faces along the height of the cylinder. */ |
||
48 | 'segmentsHeight' => 18, |
||
49 | /* Whether the ends of the cylinder are open (true) or capped ('false'). */ |
||
50 | 'openEnded' => 'false', |
||
51 | /* Starting angle in degrees. */ |
||
52 | 'thetaStart' => 0, |
||
53 | /* Central angle in degrees. */ |
||
54 | 'thetaLength' => 360 |
||
55 | ); |
||
56 | |||
57 | /** |
||
58 | * Height of the cylinder. |
||
59 | * |
||
60 | * @param &array $dom_attributes |
||
61 | * @param float|int $height |
||
62 | * @return void |
||
63 | */ |
||
64 | 6 | public function height(array &$dom_attributes, float $height) |
|
68 | |||
69 | /** |
||
70 | * Whether the ends of the cylinder are open (true) or capped (false). |
||
71 | * |
||
72 | * @param &array $dom_attributes |
||
73 | * @param bool|false $openEnded |
||
74 | * @return void |
||
75 | */ |
||
76 | 6 | public function openEnded(array &$dom_attributes, bool $openEnded = false) |
|
81 | |||
82 | /** |
||
83 | * Radius of the cylinder. |
||
84 | * |
||
85 | * @param &array $dom_attributes |
||
86 | * @param float|int $radius |
||
87 | * @return void |
||
88 | */ |
||
89 | 6 | public function radius(array &$dom_attributes, float $radius) |
|
93 | |||
94 | /** |
||
95 | * Number of rows of faces along the height of the cylinder. |
||
96 | * |
||
97 | * @param &array $dom_attributes |
||
98 | * @param int $segmentsHeight |
||
99 | * @return void |
||
100 | */ |
||
101 | 6 | public function segmentsHeight(array &$dom_attributes, int $segmentsHeight) |
|
105 | |||
106 | /** |
||
107 | * Central angle in degrees. |
||
108 | * |
||
109 | * @param &array $dom_attributes |
||
110 | * @param float|int $thetaLength |
||
111 | * @return void |
||
112 | */ |
||
113 | 6 | public function thetaLength(array &$dom_attributes, float $thetaLength) |
|
117 | |||
118 | /** |
||
119 | * Starting angle in degrees. |
||
120 | * |
||
121 | * @param &array $dom_attributes |
||
122 | * @param float|int $thetaStart |
||
123 | * @return void |
||
124 | */ |
||
125 | 6 | public function thetaStart(array &$dom_attributes, float $thetaStart) |
|
129 | |||
130 | /** |
||
131 | * Number of segmented faces around the circumference of the cylinder. |
||
132 | * |
||
133 | * @param &array $dom_attributes |
||
134 | * @param int $segmentsRadial |
||
135 | * @return void |
||
136 | */ |
||
137 | 1 | public function segmentsRadial(array &$dom_attributes, int $segmentsRadial) |
|
141 | } |
||
142 |
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.