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 | 63 | public function __construct($resource, $options = array()) |
|
| 96 | { |
||
| 97 | 63 | $options = $this->parseOptions($options); |
|
| 98 | 63 | $this->makeCache($options, $resource); |
|
| 99 | 62 | $this->makePaths($options); |
|
| 100 | |||
| 101 | 61 | if (!$this->cache->checkCache()) { |
|
| 102 | 61 | $this->makeLoader($options); |
|
| 103 | 59 | $this->makeVariables($options); |
|
| 104 | |||
| 105 | 58 | $resource = new ResourceProvider($this, $resource); |
|
| 106 | 47 | } |
|
| 107 | |||
| 108 | 47 | if ($this->cache->isHit()) { |
|
| 109 | 2 | $this->loadFromCache(); |
|
| 110 | 2 | } else { |
|
| 111 | 47 | $resource->mergeParentContent(); |
|
|
|
|||
| 112 | 47 | $this->content = $resource->getContent(); |
|
| 113 | |||
| 114 | 47 | $this->cache->setTime(time()); |
|
| 115 | 47 | $this->cache->makeCache($this); |
|
| 116 | |||
| 117 | } |
||
| 118 | 47 | } |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Parses the options so Vars can use them |
||
| 122 | * |
||
| 123 | * @param array $options The options being used for Vars |
||
| 124 | * |
||
| 125 | * @return array The parsed options |
||
| 126 | */ |
||
| 127 | 63 | private function parseOptions(array $options) |
|
| 128 | { |
||
| 129 | 63 | $parsed_options = array_merge($this->default_options, $options); |
|
| 130 | |||
| 131 | 63 | if (isset($options['loaders'])) { |
|
| 132 | 8 | $loaders = array(); |
|
| 133 | |||
| 134 | 8 | if (is_array($options['loaders']) && !empty($options['loaders'])) { |
|
| 135 | 3 | $loaders = $options['loaders']; |
|
| 136 | 8 | } elseif (is_string($options['loaders'])) { |
|
| 137 | 4 | $loaders[] = $options['loaders']; |
|
| 138 | 4 | } else { |
|
| 139 | 1 | $loaders = $this->default_options['loaders']; |
|
| 140 | } |
||
| 141 | |||
| 142 | 8 | $parsed_options['loaders'] = $loaders; |
|
| 143 | 8 | } else { |
|
| 144 | 55 | $parsed_options['loaders'] = $this->default_options['loaders']; |
|
| 145 | } |
||
| 146 | |||
| 147 | 63 | return $parsed_options; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Makes the CacheProvider with the options |
||
| 152 | * |
||
| 153 | * @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) |
|
| 157 | { |
||
| 158 | 63 | $cache = new CacheProvider($resource, $options); |
|
| 159 | 62 | $this->cache = $cache; |
|
| 160 | 62 | } |
|
| 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 | * |
||
| 166 | * @param array $options The options being used for Vars |
||
| 167 | */ |
||
| 168 | 62 | private function makePaths($options) |
|
| 169 | { |
||
| 170 | 62 | $this->setBasePath($options['base_path']); |
|
| 171 | |||
| 172 | 61 | if (is_null($options['cache_path']) && !is_null($options['base_path'])) { |
|
| 173 | 1 | $this->cache->setPath($options['base_path']); |
|
| 174 | 1 | $this->paths_loaded = true; |
|
| 175 | 1 | } |
|
| 176 | 61 | } |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Makes the LoaderProvider with the options |
||
| 180 | * |
||
| 181 | * @param array $options The options being used for Vars |
||
| 182 | */ |
||
| 183 | 61 | private function makeLoader($options) |
|
| 184 | { |
||
| 185 | 61 | $loader = new LoaderProvider($options, $this->default_options['loaders']); |
|
| 186 | 59 | $this->loader = $loader; |
|
| 187 | 59 | } |
|
| 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 | 59 | private function makeVariables($options) |
|
| 194 | { |
||
| 195 | 59 | if (isset($options['variables'])) { |
|
| 196 | 4 | $variables = new VariableResource($this, $options['variables']); |
|
| 197 | |||
| 198 | 3 | $v = array(); |
|
| 199 | |||
| 200 | 3 | foreach ($variables->getVariables() as $variable_key => $variable_value) { |
|
| 201 | 3 | $v["%".$variable_key.'%'] = $variable_value; |
|
| 202 | 3 | } |
|
| 203 | |||
| 204 | 3 | $this->variables = $v; |
|
| 205 | 3 | } |
|
| 206 | 58 | } |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Loads the cached file into the current class |
||
| 210 | */ |
||
| 211 | 2 | private function loadFromCache() |
|
| 212 | { |
||
| 213 | 2 | $this->cache->load(); |
|
| 214 | |||
| 215 | $passed_keys = array( |
||
| 216 | 2 | 'base_path', |
|
| 217 | 2 | 'content', |
|
| 218 | 2 | 'extensions', |
|
| 219 | 2 | 'loaders', |
|
| 220 | 2 | 'resources', |
|
| 221 | 2 | 'variables', |
|
| 222 | 2 | ); |
|
| 223 | |||
| 224 | 2 | $loaded_vars = get_object_vars($this->cache->getLoadedVars()); |
|
| 225 | |||
| 226 | 2 | foreach ($loaded_vars as $key => $value) { |
|
| 227 | 2 | if (in_array($key, $passed_keys)) { |
|
| 228 | 2 | $this->$key = $value; |
|
| 229 | 2 | } |
|
| 230 | 2 | } |
|
| 231 | |||
| 232 | 2 | $this->cache->setTime($loaded_vars['cache']->getTime()); |
|
| 233 | 2 | } |
|
| 234 | |||
| 235 | /** |
||
| 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 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
| 239 | */ |
||
| 240 | 53 | public function pathsLoadedCheck($resource) |
|
| 241 | { |
||
| 242 | 53 | if (!$this->paths_loaded) { |
|
| 243 | 52 | $base_path = $this->getBasePath(); |
|
| 244 | |||
| 245 | 52 | if (!$base_path) { |
|
| 246 | 50 | $file = pathinfo(realpath($resource)); |
|
| 247 | 50 | $base_path = $file['dirname']; |
|
| 248 | 50 | $this->setBasePath($base_path); |
|
| 249 | 50 | } |
|
| 250 | |||
| 251 | 52 | if ($this->cache->getProvide() && !$this->cache->getPath()) { |
|
| 252 | $this->cache->setPath($base_path); |
||
| 253 | } |
||
| 254 | |||
| 255 | 52 | $this->paths_loaded = true; |
|
| 256 | 52 | } |
|
| 257 | 53 | } |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Get the Vars base path |
||
| 261 | * |
||
| 262 | * @return string The Vars base path |
||
| 263 | */ |
||
| 264 | 53 | public function getBasePath() |
|
| 265 | { |
||
| 266 | 53 | return $this->base_path; |
|
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Set the Vars base path |
||
| 271 | * |
||
| 272 | * @param string $base_path The base path to set |
||
| 273 | * |
||
| 274 | * @throws \InvalidArgumentException If the base path does not exist or is not writable |
||
| 275 | * |
||
| 276 | * @return \M1\Vars\Vars |
||
| 277 | */ |
||
| 278 | 62 | View Code Duplication | public function setBasePath($base_path) |
| 279 | { |
||
| 280 | 62 | if (is_null($base_path)) { |
|
| 281 | 58 | return; |
|
| 282 | } |
||
| 283 | |||
| 284 | 54 | if (!is_dir($base_path)) { |
|
| 285 | 1 | throw new \InvalidArgumentException(sprintf( |
|
| 286 | 1 | "'%s' base path does not exist or is not writable", |
|
| 287 | $base_path |
||
| 288 | 1 | )); |
|
| 289 | } |
||
| 290 | |||
| 291 | 53 | $this->base_path = realpath($base_path); |
|
| 292 | 53 | return $this; |
|
| 293 | } |
||
| 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 | 53 | public function addResource($resource) |
|
| 303 | { |
||
| 304 | 53 | $r = realpath($resource); |
|
| 305 | 53 | $pos = count($this->resources); |
|
| 306 | 53 | $this->resources[$pos] = $r; |
|
| 307 | 53 | return $pos; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Updates the string resource with the FileResource |
||
| 312 | * |
||
| 313 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
| 314 | * @param int $pos The position of the string resource |
||
| 315 | * |
||
| 316 | * @return \M1\Vars\Vars |
||
| 317 | */ |
||
| 318 | 46 | public function updateResource($resource, $pos) |
|
| 319 | { |
||
| 320 | 46 | $this->resources[$pos] = $resource; |
|
| 321 | 46 | return $this; |
|
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
| 326 | * |
||
| 327 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
| 328 | * |
||
| 329 | * @return bool Has resource already been imported |
||
| 330 | */ |
||
| 331 | 53 | public function resourceImported($resource) |
|
| 332 | { |
||
| 333 | 53 | $resource = realpath($resource); |
|
| 334 | 53 | foreach ($this->getResources() as $r) { |
|
| 335 | 36 | if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) || |
|
| 336 | 36 | (is_string($r) && $resource === $r)) { |
|
| 337 | 1 | return true; |
|
| 338 | } |
||
| 339 | 53 | } |
|
| 340 | 53 | return false; |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Searches the resource stack for a certain resource |
||
| 345 | * |
||
| 346 | * @param string $resource The resource to search for |
||
| 347 | * |
||
| 348 | * @return bool Returns the resource if found |
||
| 349 | */ |
||
| 350 | 3 | public function getResource($resource) |
|
| 351 | { |
||
| 352 | 3 | foreach ($this->getResources() as $r) { |
|
| 353 | 3 | if ($resource === $r->getFilename()) { |
|
| 354 | 2 | return $r; |
|
| 355 | } |
||
| 356 | 3 | } |
|
| 357 | |||
| 358 | 1 | return false; |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns the imported resources |
||
| 363 | * |
||
| 364 | * @return array The Vars imported resources |
||
| 365 | */ |
||
| 366 | 53 | public function getResources() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the Vars replacement variables |
||
| 373 | * |
||
| 374 | * @return array The Vars replacement variables |
||
| 375 | */ |
||
| 376 | 42 | public function getVariables() |
|
| 377 | { |
||
| 378 | 42 | return $this->variables; |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Returns the CacheProvider if set, if not return false |
||
| 383 | * |
||
| 384 | * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
||
| 385 | */ |
||
| 386 | 5 | 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: