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 Vars 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 Vars, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Vars extends AbstractResource |
||
32 | { |
||
33 | /** |
||
34 | * The base path for the Vars config and cache folders |
||
35 | * |
||
36 | * @var string $base_path |
||
37 | */ |
||
38 | private $base_path; |
||
39 | |||
40 | /** |
||
41 | * The cache object if the cache is wanted, else false |
||
42 | * |
||
43 | * @var \M1\Vars\Cache\CacheProvider $cache |
||
44 | */ |
||
45 | public $cache; |
||
46 | |||
47 | /** |
||
48 | * The available extensions |
||
49 | * |
||
50 | * @var array $extensions |
||
51 | */ |
||
52 | private $extensions = array(); |
||
53 | |||
54 | /** |
||
55 | * The default options for Vars |
||
56 | * |
||
57 | * @var array $default_options |
||
58 | */ |
||
59 | private $default_options = array( |
||
60 | 'base_path' => null, |
||
61 | 'cache' => true, |
||
62 | 'cache_path' => null, |
||
63 | 'cache_expire' => 300, // 5 minutes |
||
64 | 'loaders' => array('ini', 'json', 'php', 'toml', 'yaml', 'xml',) |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * The available loaders |
||
69 | * |
||
70 | * @var array $loaders |
||
71 | */ |
||
72 | private $loaders = array(); |
||
73 | |||
74 | /** |
||
75 | * Have the base and cache paths been set |
||
76 | * |
||
77 | * @var bool $paths_loaded |
||
78 | */ |
||
79 | private $paths_loaded = false; |
||
80 | |||
81 | /** |
||
82 | * The imported resources |
||
83 | * |
||
84 | * @var array $resources |
||
85 | */ |
||
86 | private $resources = array(); |
||
87 | |||
88 | /** |
||
89 | * The words to be replaced in the config files |
||
90 | * |
||
91 | * @var array $variables |
||
92 | */ |
||
93 | private $variables = array(); |
||
94 | |||
95 | /** |
||
96 | * Creates a new instance of Vars |
||
97 | * |
||
98 | * @param string|array $resource The main configuration resource |
||
99 | * @param array $options The options being used for Vars |
||
100 | */ |
||
101 | 61 | public function __construct($resource, $options = null) |
|
130 | |||
131 | /** |
||
132 | * Makes the CacheProvider with the options |
||
133 | * |
||
134 | * @param array $options The options being used for Vars |
||
135 | * @param array|string $resource The main configuration resource |
||
136 | */ |
||
137 | 61 | private function makeCache($options, $resource) |
|
142 | |||
143 | /** |
||
144 | * Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
||
145 | * base path has |
||
146 | * |
||
147 | * @param array $options The options being used for Vars |
||
148 | */ |
||
149 | 60 | private function makePaths($options) |
|
158 | |||
159 | /** |
||
160 | * Get loaders and make extensions for the loaders |
||
161 | * |
||
162 | * @param array|null $options The options being used for Vars |
||
163 | * |
||
164 | * @throws \InvalidArgumentException If a loader from options isn't found |
||
165 | * @throws \InvalidArgumentException If no loaders were loaded |
||
166 | */ |
||
167 | 59 | private function makeLoaders($options) |
|
212 | |||
213 | /** |
||
214 | * Sets the replacement variables if the option has been set |
||
215 | * |
||
216 | * @param array|null $options The options being used for Vars |
||
217 | */ |
||
218 | 57 | private function makeVariables($options) |
|
232 | |||
233 | /** |
||
234 | * Loads the cached file into the current class |
||
235 | */ |
||
236 | 2 | private function loadFromCache() |
|
259 | |||
260 | /** |
||
261 | * Get and make extensions for loaders made from makeLoaders() |
||
262 | * |
||
263 | * @see \M1\Vars\Vars::makeLoaders() \M1\Vars\Vars::makeLoaders() |
||
264 | * |
||
265 | * @param array $loaders File loaders |
||
266 | * |
||
267 | * @throws \RuntimeException If no loader extensions were found |
||
268 | * |
||
269 | * @return array File loader supported extensions |
||
270 | */ |
||
271 | 58 | private function makeExtensions(array $loaders) |
|
285 | |||
286 | /** |
||
287 | * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
||
288 | * |
||
289 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
290 | */ |
||
291 | 51 | public function pathsLoadedCheck($resource) |
|
309 | |||
310 | /** |
||
311 | * Get the Vars file loaders |
||
312 | * |
||
313 | * @return array The Vars file loaders |
||
314 | */ |
||
315 | 51 | public function getLoaders() |
|
319 | |||
320 | /** |
||
321 | * Get the Vars file loaders extensions |
||
322 | * |
||
323 | * @return array The Vars file loader extensions |
||
324 | */ |
||
325 | 4 | public function getExtensions() |
|
329 | |||
330 | /** |
||
331 | * Get the Vars base path |
||
332 | * |
||
333 | * @return string The Vars base path |
||
334 | */ |
||
335 | 51 | public function getBasePath() |
|
339 | |||
340 | /** |
||
341 | * Set the Vars base path |
||
342 | * |
||
343 | * @param string $base_path The base path to set |
||
344 | * |
||
345 | * @throws \InvalidArgumentException If the base path does not exist or is not writable |
||
346 | * |
||
347 | * @return \M1\Vars\Vars |
||
348 | */ |
||
349 | 60 | View Code Duplication | public function setBasePath($base_path) |
365 | |||
366 | /** |
||
367 | * Adds a resource to $this->resources |
||
368 | * |
||
369 | * @param string $resource Resource to add to the stack |
||
370 | * |
||
371 | * @return int The position of the added resource |
||
372 | */ |
||
373 | 51 | public function addResource($resource) |
|
380 | |||
381 | /** |
||
382 | * Updates the string resource with the FileResource |
||
383 | * |
||
384 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
385 | * @param int $pos The position of the string resource |
||
386 | * |
||
387 | * @return \M1\Vars\Vars |
||
388 | */ |
||
389 | 44 | public function updateResource($resource, $pos) |
|
394 | |||
395 | /** |
||
396 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
397 | * |
||
398 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
399 | * |
||
400 | * @return bool Has resource already been imported |
||
401 | */ |
||
402 | 51 | public function resourceImported($resource) |
|
413 | |||
414 | /** |
||
415 | * Searches the resource stack for a certain resource |
||
416 | * |
||
417 | * @param string $resource The resource to search for |
||
418 | * |
||
419 | * @return bool Returns the resource if found |
||
420 | */ |
||
421 | 3 | public function getResource($resource) |
|
431 | |||
432 | /** |
||
433 | * Returns the imported resources |
||
434 | * |
||
435 | * @return array The Vars imported resources |
||
436 | */ |
||
437 | 51 | public function getResources() |
|
441 | |||
442 | /** |
||
443 | * Returns the Vars replacement variables |
||
444 | * |
||
445 | * @return array The Vars replacement variables |
||
446 | */ |
||
447 | 40 | public function getVariables() |
|
451 | |||
452 | /** |
||
453 | * Returns the CacheProvider if set, if not return false |
||
454 | * |
||
455 | * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
||
456 | */ |
||
457 | 5 | public function getCache() |
|
461 | } |
||
462 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: