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 | 49 | public function __construct($metaClass = null, $component = null, $options = null) |
|
| 86 | { |
||
| 87 | 49 | if (null === self::$runtimePath) |
|
| 88 | { |
||
| 89 | 1 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
|
| 90 | } |
||
| 91 | 49 | $this->path = self::$runtimePath . '/addendum'; |
|
| 92 | 49 | $this->metaClass = $metaClass; |
|
| 93 | 49 | $this->component = $component; |
|
| 94 | 49 | if (empty($options)) |
|
| 95 | { |
||
| 96 | 3 | $this->instanceId = Addendum::DefaultInstanceId; |
|
| 97 | } |
||
| 98 | elseif ($options instanceof Addendum) |
||
| 99 | { |
||
| 100 | 47 | $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 | 49 | $this->prepare(); |
|
| 111 | 49 | $this->addendum = Addendum::fly($this->instanceId); |
|
| 112 | 49 | $this->nsCache = new NsCache(dirname($this->getFilename()), $this->addendum); |
|
| 113 | 49 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Set working component |
||
| 117 | * @param AnnotatedInterface|string $component |
||
| 118 | */ |
||
| 119 | 51 | public function setComponent($component = null) |
|
| 125 | |||
| 126 | 33 | public function setOptions(MetaOptions $options = null) |
|
| 131 | |||
| 132 | 53 | private function prepare() |
|
| 133 | { |
||
| 134 | 53 | $fileDir = dirname($this->getFilename()); |
|
| 135 | 53 | if (isset(self::$prepared[$fileDir]) && self::$prepared[$fileDir]) |
|
| 136 | { |
||
| 137 | 51 | return true; |
|
| 138 | } |
||
| 139 | 5 | if (!file_exists($this->path)) |
|
| 140 | { |
||
| 141 | 1 | if (!file_exists(self::$runtimePath)) |
|
| 142 | { |
||
| 143 | |||
| 144 | if (is_writable(dirname(self::$runtimePath))) |
||
| 145 | { |
||
| 146 | $this->mkdir(self::$runtimePath); |
||
| 147 | } |
||
| 148 | if (!is_writable(self::$runtimePath)) |
||
| 149 | { |
||
| 150 | throw new RuntimeException(sprintf("Runtime path `%s` must exists and be writable", self::$runtimePath)); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | 1 | if (is_writable(self::$runtimePath)) |
|
| 154 | { |
||
| 155 | 1 | $this->mkdir($this->path); |
|
| 156 | } |
||
| 157 | 1 | if (!is_writable($this->path)) |
|
| 158 | { |
||
| 159 | throw new RuntimeException(sprintf("Addendum runtime path `%s` must exists and be writable", $this->path)); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | 5 | if (!file_exists($fileDir)) |
|
| 163 | { |
||
| 164 | 2 | $this->mkdir($fileDir); |
|
| 165 | } |
||
| 166 | 5 | self::$prepared[$fileDir] = true; |
|
| 167 | 5 | } |
|
| 168 | |||
| 169 | 51 | public function get() |
|
| 170 | { |
||
| 171 | 51 | $this->prepare(); |
|
| 172 | 51 | $fileName = $this->getFilename(); |
|
| 173 | |||
| 174 | 51 | if (NsCache::$addeNs && !$this->nsCache->valid()) |
|
| 175 | { |
||
| 176 | 1 | $this->clearCurrentPath(); |
|
| 177 | 1 | return false; |
|
| 178 | } |
||
| 179 | 51 | $key = $this->getCacheKey(); |
|
| 180 | 51 | if (isset(self::$cache[$key])) |
|
| 181 | { |
||
| 182 | 51 | return self::$cache[$key]; |
|
| 183 | } |
||
| 184 | |||
| 185 | 46 | $data = SoftIncluder::includeFile($fileName); |
|
| 186 | |||
| 187 | // Only false means not existing cache. |
||
| 188 | // NOTE: Cache might have valid `empty` value, ie. empty array. |
||
| 189 | 46 | if (false === $data) |
|
| 190 | { |
||
| 191 | 46 | 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 | 46 | public function set($data) |
|
| 221 | { |
||
| 222 | 46 | $fileName = $this->getFilename(); |
|
| 223 | 46 | $this->prepare(); |
|
| 224 | 46 | $key = $this->getCacheKey(); |
|
| 225 | 46 | self::$cache[$key] = $data; |
|
| 226 | |||
| 227 | 46 | file_put_contents($fileName, PhpExporter::export($data)); |
|
| 228 | 46 | @chmod($fileName, 0666); |
|
| 229 | 46 | $this->nsCache->set(); |
|
| 230 | 46 | 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 | 1 | private function clearCurrentPath() |
|
| 260 | |||
| 261 | 4 | private function clearPath($path) |
|
| 262 | { |
||
| 263 | 4 | if (!file_exists($path)) |
|
| 264 | { |
||
| 291 | |||
| 292 | 53 | private function getFilename() |
|
| 309 | |||
| 310 | 51 | private function getCacheKey() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Convert slash separated class name to dot separated name. |
||
| 325 | * @param string $className |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 53 | private function classToFile($className) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Recursively create dir with proper permissions. |
||
| 335 | * |
||
| 336 | * @param string $path |
||
| 337 | */ |
||
| 338 | 2 | private function mkdir($path) |
|
| 344 | |||
| 345 | } |
||
| 346 |