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 |
||
| 36 | abstract class PhpCache |
||
| 37 | { |
||
| 38 | |||
| 39 | private $metaClass = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * |
||
| 43 | * @var AnnotatedInterface|object|string |
||
| 44 | */ |
||
| 45 | private $component = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Options |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $instanceId = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Addendum runtime path |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $path = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * |
||
| 61 | * @var NsCache |
||
| 62 | */ |
||
| 63 | private $nsCache = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * |
||
| 67 | * @var Addendum |
||
| 68 | */ |
||
| 69 | private $addendum = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Runtime path |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private static $runtimePath = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Local cache |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private static $cache = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Hash map of prepared directories |
||
| 85 | * Key is directory, value is flag indicating if it's prepared. |
||
| 86 | * @var bool[] |
||
| 87 | */ |
||
| 88 | private static $prepared = []; |
||
| 89 | private $fileName = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * |
||
| 93 | * @param string $metaClass |
||
| 94 | * @param AnnotatedInterface|object|string $component |
||
| 95 | * @param MetaOptions|Addendum $options |
||
| 96 | */ |
||
| 97 | 60 | public function __construct($metaClass = null, $component = null, $options = null) |
|
| 98 | { |
||
| 99 | 60 | if (null === self::$runtimePath) |
|
| 100 | { |
||
| 101 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
||
| 102 | } |
||
| 103 | 60 | $this->path = self::$runtimePath . '/addendum'; |
|
| 104 | 60 | $this->metaClass = $metaClass; |
|
| 105 | 60 | $this->component = $component; |
|
| 106 | 60 | if ($options === null) |
|
| 107 | { |
||
| 108 | 3 | $this->instanceId = Addendum::DefaultInstanceId; |
|
| 109 | } |
||
| 110 | 58 | elseif ($options instanceof Addendum) |
|
| 111 | { |
||
| 112 | 58 | $this->instanceId = $options->getInstanceId(); |
|
| 113 | } |
||
| 114 | elseif ($options instanceof MetaOptions) |
||
| 115 | { |
||
| 116 | $this->instanceId = $options->instanceId; |
||
| 117 | } |
||
| 118 | else |
||
| 119 | { |
||
| 120 | throw new UnexpectedValueException('Unknown options'); |
||
| 121 | } |
||
| 122 | 60 | $this->prepare(); |
|
| 123 | 60 | $this->addendum = Addendum::fly($this->instanceId); |
|
| 124 | 60 | $this->nsCache = new NsCache(dirname($this->getFilename()), $this->addendum); |
|
| 125 | 60 | } |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Set working component |
||
| 129 | * @param AnnotatedInterface|object|string $component |
||
| 130 | */ |
||
| 131 | 58 | public function setComponent($component = null) |
|
| 137 | |||
| 138 | public function setOptions(MetaOptions $options = null) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Prepare cache storage |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 60 | private function prepare(): bool |
|
| 149 | { |
||
| 150 | 60 | $fileDir = dirname($this->getFilename()); |
|
| 151 | 60 | if (isset(self::$prepared[$fileDir]) && self::$prepared[$fileDir]) |
|
| 152 | { |
||
| 153 | 58 | return true; |
|
| 154 | } |
||
| 155 | 60 | if (!file_exists($this->path)) |
|
| 156 | { |
||
| 157 | if (!file_exists(self::$runtimePath)) |
||
| 158 | { |
||
| 159 | |||
| 160 | if (is_writable(dirname(self::$runtimePath))) |
||
| 161 | { |
||
| 162 | Io::mkdir(self::$runtimePath); |
||
| 163 | } |
||
| 164 | if (!is_writable(self::$runtimePath)) |
||
| 165 | { |
||
| 166 | throw new RuntimeException(sprintf("Runtime path `%s` must exists and be writable", self::$runtimePath)); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | if (is_writable(self::$runtimePath)) |
||
| 170 | { |
||
| 171 | Io::mkdir($this->path); |
||
| 172 | } |
||
| 173 | if (!is_writable($this->path)) |
||
| 174 | { |
||
| 175 | throw new RuntimeException(sprintf("Addendum runtime path `%s` must exists and be writable", $this->path)); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | 60 | if (!file_exists($fileDir)) |
|
| 179 | { |
||
| 180 | 1 | Io::mkdir($fileDir); |
|
| 181 | } |
||
| 182 | 60 | self::$prepared[$fileDir] = true; |
|
| 183 | 60 | return false; |
|
| 184 | } |
||
| 185 | |||
| 186 | 58 | public function get() |
|
| 236 | |||
| 237 | 53 | public function set($data) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function remove(): bool |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Clear entire cache |
||
| 267 | * @return boolean |
||
| 268 | */ |
||
| 269 | 3 | public function clear(): bool |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | private function clearCurrentPath(): bool |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $path |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | 3 | private function clearPath(string $path): bool |
|
| 319 | |||
| 320 | 60 | private function getFilename() |
|
| 353 | |||
| 354 | 58 | private function getCacheKey() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Convert slash separated class name to dot separated name. |
||
| 369 | * @param string $className |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | 60 | private function classToFile($className) |
|
| 376 | } |