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 |
||
32 | class Vars extends AbstractResource |
||
33 | { |
||
34 | /** |
||
35 | * The base path for the Vars config and cache folders |
||
36 | * |
||
37 | * @var string $base_path |
||
38 | */ |
||
39 | private $base_path; |
||
40 | |||
41 | /** |
||
42 | * The cache object if the cache is wanted, else false |
||
43 | * |
||
44 | * @var \M1\Vars\Cache\CacheProvider $cache |
||
45 | */ |
||
46 | public $cache; |
||
47 | |||
48 | /** |
||
49 | * The default options for Vars |
||
50 | * |
||
51 | * @var array $default_options |
||
52 | */ |
||
53 | private $default_options = array( |
||
54 | 'base_path' => null, |
||
55 | 'cache' => true, |
||
56 | 'cache_path' => null, |
||
57 | 'cache_expire' => 300, // 5 minutes |
||
58 | 'loaders' => array('ini', 'json', 'php', 'toml', 'yaml', 'xml',) |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * The loaderProvider for Vars supplies the file loaders and the extensions that are supported |
||
63 | * |
||
64 | * @var \M1\Vars\Loader\LoaderProvider $loader |
||
65 | */ |
||
66 | public $loader; |
||
67 | |||
68 | /** |
||
69 | * Have the base and cache paths been set |
||
70 | * |
||
71 | * @var bool $paths_loaded |
||
72 | */ |
||
73 | private $paths_loaded = false; |
||
74 | |||
75 | /** |
||
76 | * The imported resources |
||
77 | * |
||
78 | * @var array $resources |
||
79 | */ |
||
80 | private $resources = array(); |
||
81 | |||
82 | /** |
||
83 | * The words to be replaced in the config files |
||
84 | * |
||
85 | * @var array $variables |
||
86 | */ |
||
87 | private $variables = array(); |
||
88 | |||
89 | /** |
||
90 | * Creates a new instance of Vars |
||
91 | * |
||
92 | * @param string|array $resource The main configuration resource |
||
93 | * @param array $options The options being used for Vars |
||
94 | */ |
||
95 | public function __construct($resource, $options = array()) |
||
119 | |||
120 | 47 | /** |
|
121 | 47 | * Parses the options so Vars can use them |
|
122 | * |
||
123 | * @param array $options The options being used for Vars |
||
124 | 47 | * |
|
125 | * @return array The parsed options |
||
126 | */ |
||
127 | private function parseOptions(array $options) |
||
149 | 8 | ||
150 | 54 | /** |
|
151 | * Makes the CacheProvider with the options |
||
152 | * |
||
153 | 62 | * @param array $options The options being used for Vars |
|
154 | * @param array|string $resource The main configuration resource |
||
155 | */ |
||
156 | 63 | private function makeCache($options, $resource) |
|
161 | |||
162 | /** |
||
163 | * Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
||
164 | * base path has |
||
165 | 63 | * |
|
166 | * @param array $options The options being used for Vars |
||
167 | 63 | */ |
|
168 | 62 | private function makePaths($options) |
|
177 | 62 | ||
178 | /** |
||
179 | 62 | * Makes the LoaderProvider with the options |
|
180 | * |
||
181 | 61 | * @param array $options The options being used for Vars |
|
182 | 1 | */ |
|
183 | 1 | private function makeLoader($options) |
|
188 | /** |
||
189 | * Sets the replacement variables if the option has been set |
||
190 | * |
||
191 | * @param array|null $options The options being used for Vars |
||
192 | */ |
||
193 | 61 | private function makeVariables($options) |
|
207 | 61 | ||
208 | 60 | /** |
|
209 | 59 | * Loads the cached file into the current class |
|
210 | */ |
||
211 | private function loadFromCache() |
||
234 | 60 | ||
235 | 60 | /** |
|
236 | * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
||
237 | * |
||
238 | 60 | * @param string $resource The resource to use to set the paths if they haven't been set |
|
239 | */ |
||
240 | public function pathsLoadedCheck($resource) |
||
258 | 3 | ||
259 | 3 | /** |
|
260 | * Get the Vars base path |
||
261 | 3 | * |
|
262 | 3 | * @return string The Vars base path |
|
263 | 58 | */ |
|
264 | public function getBasePath() |
||
268 | 2 | ||
269 | /** |
||
270 | 2 | * Set the Vars base path |
|
271 | * |
||
272 | * @param string $base_path The base path to set |
||
273 | 2 | * |
|
274 | 2 | * @throws \InvalidArgumentException If the base path does not exist or is not writable |
|
275 | 2 | * |
|
276 | 2 | * @return \M1\Vars\Vars |
|
277 | 2 | */ |
|
278 | 2 | View Code Duplication | public function setBasePath($base_path) |
294 | |||
295 | /** |
||
296 | * Adds a resource to $this->resources |
||
297 | * |
||
298 | * @param string $resource Resource to add to the stack |
||
299 | * |
||
300 | * @return int The position of the added resource |
||
301 | */ |
||
302 | public function addResource($resource) |
||
309 | 60 | ||
310 | /** |
||
311 | 60 | * Updates the string resource with the FileResource |
|
312 | 1 | * |
|
313 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
314 | * @param int $pos The position of the string resource |
||
315 | 59 | * |
|
316 | * @return \M1\Vars\Vars |
||
317 | */ |
||
318 | public function updateResource($resource, $pos) |
||
323 | 53 | ||
324 | /** |
||
325 | 53 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
|
326 | 52 | * |
|
327 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
328 | 52 | * |
|
329 | 50 | * @return bool Has resource already been imported |
|
330 | 50 | */ |
|
331 | 50 | public function resourceImported($resource) |
|
342 | |||
343 | /** |
||
344 | * Searches the resource stack for a certain resource |
||
345 | * |
||
346 | * @param string $resource The resource to search for |
||
347 | 53 | * |
|
348 | * @return bool Returns the resource if found |
||
349 | 53 | */ |
|
350 | public function getResource($resource) |
||
360 | |||
361 | /** |
||
362 | * Returns the imported resources |
||
363 | * |
||
364 | * @return array The Vars imported resources |
||
365 | */ |
||
366 | public function getResources() |
||
370 | |||
371 | /** |
||
372 | * Returns the Vars replacement variables |
||
373 | * |
||
374 | * @return array The Vars replacement variables |
||
375 | */ |
||
376 | public function getVariables() |
||
380 | |||
381 | 62 | /** |
|
382 | * Returns the CacheProvider if set, if not return false |
||
383 | 62 | * |
|
384 | 58 | * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
|
385 | */ |
||
386 | public function getCache() |
||
390 | } |
||
391 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: