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 | |||
| 34 | /** |
||
| 35 | * |
||
| 36 | * @var AnnotatedInterface|object|string |
||
| 37 | */ |
||
| 38 | private $component = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Options |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $instanceId = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Addendum runtime path |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $path = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * |
||
| 54 | * @var NsCache |
||
| 55 | */ |
||
| 56 | private $nsCache = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * |
||
| 60 | * @var Addendum |
||
| 61 | */ |
||
| 62 | private $addendum = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Runtime path |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private static $runtimePath = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Local cacheq |
||
| 72 | * @var type |
||
| 73 | */ |
||
| 74 | private static $cache = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Hash map of prepared directories |
||
| 78 | * Key is directory, value is flag indicating if it's prepared. |
||
| 79 | * @var bool[] |
||
| 80 | */ |
||
| 81 | private static $prepared = []; |
||
| 82 | private $fileName = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * @param string $metaClass |
||
| 87 | * @param AnnotatedInterface|object|string $component |
||
| 88 | * @param MetaOptions|Addendum $options |
||
| 89 | */ |
||
| 90 | 56 | public function __construct($metaClass = null, $component = null, $options = null) |
|
| 91 | { |
||
| 92 | 56 | if (null === self::$runtimePath) |
|
| 93 | { |
||
| 94 | 1 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
|
| 95 | } |
||
| 96 | 56 | $this->path = self::$runtimePath . '/addendum'; |
|
| 97 | 56 | $this->metaClass = $metaClass; |
|
| 98 | 56 | $this->component = $component; |
|
| 99 | 56 | if (empty($options)) |
|
| 100 | { |
||
| 101 | 4 | $this->instanceId = Addendum::DefaultInstanceId; |
|
| 102 | } |
||
| 103 | 54 | elseif ($options instanceof Addendum) |
|
| 104 | { |
||
| 105 | 54 | $this->instanceId = $options->getInstanceId(); |
|
| 106 | } |
||
| 107 | elseif ($options instanceof MetaOptions) |
||
| 108 | { |
||
| 109 | $this->instanceId = $options->instanceId; |
||
| 110 | } |
||
| 111 | else |
||
| 112 | { |
||
| 113 | throw new UnexpectedValueException('Unknown options'); |
||
| 114 | } |
||
| 115 | 56 | $this->prepare(); |
|
| 116 | 56 | $this->addendum = Addendum::fly($this->instanceId); |
|
| 117 | 56 | $this->nsCache = new NsCache(dirname($this->getFilename()), $this->addendum); |
|
| 118 | 56 | } |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Set working component |
||
| 122 | * @param AnnotatedInterface|object|string $component |
||
| 123 | */ |
||
| 124 | 54 | public function setComponent($component = null) |
|
| 125 | { |
||
| 126 | // Reset filename as it depends on component |
||
| 127 | 54 | $this->fileName = null; |
|
| 128 | 54 | $this->component = $component; |
|
| 129 | 54 | } |
|
| 130 | |||
| 131 | 33 | public function setOptions(MetaOptions $options = null) |
|
| 132 | { |
||
| 133 | 33 | $this->fileName = null; |
|
| 134 | 33 | $this->nsCache->setOptions($options); |
|
| 135 | 33 | } |
|
| 136 | |||
| 137 | 56 | private function prepare() |
|
| 138 | { |
||
| 139 | 56 | $fileDir = dirname($this->getFilename()); |
|
| 140 | 56 | if (isset(self::$prepared[$fileDir]) && self::$prepared[$fileDir]) |
|
| 141 | { |
||
| 142 | 54 | return true; |
|
| 143 | } |
||
| 144 | 6 | if (!file_exists($this->path)) |
|
| 145 | { |
||
| 146 | 1 | if (!file_exists(self::$runtimePath)) |
|
| 147 | { |
||
| 148 | |||
| 149 | if (is_writable(dirname(self::$runtimePath))) |
||
| 150 | { |
||
| 151 | $this->mkdir(self::$runtimePath); |
||
| 152 | } |
||
| 153 | if (!is_writable(self::$runtimePath)) |
||
| 154 | { |
||
| 155 | throw new RuntimeException(sprintf("Runtime path `%s` must exists and be writable", self::$runtimePath)); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | 1 | if (is_writable(self::$runtimePath)) |
|
| 159 | { |
||
| 160 | 1 | $this->mkdir($this->path); |
|
| 161 | } |
||
| 162 | 1 | if (!is_writable($this->path)) |
|
| 163 | { |
||
| 164 | throw new RuntimeException(sprintf("Addendum runtime path `%s` must exists and be writable", $this->path)); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | 6 | if (!file_exists($fileDir)) |
|
| 168 | { |
||
| 169 | 2 | $this->mkdir($fileDir); |
|
| 170 | } |
||
| 171 | 6 | self::$prepared[$fileDir] = true; |
|
| 172 | 6 | } |
|
| 173 | |||
| 174 | 54 | public function get() |
|
| 175 | { |
||
| 176 | 54 | $this->prepare(); |
|
| 177 | 54 | $fileName = $this->getFilename(); |
|
| 178 | |||
| 179 | 54 | if (NsCache::$addeNs && !$this->nsCache->valid()) |
|
| 180 | { |
||
| 181 | 1 | $this->clearCurrentPath(); |
|
| 182 | 1 | return false; |
|
| 183 | } |
||
| 184 | 54 | $key = $this->getCacheKey(); |
|
| 185 | 54 | if (isset(self::$cache[$key])) |
|
| 186 | { |
||
| 187 | 54 | return self::$cache[$key]; |
|
| 188 | } |
||
| 189 | |||
| 190 | 52 | $data = SoftIncluder::includeFile($fileName); |
|
| 191 | |||
| 192 | // Only false means not existing cache. |
||
| 193 | // NOTE: Cache might have valid `empty` value, ie. empty array. |
||
| 194 | 52 | if (false === $data) |
|
| 195 | { |
||
| 196 | 52 | return false; |
|
| 197 | } |
||
| 198 | |||
| 199 | // Purge file cache if checkMTime is enabled and file obsolete |
||
| 200 | if ($this->addendum->checkMTime && file_exists($fileName)) |
||
| 201 | { |
||
| 202 | $cacheTime = filemtime($fileName); |
||
| 203 | |||
| 204 | // Partial component name, split by @ and take first argument |
||
| 205 | if (is_string($this->component) && strstr($this->component, '@')) |
||
| 206 | { |
||
| 207 | $parts = explode('@', $this->component); |
||
| 208 | $componentClass = array_shift($parts); |
||
| 209 | } |
||
| 210 | else |
||
| 211 | { |
||
| 212 | $componentClass = $this->component; |
||
| 213 | } |
||
| 214 | $componentTime = filemtime((new ReflectionClass($componentClass))->getFileName()); |
||
| 215 | if ($componentTime > $cacheTime) |
||
| 216 | { |
||
| 217 | $this->remove(); |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | self::$cache[$key] = $data; |
||
| 222 | return $data; |
||
| 223 | } |
||
| 224 | |||
| 225 | 52 | public function set($data) |
|
| 237 | |||
| 238 | public function remove() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Clear entire cache |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | 3 | public function clear() |
|
| 255 | { |
||
| 256 | 3 | self::$prepared = []; |
|
| 257 | 3 | return $this->clearPath($this->path); |
|
| 258 | } |
||
| 259 | |||
| 260 | 1 | private function clearCurrentPath() |
|
| 261 | { |
||
| 262 | 1 | $path = dirname($this->getFilename()); |
|
| 263 | 1 | return $this->clearPath($path); |
|
| 264 | } |
||
| 265 | |||
| 266 | 4 | private function clearPath($path) |
|
| 267 | { |
||
| 268 | 4 | if (!file_exists($path)) |
|
| 269 | { |
||
| 270 | return false; |
||
| 271 | } |
||
| 272 | 4 | foreach (new DirectoryIterator($path) as $dir) |
|
| 273 | { |
||
| 274 | 4 | if ($dir->isDot() || !$dir->isDir()) |
|
| 275 | { |
||
| 276 | 4 | continue; |
|
| 277 | } |
||
| 278 | 3 | foreach (new DirectoryIterator($dir->getPathname()) as $file) |
|
| 279 | { |
||
| 280 | 3 | if (!$file->isFile()) |
|
| 281 | { |
||
| 282 | 3 | continue; |
|
| 283 | } |
||
| 284 | |||
| 285 | // Skip ns cache file, or it might regenerate over and over |
||
| 286 | // ns file cache is replaced when needed by NsCache |
||
| 287 | 3 | if ($file->getBasename() === NsCache::FileName) |
|
| 288 | { |
||
| 289 | 3 | continue; |
|
| 290 | } |
||
| 291 | 3 | unlink($file->getPathname()); |
|
| 292 | } |
||
| 293 | } |
||
| 294 | 4 | self::$prepared[$path] = false; |
|
| 295 | 4 | } |
|
| 296 | |||
| 297 | 56 | private function getFilename() |
|
| 298 | { |
||
| 299 | 56 | if (!empty($this->fileName)) |
|
| 300 | { |
||
| 301 | 56 | return $this->fileName; |
|
| 302 | } |
||
| 303 | 56 | if (is_object($this->component)) |
|
| 304 | { |
||
| 305 | 32 | $className = get_class($this->component); |
|
| 306 | } |
||
| 307 | 56 | elseif (is_string($this->component) || null === $this->component) |
|
| 308 | { |
||
| 309 | 56 | $className = $this->component; |
|
| 310 | } |
||
| 311 | else |
||
| 312 | { |
||
| 313 | throw new UnexpectedValueException(sprintf('Expected string or object or null got: `%s`', gettype($className))); |
||
| 314 | } |
||
| 315 | |||
| 316 | 56 | if (ClassChecker::isAnonymous($className)) |
|
| 317 | { |
||
| 318 | 1 | NameNormalizer::normalize($className); |
|
| 319 | } |
||
| 320 | |||
| 321 | $params = [ |
||
| 322 | 56 | (string) $this->path, |
|
| 323 | 56 | (string) $this->classToFile($this->metaClass), |
|
| 324 | 56 | (string) $this->instanceId, |
|
| 325 | 56 | (string) str_replace('\\', '/', $this->classToFile($className)) |
|
| 326 | ]; |
||
| 327 | 56 | $this->fileName = vsprintf('%s/%s@%s/%s.php', $params); |
|
| 328 | 56 | return $this->fileName; |
|
| 329 | } |
||
| 330 | |||
| 331 | 54 | private function getCacheKey() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Convert slash separated class name to dot separated name. |
||
| 346 | * @param string $className |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | 56 | private function classToFile($className) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Recursively create dir with proper permissions. |
||
| 356 | * |
||
| 357 | * @param string $path |
||
| 358 | */ |
||
| 359 | 2 | private function mkdir($path) |
|
| 365 | |||
| 366 | } |
||
| 367 |