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 |
||
29 | final class Animation implements AnimationInterface |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * Constructor |
||
34 | * |
||
35 | * @param string $id |
||
36 | */ |
||
37 | 5 | public function __construct(string $id = 'untitled') |
|
41 | |||
42 | /** |
||
43 | * Attribute to animate |
||
44 | * |
||
45 | * Attribute to animate. To specify a component attribute, use componentName.property syntax (e.g., |
||
46 | * light.intensity). |
||
47 | * |
||
48 | * @param string $attr |
||
49 | * @return AnimationInterface |
||
50 | */ |
||
51 | 4 | public function attribute(string $attr = 'rotation'): AnimationInterface |
|
56 | |||
57 | /** |
||
58 | * Delay (in milliseconds) |
||
59 | * |
||
60 | * Delay (in milliseconds) or event name to wait on before beginning animation |
||
61 | * |
||
62 | * @param mixed $ms |
||
63 | * @return AnimationInterface |
||
64 | */ |
||
65 | 4 | public function begin($ms = 0): AnimationInterface |
|
70 | |||
71 | /** |
||
72 | * Direction of the animation |
||
73 | * |
||
74 | * Direction of the animation (between from and to). One of alternate, alternateReverse, normal, reverse. |
||
75 | * |
||
76 | * @param string $direction |
||
77 | * @return AnimationInterface |
||
78 | */ |
||
79 | 1 | public function direction(string $direction = 'normal'): AnimationInterface |
|
84 | |||
85 | /** |
||
86 | * Duration in (milliseconds) |
||
87 | * |
||
88 | * Duration in (milliseconds) of the animation. |
||
89 | * |
||
90 | * @param int $ms |
||
91 | * @return AnimationInterface |
||
92 | */ |
||
93 | 4 | public function dur(int $ms = 1000): AnimationInterface |
|
98 | |||
99 | /** |
||
100 | * Easing function |
||
101 | * |
||
102 | * Easing function of the animation. There are very many to choose from. |
||
103 | * |
||
104 | * @param string $func |
||
105 | * @return AnimationInterface |
||
106 | */ |
||
107 | 4 | public function easing(string $func = 'ease'): AnimationInterface |
|
112 | |||
113 | /** |
||
114 | * Determines effect of animation when not actively in play |
||
115 | * |
||
116 | * One of backwards, both, forwards, none. |
||
117 | * |
||
118 | * @param string $effect |
||
119 | * @return AnimationInterface |
||
120 | */ |
||
121 | 4 | public function fill(string $effect = 'forwards'): AnimationInterface |
|
126 | |||
127 | /** |
||
128 | * Starting value. |
||
129 | * |
||
130 | * @param string $val |
||
131 | * @return AnimationInterface |
||
132 | */ |
||
133 | 4 | public function from(string $val = 'Current'): AnimationInterface |
|
138 | |||
139 | /** |
||
140 | * Repeat count or indefinite. |
||
141 | * |
||
142 | * @param int $count |
||
143 | * @return AnimationInterface |
||
144 | */ |
||
145 | 1 | public function repeat(int $count = 0): AnimationInterface |
|
150 | |||
151 | /** |
||
152 | * Ending value. |
||
153 | * Must be specified. |
||
154 | * |
||
155 | * @param string $val |
||
156 | * @return AnimationInterface |
||
157 | */ |
||
158 | 4 | public function to(string $val = 'true'): AnimationInterface |
|
163 | |||
164 | /** |
||
165 | * Create and add DOM element of the entity |
||
166 | * |
||
167 | * @param \DOMDocument $aframe_dom |
||
168 | * @return \DOMElement |
||
169 | */ |
||
170 | 2 | public function domElement(\DOMDocument &$aframe_dom): DOMElement |
|
176 | |||
177 | /** |
||
178 | * Append DOM attributes no set by components |
||
179 | * |
||
180 | * @param \DOMElement $a_entity |
||
181 | */ |
||
182 | 2 | View Code Duplication | private function appendAttributes(\DOMElement &$a_entity) |
190 | } |
||
191 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: