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 |
||
| 29 | abstract class PhpCache |
||
| 30 | { |
||
| 31 | |||
| 32 | private $metaClass = null; |
||
| 33 | private $component = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Options |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $instanceId = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Addendum runtime path |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $path = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * |
||
| 49 | * @var NsCache |
||
| 50 | */ |
||
| 51 | private $nsCache = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * @var Addendum |
||
| 56 | */ |
||
| 57 | private $addendum = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Runtime path |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private static $runtimePath = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Local cacheq |
||
| 67 | * @var type |
||
| 68 | */ |
||
| 69 | private static $cache = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Hash map of prepared directories |
||
| 73 | * Key is directory, value is flag indicating if it's prepared. |
||
| 74 | * @var bool[] |
||
| 75 | */ |
||
| 76 | private static $prepared = []; |
||
| 77 | private $fileName = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @param string $metaClass |
||
| 82 | * @param AnnotatedInterface|string $component |
||
| 83 | * @param MetaOptions|Addendum $options |
||
| 84 | */ |
||
| 85 | 48 | public function __construct($metaClass = null, $component = null, $options = null) |
|
| 86 | { |
||
| 87 | 48 | if (null === self::$runtimePath) |
|
| 88 | { |
||
| 89 | 1 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
|
| 90 | } |
||
| 91 | 48 | $this->path = self::$runtimePath . '/addendum'; |
|
| 92 | 48 | $this->metaClass = $metaClass; |
|
| 93 | 48 | $this->component = $component; |
|
| 94 | 48 | if (empty($options)) |
|
| 95 | { |
||
| 96 | 3 | $this->instanceId = Addendum::DefaultInstanceId; |
|
| 97 | } |
||
| 98 | elseif ($options instanceof Addendum) |
||
| 99 | { |
||
| 100 | 46 | $this->instanceId = $options->getInstanceId(); |
|
| 101 | } |
||
| 102 | elseif ($options instanceof MetaOptions) |
||
| 103 | { |
||
| 104 | $this->instanceId = $options->instanceId; |
||
| 105 | } |
||
| 106 | else |
||
| 107 | { |
||
| 108 | throw new UnexpectedValueException('Unknown options'); |
||
| 109 | } |
||
| 110 | 48 | $this->prepare(); |
|
| 111 | 48 | $this->addendum = Addendum::fly($this->instanceId); |
|
| 112 | 48 | $this->nsCache = new NsCache(dirname($this->getFilename()), $this->addendum); |
|
| 113 | 48 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Set working component |
||
| 117 | * @param AnnotatedInterface|string $component |
||
| 118 | */ |
||
| 119 | 50 | public function setComponent($component = null) |
|
| 125 | |||
| 126 | 32 | public function setOptions(MetaOptions $options = null) |
|
| 131 | |||
| 132 | 52 | private function prepare() |
|
| 168 | |||
| 169 | 50 | public function get() |
|
| 217 | |||
| 218 | 45 | public function set($data) |
|
| 230 | |||
| 231 | public function remove() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Clear entire cache |
||
| 245 | * @return boolean |
||
| 246 | */ |
||
| 247 | 3 | public function clear() |
|
| 252 | |||
| 253 | 2 | private function clearCurrentPath() |
|
| 258 | |||
| 259 | 5 | private function clearPath($path) |
|
| 272 | |||
| 273 | 52 | private function getFilename() |
|
| 290 | |||
| 291 | 50 | private function getCacheKey() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Convert slash separated class name to dot separated name. |
||
| 306 | * @param string $className |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 52 | private function classToFile($className) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Recursively create dir with proper permissions. |
||
| 316 | * |
||
| 317 | * @param string $path |
||
| 318 | */ |
||
| 319 | 6 | private function mkdir($path) |
|
| 325 | |||
| 326 | } |
||
| 327 |