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 |
||
33 | class Vars extends AbstractResource |
||
34 | { |
||
35 | /** |
||
36 | * Used for to* functions |
||
37 | */ |
||
38 | use TransformerTrait; |
||
39 | |||
40 | /** |
||
41 | * The base path for the Vars config and cache folders |
||
42 | * |
||
43 | * @var string $base_path |
||
44 | */ |
||
45 | private $base_path; |
||
46 | |||
47 | /** |
||
48 | * The cache object if the cache is wanted, else false |
||
49 | * |
||
50 | * @var \M1\Vars\Cache\CacheProvider $cache |
||
51 | */ |
||
52 | public $cache; |
||
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('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',) |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * The loaderProvider for Vars supplies the file loaders and the extensions that are supported |
||
69 | * |
||
70 | * @var \M1\Vars\Loader\LoaderProvider $loader |
||
71 | */ |
||
72 | public $loader; |
||
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 | 80 | public function __construct($resource, $options = array()) |
|
125 | |||
126 | /** |
||
127 | * Parses the options so Vars can use them |
||
128 | * |
||
129 | * @param array $options The options being used for Vars |
||
130 | * |
||
131 | * @return array The parsed options |
||
132 | */ |
||
133 | 80 | private function parseOptions(array $options) |
|
139 | |||
140 | /** |
||
141 | * Makes the CacheProvider with the options |
||
142 | * |
||
143 | * @param array $options The options being used for Vars |
||
144 | * @param array|string $resource The main configuration resource |
||
145 | */ |
||
146 | 80 | private function makeCache($options, $resource) |
|
151 | |||
152 | /** |
||
153 | * Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
||
154 | * base path has |
||
155 | * |
||
156 | * @param array $options The options being used for Vars |
||
157 | */ |
||
158 | 79 | private function makePaths($options) |
|
167 | |||
168 | /** |
||
169 | * Makes the LoaderProvider with the options |
||
170 | * |
||
171 | * @param array $options The options being used for Vars |
||
172 | */ |
||
173 | 78 | private function makeLoader($options) |
|
178 | |||
179 | /** |
||
180 | * Sets the replacement variables if the option has been set |
||
181 | * |
||
182 | * @param array|null $options The options being used for Vars |
||
183 | */ |
||
184 | 76 | private function makeVariables($options) |
|
198 | |||
199 | /** |
||
200 | * Loads the cached file into the current class |
||
201 | */ |
||
202 | 3 | private function loadFromCache() |
|
225 | |||
226 | /** |
||
227 | * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
||
228 | * |
||
229 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
230 | */ |
||
231 | 69 | public function pathsLoadedCheck($resource) |
|
249 | |||
250 | /** |
||
251 | * Get the Vars base path |
||
252 | * |
||
253 | * @return string The Vars base path |
||
254 | */ |
||
255 | 69 | public function getBasePath() |
|
259 | |||
260 | /** |
||
261 | * Set the Vars base path |
||
262 | * |
||
263 | * @param string $base_path The base path to set |
||
264 | * |
||
265 | * @throws \InvalidArgumentException If the base path does not exist or is not writable |
||
266 | * |
||
267 | * @return \M1\Vars\Vars |
||
268 | */ |
||
269 | 79 | View Code Duplication | public function setBasePath($base_path) |
285 | |||
286 | /** |
||
287 | * Adds a resource to $this->resources |
||
288 | * |
||
289 | * @param string $resource Resource to add to the stack |
||
290 | * |
||
291 | * @return int The position of the added resource |
||
292 | */ |
||
293 | 69 | public function addResource($resource) |
|
300 | |||
301 | /** |
||
302 | * Updates the string resource with the FileResource |
||
303 | * |
||
304 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
305 | * @param int $pos The position of the string resource |
||
306 | * |
||
307 | * @return \M1\Vars\Vars |
||
308 | */ |
||
309 | 60 | public function updateResource($resource, $pos) |
|
314 | |||
315 | /** |
||
316 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
317 | * |
||
318 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
319 | * |
||
320 | * @return bool Has resource already been imported |
||
321 | */ |
||
322 | 69 | public function resourceImported($resource) |
|
333 | |||
334 | /** |
||
335 | * Searches the resource stack for a certain resource |
||
336 | * |
||
337 | * @param string $resource The resource to search for |
||
338 | * |
||
339 | * @return bool Returns the resource if found |
||
340 | */ |
||
341 | 3 | public function getResource($resource) |
|
351 | |||
352 | /** |
||
353 | * Returns the imported resources |
||
354 | * |
||
355 | * @return array The Vars imported resources |
||
356 | */ |
||
357 | 69 | public function getResources() |
|
361 | |||
362 | /** |
||
363 | * Returns the Vars replacement variables |
||
364 | * |
||
365 | * @return array The Vars replacement variables |
||
366 | */ |
||
367 | 52 | public function getVariables() |
|
371 | |||
372 | /** |
||
373 | * Returns the CacheProvider if set, if not return false |
||
374 | * |
||
375 | * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
||
376 | */ |
||
377 | 7 | public function getCache() |
|
381 | } |
||
382 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: