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 StandardVariableProvider 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 StandardVariableProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class StandardVariableProvider implements VariableProviderInterface |
||
13 | { |
||
14 | const ACCESSOR_ARRAY = 'array'; |
||
15 | const ACCESSOR_GETTER = 'getter'; |
||
16 | const ACCESSOR_ASSERTER = 'asserter'; |
||
17 | const ACCESSOR_PUBLICPROPERTY = 'public'; |
||
18 | |||
19 | /** |
||
20 | * Variables stored in context |
||
21 | * |
||
22 | * @var mixed |
||
23 | */ |
||
24 | protected $variables = []; |
||
25 | |||
26 | /** |
||
27 | * Internal array of ['className' => ['getFoo', 'getBar'], ...] |
||
28 | * storing all names of getter methods for a given class. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected static $gettersByClassName = []; |
||
33 | |||
34 | /** |
||
35 | * Variables, if any, with which to initialize this |
||
36 | * VariableProvider. |
||
37 | * |
||
38 | * @param array $variables |
||
39 | */ |
||
40 | public function __construct(array $variables = []) |
||
44 | |||
45 | /** |
||
46 | * @param array|\ArrayAccess $variables |
||
47 | * @return VariableProviderInterface |
||
48 | */ |
||
49 | public function getScopeCopy($variables) |
||
57 | |||
58 | /** |
||
59 | * Set the source data used by this VariableProvider. The |
||
60 | * source can be any type, but the type must of course be |
||
61 | * supported by the VariableProvider itself. |
||
62 | * |
||
63 | * @param mixed $source |
||
64 | * @return void |
||
65 | */ |
||
66 | public function setSource($source) |
||
70 | |||
71 | /** |
||
72 | * @return mixed |
||
73 | */ |
||
74 | public function getSource() |
||
78 | |||
79 | /** |
||
80 | * Get every variable provisioned by the VariableProvider |
||
81 | * implementing the interface. Must return an array or |
||
82 | * ArrayAccess instance! |
||
83 | * |
||
84 | * @return array|\ArrayAccess |
||
85 | */ |
||
86 | public function getAll() |
||
90 | |||
91 | /** |
||
92 | * Add a variable to the context |
||
93 | * |
||
94 | * @param string $identifier Identifier of the variable to add |
||
95 | * @param mixed $value The variable's value |
||
96 | * @return void |
||
97 | * @api |
||
98 | */ |
||
99 | public function add($identifier, $value) |
||
103 | |||
104 | /** |
||
105 | * Get a variable from the context. Throws exception if variable is not found in context. |
||
106 | * |
||
107 | * If "_all" is given as identifier, all variables are returned in an array, |
||
108 | * if one of the other reserved variables are given, their appropriate value |
||
109 | * they're representing is returned. |
||
110 | * |
||
111 | * @param string $identifier |
||
112 | * @return mixed The variable value identified by $identifier |
||
113 | * @api |
||
114 | */ |
||
115 | public function get($identifier) |
||
119 | |||
120 | /** |
||
121 | * Get a variable by dotted path expression, retrieving the |
||
122 | * variable from nested arrays/objects one segment at a time. |
||
123 | * If the second variable is passed, it is expected to contain |
||
124 | * extraction method names (constants from VariableExtractor) |
||
125 | * which indicate how each value is extracted. |
||
126 | * |
||
127 | * @param string $path |
||
128 | * @param array $accessors Optional list of accessors (see class constants) |
||
129 | * @return mixed |
||
130 | */ |
||
131 | public function getByPath($path, array $accessors = []) |
||
143 | |||
144 | /** |
||
145 | * Remove a variable from context. Throws exception if variable is not found in context. |
||
146 | * |
||
147 | * @param string $identifier The identifier to remove |
||
148 | * @return void |
||
149 | * @api |
||
150 | */ |
||
151 | public function remove($identifier) |
||
157 | |||
158 | /** |
||
159 | * Returns an array of all identifiers available in the context. |
||
160 | * |
||
161 | * @return array Array of identifier strings |
||
162 | */ |
||
163 | public function getAllIdentifiers() |
||
167 | |||
168 | /** |
||
169 | * Checks if this property exists in the VariableContainer. |
||
170 | * |
||
171 | * @param string $identifier |
||
172 | * @return boolean TRUE if $identifier exists, FALSE otherwise |
||
173 | * @api |
||
174 | */ |
||
175 | public function exists($identifier) |
||
179 | |||
180 | /** |
||
181 | * Clean up for serializing. |
||
182 | * |
||
183 | * @return string[] |
||
184 | */ |
||
185 | public function __sleep() |
||
189 | |||
190 | /** |
||
191 | * Adds a variable to the context. |
||
192 | * |
||
193 | * @param string $identifier Identifier of the variable to add |
||
194 | * @param mixed $value The variable's value |
||
195 | * @return void |
||
196 | */ |
||
197 | public function offsetSet($identifier, $value) |
||
201 | |||
202 | /** |
||
203 | * Remove a variable from context. Throws exception if variable is not found in context. |
||
204 | * |
||
205 | * @param string $identifier The identifier to remove |
||
206 | * @return void |
||
207 | */ |
||
208 | public function offsetUnset($identifier) |
||
212 | |||
213 | /** |
||
214 | * Checks if this property exists in the VariableContainer. |
||
215 | * |
||
216 | * @param string $identifier |
||
217 | * @return boolean TRUE if $identifier exists, FALSE otherwise |
||
218 | */ |
||
219 | public function offsetExists($identifier) |
||
223 | |||
224 | /** |
||
225 | * Get a variable from the context. Throws exception if variable is not found in context. |
||
226 | * |
||
227 | * @param string $identifier |
||
228 | * @return mixed The variable identified by $identifier |
||
229 | */ |
||
230 | public function offsetGet($identifier) |
||
234 | |||
235 | /** |
||
236 | * @param string $propertyPath |
||
237 | * @return array |
||
238 | */ |
||
239 | View Code Duplication | public function getAccessorsForPath($propertyPath) |
|
258 | |||
259 | /** |
||
260 | * @param string $propertyPath |
||
261 | * @return string |
||
262 | */ |
||
263 | View Code Duplication | protected function resolveSubVariableReferences($propertyPath) |
|
274 | |||
275 | /** |
||
276 | * Extracts a single value from an array or object. |
||
277 | * |
||
278 | * @param mixed $subject |
||
279 | * @param string $propertyName |
||
280 | * @param string|null $accessor |
||
281 | * @return mixed |
||
282 | */ |
||
283 | View Code Duplication | protected function extractSingleValue($subject, $propertyName, $accessor = null) |
|
290 | |||
291 | /** |
||
292 | * Returns TRUE if the data type of $subject is potentially compatible |
||
293 | * with the $accessor. |
||
294 | * |
||
295 | * @param mixed $subject |
||
296 | * @param string $propertyName |
||
297 | * @param string $accessor |
||
298 | * @return boolean |
||
299 | */ |
||
300 | protected function canExtractWithAccessor($subject, $propertyName, $accessor) |
||
319 | |||
320 | /** |
||
321 | * @param mixed $subject |
||
322 | * @param string $propertyName |
||
323 | * @param string $accessor |
||
324 | * @return mixed |
||
325 | */ |
||
326 | View Code Duplication | protected function extractWithAccessor($subject, $propertyName, $accessor) |
|
343 | |||
344 | /** |
||
345 | * Detect which type of accessor to use when extracting |
||
346 | * $propertyName from $subject. |
||
347 | * |
||
348 | * @param mixed $subject |
||
349 | * @param string $propertyName |
||
350 | * @return string|NULL |
||
351 | */ |
||
352 | protected function detectAccessor($subject, $propertyName) |
||
377 | |||
378 | /** |
||
379 | * Tests whether a property can be extracted through `is*` or `has*` methods. |
||
380 | * |
||
381 | * @param mixed $subject |
||
382 | * @param string $propertyName |
||
383 | * @return bool |
||
384 | */ |
||
385 | protected function isExtractableThroughAsserter($subject, $propertyName) |
||
391 | |||
392 | /** |
||
393 | * Extracts a property through `is*` or `has*` methods. |
||
394 | * |
||
395 | * @param object $subject |
||
396 | * @param string $propertyName |
||
397 | * @return mixed |
||
398 | */ |
||
399 | protected function extractThroughAsserter($subject, $propertyName) |
||
409 | |||
410 | /** |
||
411 | * Studies an object and fills the static internal cache of getter method awareness. |
||
412 | * The method must be called when detecting an accessor for an object and has a very |
||
413 | * particular reason for existing: |
||
414 | * |
||
415 | * - method_exists() will return TRUE even for protected methods |
||
416 | * - is_callable() will not, but is far too greedy and returns TRUE for *any* method |
||
417 | * name if __call is defined on the class. |
||
418 | * - disregarding Reflection which is horribly slow, get_class_methods is the only |
||
419 | * method which returns a list of public methods. |
||
420 | * - but repeatedly calling get_class_methods() *AND* in_array() is not a sensible |
||
421 | * solution. |
||
422 | * - therefore, an internal cached array is built by class name which allows a null- |
||
423 | * coalesce expression to determine if a getter method exists *AND* is public *AND* |
||
424 | * is not going to be handled by __call. |
||
425 | * |
||
426 | * For ease of use (to avoid repeating get_class calls) the method returns the class |
||
427 | * name of the instance as returned by get_class($instance). |
||
428 | * |
||
429 | * @param object $instance |
||
430 | * @return string |
||
431 | */ |
||
432 | protected function populateGettersByClassName($instance) |
||
441 | } |
||
442 |
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.