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 |
||
37 | View Code Duplication | class Scale implements ComponentInterface |
|
|
|||
38 | { |
||
39 | |||
40 | /** |
||
41 | * Scaling factor in the X direction. |
||
42 | * |
||
43 | * @var integer|double $x |
||
44 | */ |
||
45 | protected $x; |
||
46 | |||
47 | /** |
||
48 | * Scaling factor in the Y direction. |
||
49 | * |
||
50 | * @var integer|double $y |
||
51 | */ |
||
52 | protected $y; |
||
53 | |||
54 | /** |
||
55 | * Scaling factor in the Z direction. |
||
56 | * |
||
57 | * @var integer|double $z |
||
58 | */ |
||
59 | protected $z; |
||
60 | |||
61 | /** |
||
62 | * Constructor |
||
63 | * |
||
64 | * @param integer|double $x |
||
65 | * @param integer|double $y |
||
66 | * @param integer|double $z |
||
67 | */ |
||
68 | 51 | public function __construct(float $x = 0, float $y = 0, float $z = 0) |
|
72 | |||
73 | /** |
||
74 | * Get Component scripts |
||
75 | * |
||
76 | * {@inheritdoc} |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | 1 | public function getScripts(): array |
|
84 | |||
85 | /** |
||
86 | * Does component have DOM Atributes |
||
87 | * |
||
88 | * {@inheritdoc} |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | 5 | public function hasDOMAttributes(): bool |
|
96 | |||
97 | /** |
||
98 | * Remove default DOMElement Attributes which are not required |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | 5 | public function removeDefaultDOMAttributes() |
|
110 | |||
111 | /** |
||
112 | * Get DOMAttr for the entity |
||
113 | * |
||
114 | * @return DOMAttr |
||
115 | */ |
||
116 | 1 | public function getDOMAttributes(): DOMAttr |
|
120 | |||
121 | /** |
||
122 | * Update scale |
||
123 | * |
||
124 | * If any of the scaling factors are set to 0, then A-Frame will |
||
125 | * assign instead an extremely small value such that things don’t break. |
||
126 | * |
||
127 | * @param integer|double $x |
||
128 | * @param integer|double $y |
||
129 | * @param integer|double $z |
||
130 | */ |
||
131 | 51 | public function update(float $x = 0, float $y = 0, float $z = 00) |
|
137 | } |
||
138 | |||
139 |
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.