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 | 63 | 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 | 63 | private function parseOptions(array $options) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Makes the CacheProvider with the options |
||
| 161 | * |
||
| 162 | * @param array $options The options being used for Vars |
||
| 163 | * @param array|string $resource The main configuration resource |
||
| 164 | */ |
||
| 165 | 63 | private function makeCache($options, $resource) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
||
| 173 | * base path has |
||
| 174 | * |
||
| 175 | * @param array $options The options being used for Vars |
||
| 176 | */ |
||
| 177 | 62 | private function makePaths($options) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Get loaders and make extensions for the loaders |
||
| 189 | * |
||
| 190 | * @param array|null $options The options being used for Vars |
||
| 191 | * |
||
| 192 | */ |
||
| 193 | 61 | private function makeLoaders($options) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Makes namespace loaders from loader strings |
||
| 213 | * |
||
| 214 | * @param array $loaders The options being used for Vars |
||
| 215 | * |
||
| 216 | * @throws \InvalidArgumentException If a loader from options isn't found |
||
| 217 | * @throws \InvalidArgumentException If no loaders were loaded |
||
| 218 | * |
||
| 219 | * @return array The namespace loaders |
||
| 220 | */ |
||
| 221 | 61 | private function makeNameSpaceLoaders($loaders) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Sets the replacement variables if the option has been set |
||
| 247 | * |
||
| 248 | * @param array|null $options The options being used for Vars |
||
| 249 | */ |
||
| 250 | 59 | private function makeVariables($options) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Loads the cached file into the current class |
||
| 267 | */ |
||
| 268 | 2 | private function loadFromCache() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Get and make extensions for loaders made from makeLoaders() |
||
| 294 | * |
||
| 295 | * @see \M1\Vars\Vars::makeLoaders() \M1\Vars\Vars::makeLoaders() |
||
| 296 | * |
||
| 297 | * @param array $loaders File loaders |
||
| 298 | * |
||
| 299 | * @throws \RuntimeException If no loader extensions were found |
||
| 300 | * |
||
| 301 | * @return array File loader supported extensions |
||
| 302 | */ |
||
| 303 | 60 | private function makeExtensions(array $loaders) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
||
| 320 | * |
||
| 321 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
| 322 | */ |
||
| 323 | 53 | public function pathsLoadedCheck($resource) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get the Vars file loaders |
||
| 344 | * |
||
| 345 | * @return array The Vars file loaders |
||
| 346 | */ |
||
| 347 | 53 | public function getLoaders() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get the Vars file loaders extensions |
||
| 354 | * |
||
| 355 | * @return array The Vars file loader extensions |
||
| 356 | */ |
||
| 357 | 4 | public function getExtensions() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get the Vars base path |
||
| 364 | * |
||
| 365 | * @return string The Vars base path |
||
| 366 | */ |
||
| 367 | 53 | public function getBasePath() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Set the Vars base path |
||
| 374 | * |
||
| 375 | * @param string $base_path The base path to set |
||
| 376 | * |
||
| 377 | * @throws \InvalidArgumentException If the base path does not exist or is not writable |
||
| 378 | * |
||
| 379 | * @return \M1\Vars\Vars |
||
| 380 | */ |
||
| 381 | 62 | View Code Duplication | public function setBasePath($base_path) |
| 397 | |||
| 398 | /** |
||
| 399 | * Adds a resource to $this->resources |
||
| 400 | * |
||
| 401 | * @param string $resource Resource to add to the stack |
||
| 402 | * |
||
| 403 | * @return int The position of the added resource |
||
| 404 | */ |
||
| 405 | 53 | public function addResource($resource) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Updates the string resource with the FileResource |
||
| 415 | * |
||
| 416 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
| 417 | * @param int $pos The position of the string resource |
||
| 418 | * |
||
| 419 | * @return \M1\Vars\Vars |
||
| 420 | */ |
||
| 421 | 46 | public function updateResource($resource, $pos) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
| 429 | * |
||
| 430 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
| 431 | * |
||
| 432 | * @return bool Has resource already been imported |
||
| 433 | */ |
||
| 434 | 53 | public function resourceImported($resource) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Searches the resource stack for a certain resource |
||
| 448 | * |
||
| 449 | * @param string $resource The resource to search for |
||
| 450 | * |
||
| 451 | * @return bool Returns the resource if found |
||
| 452 | */ |
||
| 453 | 3 | public function getResource($resource) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the imported resources |
||
| 466 | * |
||
| 467 | * @return array The Vars imported resources |
||
| 468 | */ |
||
| 469 | 53 | public function getResources() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the Vars replacement variables |
||
| 476 | * |
||
| 477 | * @return array The Vars replacement variables |
||
| 478 | */ |
||
| 479 | 42 | public function getVariables() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Returns the CacheProvider if set, if not return false |
||
| 486 | * |
||
| 487 | * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
||
| 488 | */ |
||
| 489 | 5 | public function getCache() |
|
| 493 | } |
||
| 494 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: