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 SphereMethods |
|
|
|||
27 | { |
||
28 | |||
29 | /** |
||
30 | * The sphere primitive can define spheres in the traditional sense like a basketball. |
||
31 | * |
||
32 | * But it can also define various polyhedrons and abstract shapes given that it can specify |
||
33 | * the number of horizontal and vertical angles and faces. |
||
34 | * |
||
35 | * Sticking with a basic sphere, the default number of segments is high enough to make the sphere appear round. |
||
36 | */ |
||
37 | const DEFAULTS = array( |
||
38 | /* Radius of the sphere. */ |
||
39 | 'radius' => 1, |
||
40 | /* Number of horizontal segments. */ |
||
41 | 'segmentsWidth' => 18, |
||
42 | /* Number of vertical segments. */ |
||
43 | 'segmentsHeight' => 36, |
||
44 | /* Horizontal starting angle. */ |
||
45 | 'phiStart' => 0, |
||
46 | /* Horizontal sweep angle size. */ |
||
47 | 'phiLength' => 360, |
||
48 | /* Vertical starting angle. */ |
||
49 | 'thetaStart' => 0, |
||
50 | /* Vertical sweep angle size. */ |
||
51 | 'thetaLength' => 360 |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Radius of the sphere. |
||
56 | * |
||
57 | * @param &array $dom_attributes |
||
58 | * @param float|int $radius |
||
59 | * @return void |
||
60 | */ |
||
61 | 10 | public function radius(array &$dom_attributes, float $radius) |
|
65 | |||
66 | /** |
||
67 | * Number of vertical segments. |
||
68 | * |
||
69 | * @param &array $dom_attributes |
||
70 | * @param int $segmentsHeight |
||
71 | * @return void |
||
72 | */ |
||
73 | 10 | public function segmentsHeight(array &$dom_attributes, int $segmentsHeight) |
|
77 | |||
78 | /** |
||
79 | * Number of horizontal segments. |
||
80 | * |
||
81 | * @param &array $dom_attributes |
||
82 | * @param int $segmentsWidth |
||
83 | * @return void |
||
84 | */ |
||
85 | 10 | public function segmentsWidth(array &$dom_attributes, int $segmentsWidth) |
|
89 | |||
90 | /** |
||
91 | * Horizontal starting angle. |
||
92 | * |
||
93 | * @param &array $dom_attributes |
||
94 | * @param float|int $phiStart |
||
95 | * @return void |
||
96 | */ |
||
97 | 1 | public function phiStart(array &$dom_attributes, float $phiStart) |
|
101 | |||
102 | /** |
||
103 | * Horizontal sweep angle size. |
||
104 | * |
||
105 | * @param &array $dom_attributes |
||
106 | * @param float|int $phiLength |
||
107 | * @return void |
||
108 | */ |
||
109 | 1 | public function phiLength(array &$dom_attributes, float $phiLength = null) |
|
113 | |||
114 | /** |
||
115 | * Vertical starting angle. |
||
116 | * |
||
117 | * @param &array $dom_attributes |
||
118 | * @param float|int $thetaStart |
||
119 | * @return void |
||
120 | */ |
||
121 | 1 | public function thetaStart(array &$dom_attributes, float $thetaStart) |
|
125 | |||
126 | /** |
||
127 | * Vertical sweep angle size. |
||
128 | * |
||
129 | * @param &array $dom_attributes |
||
130 | * @param float|int $thetaLength |
||
131 | * @return void |
||
132 | */ |
||
133 | 1 | public function thetaLength(array &$dom_attributes, float $thetaLength) |
|
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.