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 | * Animation DOM attributes array |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $attrs; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructor |
||
| 40 | * |
||
| 41 | * @param string $id |
||
| 42 | */ |
||
| 43 | 5 | public function __construct(string $id = 'untitled') |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Attribute to animate |
||
| 50 | * |
||
| 51 | * Attribute to animate. To specify a component attribute, use componentName.property syntax (e.g., |
||
| 52 | * light.intensity). |
||
| 53 | * |
||
| 54 | * @param string $attr |
||
| 55 | * @return AnimationInterface |
||
| 56 | */ |
||
| 57 | 4 | public function attribute(string $attr = 'rotation'): AnimationInterface |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Delay (in milliseconds) |
||
| 65 | * |
||
| 66 | * Delay (in milliseconds) or event name to wait on before beginning animation |
||
| 67 | * |
||
| 68 | * @param int|string $ms |
||
|
|
|||
| 69 | * @return AnimationInterface |
||
| 70 | */ |
||
| 71 | 4 | public function begin($ms = 0): AnimationInterface |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Direction of the animation |
||
| 79 | * |
||
| 80 | * Direction of the animation (between from and to). One of alternate, alternateReverse, normal, reverse. |
||
| 81 | * |
||
| 82 | * @param string $direction |
||
| 83 | * @return AnimationInterface |
||
| 84 | */ |
||
| 85 | 1 | public function direction(string $direction = 'normal'): AnimationInterface |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Duration in (milliseconds) |
||
| 93 | * |
||
| 94 | * Duration in (milliseconds) of the animation. |
||
| 95 | * |
||
| 96 | * @param int $ms |
||
| 97 | * @return AnimationInterface |
||
| 98 | */ |
||
| 99 | 4 | public function dur(int $ms = 1000): AnimationInterface |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Easing function |
||
| 107 | * |
||
| 108 | * Easing function of the animation. There are very many to choose from. |
||
| 109 | * |
||
| 110 | * @param string $func |
||
| 111 | * @return AnimationInterface |
||
| 112 | */ |
||
| 113 | 4 | public function easing(string $func = 'ease'): AnimationInterface |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Determines effect of animation when not actively in play |
||
| 121 | * |
||
| 122 | * One of backwards, both, forwards, none. |
||
| 123 | * |
||
| 124 | * @param string $effect |
||
| 125 | * @return AnimationInterface |
||
| 126 | */ |
||
| 127 | 4 | public function fill(string $effect = 'forwards'): AnimationInterface |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Starting value. |
||
| 135 | * |
||
| 136 | * @param string $val |
||
| 137 | * @return AnimationInterface |
||
| 138 | */ |
||
| 139 | 4 | public function from(string $val = 'Current'): AnimationInterface |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Repeat count or indefinite. |
||
| 147 | * |
||
| 148 | * @param int $count |
||
| 149 | * @return AnimationInterface |
||
| 150 | */ |
||
| 151 | 1 | public function repeat(int $count = 0): AnimationInterface |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Ending value. |
||
| 159 | * Must be specified. |
||
| 160 | * |
||
| 161 | * @param string $val |
||
| 162 | * @return AnimationInterface |
||
| 163 | */ |
||
| 164 | 4 | public function to(string $val = 'true'): AnimationInterface |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Create and add DOM element of the entity |
||
| 172 | * |
||
| 173 | * @param \DOMDocument $aframe_dom |
||
| 174 | * @return \DOMElement |
||
| 175 | */ |
||
| 176 | 2 | public function domElement(\DOMDocument &$aframe_dom): DOMElement |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Append DOM attributes no set by components |
||
| 185 | * |
||
| 186 | * @param \DOMElement $a_entity |
||
| 187 | */ |
||
| 188 | 2 | private function appendAttributes(\DOMElement &$a_entity) |
|
| 194 | |||
| 195 | 2 | View Code Duplication | private function setAttribute(&$a_entity, $attribute, $val) |
| 202 | |||
| 203 | } |
||
| 204 |