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 |
||
| 26 | abstract class PhpCache |
||
| 27 | { |
||
| 28 | |||
| 29 | private $metaClass = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var AnnotatedInterface|object|string |
||
| 34 | */ |
||
| 35 | private $component = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Options |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $instanceId = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Addendum runtime path |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $path = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | * @var NsCache |
||
| 52 | */ |
||
| 53 | private $nsCache = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | * @var Addendum |
||
| 58 | */ |
||
| 59 | private $addendum = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Runtime path |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private static $runtimePath = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Local cacheq |
||
| 69 | * @var type |
||
| 70 | */ |
||
| 71 | private static $cache = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Hash map of prepared directories |
||
| 75 | * Key is directory, value is flag indicating if it's prepared. |
||
| 76 | * @var bool[] |
||
| 77 | */ |
||
| 78 | private static $prepared = []; |
||
| 79 | private $fileName = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * |
||
| 83 | * @param string $metaClass |
||
| 84 | * @param AnnotatedInterface|object|string $component |
||
| 85 | * @param MetaOptions|Addendum $options |
||
| 86 | */ |
||
| 87 | 51 | public function __construct($metaClass = null, $component = null, $options = null) |
|
| 88 | { |
||
| 89 | 51 | if (null === self::$runtimePath) |
|
| 90 | 51 | { |
|
| 91 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
||
| 92 | } |
||
| 93 | 51 | $this->path = self::$runtimePath . '/addendum'; |
|
| 94 | 51 | $this->metaClass = $metaClass; |
|
| 95 | 51 | $this->component = $component; |
|
| 96 | 51 | if (empty($options)) |
|
| 97 | 51 | { |
|
| 98 | 3 | $this->instanceId = Addendum::DefaultInstanceId; |
|
| 99 | 3 | } |
|
| 100 | 49 | elseif ($options instanceof Addendum) |
|
| 101 | { |
||
| 102 | 49 | $this->instanceId = $options->getInstanceId(); |
|
| 103 | 49 | } |
|
| 104 | elseif ($options instanceof MetaOptions) |
||
| 105 | { |
||
| 106 | $this->instanceId = $options->instanceId; |
||
| 107 | } |
||
| 108 | else |
||
| 109 | { |
||
| 110 | throw new UnexpectedValueException('Unknown options'); |
||
| 111 | } |
||
| 112 | 51 | $this->prepare(); |
|
| 113 | 51 | $this->addendum = Addendum::fly($this->instanceId); |
|
| 114 | 51 | $this->nsCache = new NsCache(dirname($this->getFilename()), $this->addendum); |
|
| 115 | 51 | } |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Set working component |
||
| 119 | * @param AnnotatedInterface|object|string $component |
||
| 120 | */ |
||
| 121 | 56 | public function setComponent($component = null) |
|
| 127 | |||
| 128 | 37 | public function setOptions(MetaOptions $options = null) |
|
| 133 | |||
| 134 | 58 | private function prepare() |
|
| 135 | { |
||
| 136 | 58 | $fileDir = dirname($this->getFilename()); |
|
| 137 | 58 | if (isset(self::$prepared[$fileDir]) && self::$prepared[$fileDir]) |
|
| 138 | 58 | { |
|
| 139 | 49 | return true; |
|
| 140 | } |
||
| 141 | 58 | if (!file_exists($this->path)) |
|
| 142 | 58 | { |
|
| 143 | if (!file_exists(self::$runtimePath)) |
||
| 144 | { |
||
| 145 | |||
| 146 | if (is_writable(dirname(self::$runtimePath))) |
||
| 147 | { |
||
| 148 | $this->mkdir(self::$runtimePath); |
||
| 149 | } |
||
| 150 | if (!is_writable(self::$runtimePath)) |
||
| 151 | { |
||
| 152 | throw new RuntimeException(sprintf("Runtime path `%s` must exists and be writable", self::$runtimePath)); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | if (is_writable(self::$runtimePath)) |
||
| 156 | { |
||
| 157 | $this->mkdir($this->path); |
||
| 158 | } |
||
| 159 | if (!is_writable($this->path)) |
||
| 160 | { |
||
| 161 | throw new RuntimeException(sprintf("Addendum runtime path `%s` must exists and be writable", $this->path)); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | 58 | if (!file_exists($fileDir)) |
|
| 165 | 58 | { |
|
| 166 | 1 | $this->mkdir($fileDir); |
|
| 167 | 1 | } |
|
| 168 | 58 | self::$prepared[$fileDir] = true; |
|
| 169 | 58 | } |
|
| 170 | |||
| 171 | 56 | public function get() |
|
| 221 | |||
| 222 | 48 | public function set($data) |
|
| 234 | |||
| 235 | public function remove() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Clear entire cache |
||
| 249 | * @return boolean |
||
| 250 | */ |
||
| 251 | 3 | public function clear() |
|
| 256 | |||
| 257 | 1 | private function clearCurrentPath() |
|
| 262 | |||
| 263 | 4 | private function clearPath($path) |
|
| 293 | |||
| 294 | 58 | private function getFilename() |
|
| 321 | |||
| 322 | 56 | private function getCacheKey() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Convert slash separated class name to dot separated name. |
||
| 337 | * @param string $className |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | 58 | private function classToFile($className) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Recursively create dir with proper permissions. |
||
| 347 | * |
||
| 348 | * @param string $path |
||
| 349 | */ |
||
| 350 | 1 | private function mkdir($path) |
|
| 356 | |||
| 357 | } |
||
| 358 |