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 |
||
36 | View Code Duplication | class Rotation implements ComponentInterface |
|
|
|||
37 | { |
||
38 | |||
39 | /** |
||
40 | * Roll, rotation about the X-axis. |
||
41 | * |
||
42 | * @var integer|double $x |
||
43 | */ |
||
44 | protected $x; |
||
45 | |||
46 | /** |
||
47 | * Pitch, rotation about the Y-axis. |
||
48 | * |
||
49 | * @var integer|double $y |
||
50 | */ |
||
51 | protected $y; |
||
52 | |||
53 | /** |
||
54 | * Yaw, rotation about the Z-axis. |
||
55 | * |
||
56 | * @var integer|double $z |
||
57 | */ |
||
58 | protected $z; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param integer|double $x |
||
64 | * @param integer|double $y |
||
65 | * @param integer|double $z |
||
66 | */ |
||
67 | 51 | public function __construct(float $x = 0, float $y = 0, float $z = 0) |
|
71 | |||
72 | /** |
||
73 | * Get Component scripts |
||
74 | * |
||
75 | * {@inheritdoc} |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | 1 | public function getScripts(): array |
|
80 | { |
||
81 | 1 | return array(); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Does component have DOM Atributes |
||
86 | * |
||
87 | * {@inheritdoc} |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | 5 | public function hasDOMAttributes(): bool |
|
95 | |||
96 | /** |
||
97 | * Remove default DOMElement Attributes which are not required |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | 5 | public function removeDefaultDOMAttributes() |
|
102 | { |
||
103 | 5 | if (empty($this->x) && empty($this->y) && empty($this->z)) { |
|
104 | 4 | unset($this->x); |
|
105 | 4 | unset($this->y); |
|
106 | 4 | unset($this->z); |
|
107 | } |
||
108 | 5 | } |
|
109 | |||
110 | /** |
||
111 | * Get DOMAttr for the entity |
||
112 | * |
||
113 | * @return DOMAttr |
||
114 | */ |
||
115 | 1 | public function getDOMAttributes(): DOMAttr |
|
119 | |||
120 | /** |
||
121 | * Update rotation |
||
122 | * |
||
123 | * A-Frame uses a right-handed coordinate system. When aligning our right hand’s thumb with a positive axis, |
||
124 | * our hand will curl in the positive direction of rotation. |
||
125 | * |
||
126 | * @param integer|double $x |
||
127 | * @param integer|double $y |
||
128 | * @param integer|double $z |
||
129 | */ |
||
130 | 51 | public function update(float $x = 0, float $y = 0, float $z = 0) |
|
136 | } |
||
137 |
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.