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 |
||
23 | abstract class AbstractBuilder implements \ArrayAccess |
||
24 | { |
||
25 | /** |
||
26 | * A placeholder for constructor arguments. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32; |
||
31 | |||
32 | /** |
||
33 | * AbstractBuilder constructor. |
||
34 | * |
||
35 | * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException |
||
36 | */ |
||
37 | 15 | public function __construct() |
|
45 | |||
46 | /** |
||
47 | * Builds new building class instance from provided arguments. |
||
48 | * |
||
49 | * @return object |
||
50 | */ |
||
51 | 5 | public function build() |
|
56 | |||
57 | /** |
||
58 | * Set building class constructor arguments from array. |
||
59 | * |
||
60 | * @param array $values Values for constructor arguments of building class. |
||
61 | * @return AbstractBuilder $this Fluent interface |
||
62 | */ |
||
63 | 3 | public function fromArray(array $values) |
|
71 | |||
72 | /** |
||
73 | * Get all building class constructor arguments as array. |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | 1 | public function toArray(array $keys = []) |
|
89 | |||
90 | /** |
||
91 | * Set building class constructor argument. |
||
92 | * |
||
93 | * @param string $name Argument name. |
||
94 | * @param mixed $value Argument value. |
||
95 | * |
||
96 | * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException |
||
97 | */ |
||
98 | 6 | View Code Duplication | public function __set($name, $value) |
111 | |||
112 | /** |
||
113 | * Get building class constructor argument. |
||
114 | * |
||
115 | * @param string $name Argument name. |
||
116 | * @return mixed Argument value. |
||
117 | * |
||
118 | * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException |
||
119 | */ |
||
120 | 3 | View Code Duplication | public function __get($name) |
132 | |||
133 | /** |
||
134 | * Check if building class constructor argument is defined. |
||
135 | * |
||
136 | * @param string $name Argument name. |
||
137 | * @return bool TRUE if argument is defined. |
||
138 | */ |
||
139 | 1 | public function __isset($name) |
|
143 | |||
144 | /** |
||
145 | * Get/set building class constructor argument. |
||
146 | * |
||
147 | * @param string $name A method name. |
||
148 | * @param array $arguments A method arguments. |
||
149 | * |
||
150 | * @return $this|mixed Fluent interface or argument value, depending on method name. |
||
151 | * |
||
152 | * @throws \RunOpenCode\AbstractBuilder\Exception\BadMethodCallException |
||
153 | */ |
||
154 | 5 | public function __call($name, array $arguments) |
|
182 | |||
183 | /** |
||
184 | * Function call to builder object instance will produce building class. |
||
185 | * |
||
186 | * @return object |
||
187 | */ |
||
188 | 1 | public function __invoke() |
|
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | 3 | public function offsetExists($offset) |
|
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | 2 | public function offsetGet($offset) |
|
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | * |
||
216 | * @throws \RunOpenCode\AbstractBuilder\Exception\BadMethodCallException |
||
217 | */ |
||
218 | 3 | public function offsetSet($offset, $value) |
|
235 | |||
236 | /** |
||
237 | * Unused, throws an exception. |
||
238 | * |
||
239 | * @param mixed $offset |
||
240 | * |
||
241 | * @throws \RunOpenCode\AbstractBuilder\Exception\BadMethodCallException |
||
242 | */ |
||
243 | 1 | public function offsetUnset($offset) |
|
247 | |||
248 | /** |
||
249 | * Produces new builder. |
||
250 | * |
||
251 | * @return static |
||
252 | * |
||
253 | * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException |
||
254 | */ |
||
255 | 14 | public static function createBuilder() |
|
259 | |||
260 | /** |
||
261 | * Force get value from value storage, without consulting getter. |
||
262 | * |
||
263 | * Use this method to get raw value of parameter storage when creating getter method. |
||
264 | * |
||
265 | * @param string $name |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | 3 | View Code Duplication | protected function __doGet($name) |
277 | |||
278 | /** |
||
279 | * Force set value to value storage, without consulting setter. |
||
280 | * |
||
281 | * Use this method to set raw value of parameter storage when creating setter method. |
||
282 | * |
||
283 | * @param string $name |
||
284 | * @param mixed $value |
||
285 | * |
||
286 | * @return $this Fluent interface. |
||
287 | * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException |
||
288 | */ |
||
289 | 5 | View Code Duplication | protected function __doSet($name, $value) |
298 | |||
299 | /** |
||
300 | * Configure builder parameters that will be passed to building class constructor. |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | abstract protected function configureParameters(); |
||
305 | |||
306 | /** |
||
307 | * Get full qualified class name of class which instance ought to be constructed. |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | abstract protected function getObjectFqcn(); |
||
312 | } |
||
313 |
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.