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 | 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 | 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 | 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 | private function makeLoaders($options) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Sets the replacement variables if the option has been set |
||
| 217 | * |
||
| 218 | * @param array|null $options The options being used for Vars |
||
| 219 | */ |
||
| 220 | private function makeVariables($options) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Loads the cached file into the current class |
||
| 237 | */ |
||
| 238 | private function loadFromCache() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get and make extensions for loaders made from makeLoaders() |
||
| 264 | * |
||
| 265 | * @see \M1\Vars\Vars::makeLoaders() \M1\Vars\Vars::makeLoaders() |
||
| 266 | * |
||
| 267 | * @param array $loaders File loaders |
||
| 268 | * |
||
| 269 | * @throws \RuntimeException If no loader extensions were found |
||
| 270 | * |
||
| 271 | * @return array File loader supported extensions |
||
| 272 | */ |
||
| 273 | private function makeExtensions(array $loaders) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
||
| 290 | * |
||
| 291 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
| 292 | */ |
||
| 293 | public function pathsLoadedCheck($resource) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the Vars file loaders |
||
| 314 | * |
||
| 315 | * @return array The Vars file loaders |
||
| 316 | */ |
||
| 317 | public function getLoaders() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get the Vars file loaders extensions |
||
| 324 | * |
||
| 325 | * @return array The Vars file loader extensions |
||
| 326 | */ |
||
| 327 | public function getExtensions() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get the Vars base path |
||
| 334 | * |
||
| 335 | * @return string The Vars base path |
||
| 336 | */ |
||
| 337 | public function getBasePath() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Set the Vars base path |
||
| 344 | * |
||
| 345 | * @param string $base_path The base path to set |
||
| 346 | * |
||
| 347 | * @throws \InvalidArgumentException If the base path does not exist or is not writable |
||
| 348 | * |
||
| 349 | * @return \M1\Vars\Vars |
||
| 350 | */ |
||
| 351 | View Code Duplication | public function setBasePath($base_path) |
|
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * Adds a resource to $this->resources |
||
| 372 | * |
||
| 373 | * @param string $resource Resource to add to the stack |
||
| 374 | * |
||
| 375 | * @return int The position of the added resource |
||
| 376 | */ |
||
| 377 | public function addResource($resource) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Updates the string resource with the FileResource |
||
| 387 | * |
||
| 388 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
| 389 | * @param int $pos The position of the string resource |
||
| 390 | * |
||
| 391 | * @return \M1\Vars\Vars |
||
| 392 | */ |
||
| 393 | public function updateResource($resource, $pos) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
| 401 | * |
||
| 402 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
| 403 | * |
||
| 404 | * @return bool Has resource already been imported |
||
| 405 | */ |
||
| 406 | public function resourceImported($resource) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Searches the resource stack for a certain resource |
||
| 420 | * |
||
| 421 | * @param string $resource The resource to search for |
||
| 422 | * |
||
| 423 | * @return bool Returns the resource if found |
||
| 424 | */ |
||
| 425 | public function getResource($resource) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns the imported resources |
||
| 438 | * |
||
| 439 | * @return array The Vars imported resources |
||
| 440 | */ |
||
| 441 | public function getResources() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Returns the Vars replacement variables |
||
| 448 | * |
||
| 449 | * @return array The Vars replacement variables |
||
| 450 | */ |
||
| 451 | public function getVariables() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Returns the CacheProvider if set, if not return false |
||
| 458 | * |
||
| 459 | * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
||
| 460 | */ |
||
| 461 | public function getCache() |
||
| 465 | } |
||
| 466 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: