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:
Complex classes like Variables often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Variables, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Variables implements \ArrayAccess |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $variables = array(); |
||
34 | |||
35 | /** |
||
36 | * @var Variables |
||
37 | */ |
||
38 | private $parent; |
||
39 | |||
40 | /** |
||
41 | * @var Variables[] |
||
42 | */ |
||
43 | private $children = array(); |
||
44 | |||
45 | /** |
||
46 | * Variables constructor. |
||
47 | * |
||
48 | * @param Variables|null $parent Parent variables container |
||
49 | * @param array $defaults Array with default values |
||
50 | */ |
||
51 | public function __construct(Variables $parent = null, array $defaults = array()) |
||
72 | |||
73 | /** |
||
74 | * Clone the children with this step as well, cause otherwise they'll lose |
||
75 | * connection to parent |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | function __clone() |
||
88 | |||
89 | /** |
||
90 | * Bind to another parent |
||
91 | * |
||
92 | * @param Variables $newParent The new parent |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | public function bindTo(Variables $newParent) |
||
109 | |||
110 | /** |
||
111 | * Get the parent variables object, if any |
||
112 | * |
||
113 | * @return Variables |
||
114 | */ |
||
115 | public function getParent() |
||
119 | |||
120 | /** |
||
121 | * Provide an array of variable configurations: |
||
122 | * - Keys are the variable names |
||
123 | * - Values can be |
||
124 | * -- an array with the configuration - following keys are interpreted: |
||
125 | * --- required (defaults to false) |
||
126 | * --- default (defaults to null, ignored when required) |
||
127 | * -- null: The default value will be set to null |
||
128 | * -- false: No default value will be set |
||
129 | * |
||
130 | * Also you can add a "--" with numeric key, to state the boundaries of |
||
131 | * current and parent variables configuration |
||
132 | * |
||
133 | * In either case variables in this configuration will always be fetched |
||
134 | * from and saved to the very scope of this variables object (this) |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | protected function configureVariables() |
||
142 | |||
143 | /** |
||
144 | * Get a variable from this very object (unexpanded) |
||
145 | * |
||
146 | * @param mixed $offset Variable name |
||
147 | * |
||
148 | * @internal Required set/get/has/remove in order to access entries of Variables |
||
149 | * without looking up parents. |
||
150 | * You can however override this to do your own logic on plain offsets |
||
151 | * (no dot paths as in set/get/has/remove). |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public function &offsetGet($offset) |
||
159 | |||
160 | /** |
||
161 | * Determine if a variable is available on this very object |
||
162 | * |
||
163 | * @param mixed $offset Variable name |
||
164 | * |
||
165 | * @internal See {@see Variables::offsetGet()} |
||
166 | * |
||
167 | * @return boolean |
||
168 | */ |
||
169 | public function offsetExists($offset) |
||
173 | |||
174 | /** |
||
175 | * Set a variable on this very object |
||
176 | * |
||
177 | * @param mixed $offset Variable name |
||
178 | * @param mixed $value The value |
||
179 | * |
||
180 | * @internal See {@see Variables::offsetGet()} |
||
181 | * |
||
182 | * @return void |
||
183 | */ |
||
184 | public function offsetSet($offset, $value) |
||
188 | |||
189 | /** |
||
190 | * Unset a variable from this very object |
||
191 | * |
||
192 | * @param mixed $offset Variable name |
||
193 | * |
||
194 | * @internal See {@see Variables::offsetGet()} |
||
195 | * |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function offsetUnset($offset) |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Find the first context that contains the first part of the variable |
||
206 | * (this will always return current context - configured variables as well) |
||
207 | * |
||
208 | * @param array $variableParts The path split by dot |
||
209 | * |
||
210 | * @return Variables |
||
211 | */ |
||
212 | private function findContext(&$variableParts) |
||
240 | |||
241 | /** |
||
242 | * Get an expanded variable by it's path (second argument can be a default value) |
||
243 | * |
||
244 | * @param string $name The variable name |
||
245 | * |
||
246 | * @final This method is not to be overwritten as logic is to complex to deal |
||
247 | * with overrides because callee detection might be introduced later on |
||
248 | * and the expansion logic of dot path's is mostly internal. |
||
249 | * If you need to intercept the variable handling rather override |
||
250 | * offsetSet, offsetGet, offsetExists or offsetUnset. |
||
251 | * |
||
252 | * @return mixed |
||
253 | */ |
||
254 | final public function get($name) |
||
285 | |||
286 | /** |
||
287 | * Determine if a variable path is available |
||
288 | * |
||
289 | * @param string $name The variable name |
||
290 | * |
||
291 | * @final See {@see Variables::get()} |
||
292 | * |
||
293 | * @return boolean |
||
294 | */ |
||
295 | final public function has($name) |
||
315 | |||
316 | /** |
||
317 | * Set a variable by it's path |
||
318 | * |
||
319 | * @param string $name The variable name |
||
320 | * @param mixed $value The value |
||
321 | * |
||
322 | * @final See {@see Variables::get()} |
||
323 | * |
||
324 | * @return $this |
||
325 | */ |
||
326 | final public function set($name, $value) |
||
363 | |||
364 | /** |
||
365 | * Unset a variable by it's path |
||
366 | * |
||
367 | * @param string $name Variable name |
||
368 | * |
||
369 | * @final See {@see Variables::get()} |
||
370 | * |
||
371 | * @return $this |
||
372 | */ |
||
373 | final public function remove($name) |
||
407 | |||
408 | /** |
||
409 | * Determine if a variable is an array or accessible as array and has the key |
||
410 | * |
||
411 | * This method is required as isset returns false on existing keys with null |
||
412 | * values and array_key_exists doesn't invoke {@see \ArrayAccess::offsetExists()} |
||
413 | * |
||
414 | * Use this BEFORE {@see Variables::propertyExists()} in according checks, as |
||
415 | * it will match {@see Variables} objects as well. This will likely speed things |
||
416 | * up a little but more importantly it will avoid that private or protected |
||
417 | * properties are detected by {@see Variables::propertyExists()}. |
||
418 | * |
||
419 | * @param array|\ArrayAccess $array Array |
||
420 | * @param string $key The key |
||
421 | * |
||
422 | * @return bool |
||
423 | */ |
||
424 | private function arrayKeyExists($array, $key) |
||
433 | |||
434 | /** |
||
435 | * Determine if a variable is an object and has a property |
||
436 | * |
||
437 | * This method is required as isset returns false on existing properties with |
||
438 | * null values and property_exists doesn't invoke {@see __isset()} |
||
439 | * |
||
440 | * @param object $object The object |
||
441 | * @param string $property The property |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | private function propertyExists($object, $property) |
||
457 | |||
458 | /** |
||
459 | * Set from array |
||
460 | * |
||
461 | * @param array $values Array with key value pairs |
||
462 | * |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function setFromArray($values) |
||
472 | |||
473 | /** |
||
474 | * Expand expressions within the $value |
||
475 | * |
||
476 | * @param mixed $value The value |
||
477 | * |
||
478 | * @return string|mixed |
||
479 | */ |
||
480 | public function expand($value) |
||
488 | |||
489 | /** |
||
490 | * Handles special variable names |
||
491 | * |
||
492 | * @param string $key Current part of the complete variable name |
||
493 | * @param mixed $value The value |
||
494 | * |
||
495 | * @return void |
||
496 | */ |
||
497 | private function filterSpecialNames(&$key, &$value) |
||
518 | } |
||
519 | ?> |
||
520 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.