Complex classes like PhpCache 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 PhpCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class PhpCache |
||
| 36 | { |
||
| 37 | |||
| 38 | private $metaClass = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @var AnnotatedInterface|object|string |
||
| 43 | */ |
||
| 44 | private $component = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Options |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $instanceId = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Addendum runtime path |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $path = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * |
||
| 60 | * @var NsCache |
||
| 61 | */ |
||
| 62 | private $nsCache = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * |
||
| 66 | * @var Addendum |
||
| 67 | */ |
||
| 68 | private $addendum = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Runtime path |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private static $runtimePath = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Local cache |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private static $cache = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Hash map of prepared directories |
||
| 84 | * Key is directory, value is flag indicating if it's prepared. |
||
| 85 | * @var bool[] |
||
| 86 | */ |
||
| 87 | private static $prepared = []; |
||
| 88 | private $fileName = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * |
||
| 92 | * @param string $metaClass |
||
| 93 | * @param AnnotatedInterface|object|string $component |
||
| 94 | * @param MetaOptions|Addendum $options |
||
| 95 | */ |
||
| 96 | 61 | public function __construct($metaClass = null, $component = null, $options = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Set working component |
||
| 128 | * @param AnnotatedInterface|object|string $component |
||
| 129 | */ |
||
| 130 | 59 | public function setComponent($component = null) |
|
| 136 | |||
| 137 | public function setOptions(MetaOptions $options = null) |
||
| 138 | { |
||
| 139 | $this->fileName = null; |
||
| 140 | $this->nsCache->setOptions($options); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Prepare cache storage |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 61 | private function prepare(): bool |
|
| 184 | |||
| 185 | 59 | public function get() |
|
| 186 | { |
||
| 187 | 59 | $this->prepare(); |
|
| 188 | 59 | $fileName = $this->getFilename(); |
|
| 189 | |||
| 190 | 59 | if (NsCache::$addedNs && !$this->nsCache->valid()) |
|
| 191 | { |
||
| 192 | $this->clearCurrentPath(); |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | 59 | $key = $this->getCacheKey(); |
|
| 196 | 59 | if (isset(self::$cache[$key])) |
|
| 197 | { |
||
| 198 | 59 | return self::$cache[$key]; |
|
| 199 | } |
||
| 200 | |||
| 201 | 54 | $data = SoftIncluder::includeFile($fileName); |
|
| 202 | |||
| 203 | // Only false means not existing cache. |
||
| 204 | // NOTE: Cache might have valid `empty` value, ie. empty array. |
||
| 205 | 54 | if (false === $data) |
|
| 206 | { |
||
| 207 | 54 | return false; |
|
| 208 | } |
||
| 209 | |||
| 210 | // Purge file cache if checkMTime is enabled and file obsolete |
||
| 211 | if ($this->addendum->checkMTime && file_exists($fileName)) |
||
| 212 | { |
||
| 213 | $cacheTime = filemtime($fileName); |
||
| 214 | |||
| 215 | // Partial component name, split by @ and take first argument |
||
| 216 | if (is_string($this->component) && strstr($this->component, '@')) |
||
| 217 | { |
||
| 218 | $parts = explode('@', $this->component); |
||
| 219 | $componentClass = array_shift($parts); |
||
| 220 | } |
||
| 221 | else |
||
| 222 | { |
||
| 223 | $componentClass = $this->component; |
||
| 224 | } |
||
| 225 | $componentTime = filemtime((new ReflectionClass($componentClass))->getFileName()); |
||
| 226 | if ($componentTime > $cacheTime) |
||
| 227 | { |
||
| 228 | $this->remove(); |
||
| 229 | return false; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | self::$cache[$key] = $data; |
||
| 233 | return $data; |
||
| 234 | } |
||
| 235 | |||
| 236 | 54 | public function set($data) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function remove(): bool |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Clear entire cache |
||
| 266 | * @return boolean |
||
| 267 | */ |
||
| 268 | 3 | public function clear(): bool |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | private function clearCurrentPath(): bool |
||
| 278 | { |
||
| 279 | $path = dirname($this->getFilename()); |
||
| 280 | return $this->clearPath($path); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $path |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | 3 | private function clearPath(string $path): bool |
|
| 318 | |||
| 319 | 61 | private function getFilename() |
|
| 320 | { |
||
| 321 | 61 | if (!empty($this->fileName)) |
|
| 322 | { |
||
| 323 | 61 | return $this->fileName; |
|
| 324 | } |
||
| 325 | 61 | if (is_object($this->component)) |
|
| 326 | { |
||
| 327 | $className = get_class($this->component); |
||
| 328 | } |
||
| 329 | 61 | elseif (is_string($this->component) || null === $this->component) |
|
| 330 | { |
||
| 331 | 61 | $className = $this->component; |
|
| 332 | } |
||
| 333 | else |
||
| 334 | { |
||
| 335 | throw new UnexpectedValueException(sprintf('Expected string or object or null got: `%s`', gettype($this->component))); |
||
| 336 | } |
||
| 337 | |||
| 338 | 61 | if (ClassChecker::isAnonymous($className)) |
|
| 339 | { |
||
| 340 | 1 | NameNormalizer::normalize($className); |
|
| 341 | } |
||
| 342 | |||
| 343 | $params = [ |
||
| 344 | 61 | (string) $this->path, |
|
| 345 | 61 | (string) $this->classToFile($this->metaClass), |
|
| 346 | 61 | (string) $this->instanceId, |
|
| 347 | 61 | (string) str_replace('\\', '/', $this->classToFile($className)) |
|
| 348 | ]; |
||
| 349 | 61 | $this->fileName = vsprintf('%s/%s@%s/%s.php', $params); |
|
| 350 | 61 | return $this->fileName; |
|
| 351 | } |
||
| 352 | |||
| 353 | 59 | private function getCacheKey() |
|
| 354 | { |
||
| 355 | 59 | if (is_object($this->component)) |
|
| 356 | { |
||
| 357 | $className = get_class($this->component); |
||
| 358 | } |
||
| 359 | else |
||
| 360 | { |
||
| 361 | 59 | $className = $this->component; |
|
| 362 | } |
||
| 363 | 59 | return sprintf('%s@%s@%s@%s.php', $this->classToFile(static::class), $this->classToFile($this->metaClass), $this->instanceId, str_replace('\\', '/', $this->classToFile($className))); |
|
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Convert slash separated class name to dot separated name. |
||
| 368 | * @param string $className |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | 61 | private function classToFile($className) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Recursively create dir with proper permissions. |
||
| 378 | * |
||
| 379 | * @param string $path |
||
| 380 | */ |
||
| 381 | 2 | private function mkdir($path) |
|
| 387 | |||
| 388 | } |