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() |
|
| 170 | { |
||
| 171 | 50 | $this->prepare(); |
|
| 172 | 50 | $fileName = $this->getFilename(); |
|
| 173 | |||
| 174 | 50 | if (!$this->nsCache->valid()) |
|
| 175 | { |
||
| 176 | 3 | $this->clearCurrentPath(); |
|
| 177 | 3 | return false; |
|
| 178 | } |
||
| 179 | 50 | $key = $this->getCacheKey(); |
|
| 180 | 50 | if (isset(self::$cache[$key])) |
|
| 181 | { |
||
| 182 | 50 | return self::$cache[$key]; |
|
| 183 | } |
||
| 184 | |||
| 185 | 45 | $data = SoftIncluder::includeFile($fileName); |
|
| 186 | |||
| 187 | // Only false means not existing cache. |
||
| 188 | // NOTE: Cache might have valid `empty` value, ie. empty array. |
||
| 189 | 45 | if (false === $data) |
|
| 190 | { |
||
| 191 | 45 | return false; |
|
| 192 | } |
||
| 193 | |||
| 194 | // Purge file cache if checkMTime is enabled and file obsolete |
||
| 195 | if ($this->addendum->checkMTime && file_exists($fileName)) |
||
| 196 | { |
||
| 197 | $cacheTime = filemtime($fileName); |
||
| 198 | |||
| 199 | // Partial component name, split by @ and take first argument |
||
| 200 | if (is_string($this->component) && strstr($this->component, '@')) |
||
| 201 | { |
||
| 202 | $parts = explode('@', $this->component); |
||
| 203 | $componentClass = array_shift($parts); |
||
| 204 | } |
||
| 205 | else |
||
| 206 | { |
||
| 207 | $componentClass = $this->component; |
||
| 208 | } |
||
| 209 | $componentTime = filemtime((new ReflectionClass($componentClass))->getFileName()); |
||
| 210 | if ($componentTime > $cacheTime) |
||
| 211 | { |
||
| 212 | $this->remove(); |
||
| 213 | return false; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | self::$cache[$key] = $data; |
||
| 217 | return $data; |
||
| 218 | } |
||
| 219 | |||
| 220 | 45 | public function set($data) |
|
| 221 | { |
||
| 222 | 45 | $fileName = $this->getFilename(); |
|
| 223 | 45 | $this->prepare(); |
|
| 224 | 45 | $key = $this->getCacheKey(); |
|
| 225 | 45 | self::$cache[$key] = $data; |
|
| 226 | |||
| 227 | 45 | file_put_contents($fileName, PhpExporter::export($data)); |
|
| 228 | 45 | @chmod($fileName, 0666); |
|
| 229 | 45 | $this->nsCache->set(); |
|
| 230 | 45 | return $data; |
|
| 231 | } |
||
| 232 | |||
| 233 | public function remove() |
||
| 234 | { |
||
| 235 | $fileName = $this->getFilename(); |
||
| 236 | $key = $this->getCacheKey(); |
||
| 237 | unset(self::$cache[$key]); |
||
| 238 | if (file_exists($fileName)) |
||
| 239 | { |
||
| 240 | return unlink($fileName); |
||
| 241 | } |
||
| 242 | return false; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Clear entire cache |
||
| 247 | * @return boolean |
||
| 248 | */ |
||
| 249 | 3 | public function clear() |
|
| 254 | |||
| 255 | 3 | private function clearCurrentPath() |
|
| 260 | |||
| 261 | 6 | private function clearPath($path) |
|
| 274 | |||
| 275 | 52 | private function getFilename() |
|
| 292 | |||
| 293 | 50 | private function getCacheKey() |
|
| 294 | { |
||
| 295 | 50 | if (is_object($this->component)) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Convert slash separated class name to dot separated name. |
||
| 308 | * @param string $className |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 52 | private function classToFile($className) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Recursively create dir with proper permissions. |
||
| 318 | * |
||
| 319 | * @param string $path |
||
| 320 | */ |
||
| 321 | 7 | private function mkdir($path) |
|
| 327 | |||
| 328 | } |
||
| 329 |