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 | 23 | public function __construct($metaClass = null, $component = null, $options = null) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Set working component |
||
| 117 | * @param AnnotatedInterface|string $component |
||
| 118 | */ |
||
| 119 | 50 | public function setComponent($component = null) |
|
| 125 | |||
| 126 | 30 | public function setOptions(MetaOptions $options = null) |
|
| 130 | |||
| 131 | 50 | private function prepare() |
|
| 167 | |||
| 168 | 50 | public function get() |
|
| 216 | |||
| 217 | public function set($data) |
||
| 229 | |||
| 230 | public function remove() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Clear entire cache |
||
| 244 | * @return boolean |
||
| 245 | */ |
||
| 246 | 3 | public function clear() |
|
| 250 | |||
| 251 | private function clearCurrentPath() |
||
| 255 | |||
| 256 | 3 | private function clearPath($path) |
|
| 268 | |||
| 269 | 52 | private function getFilename() |
|
| 286 | |||
| 287 | private function getCacheKey() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Convert slash separated class name to dot separated name. |
||
| 302 | * @param string $className |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | 52 | private function classToFile($className) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Recursively create dir with proper permissions. |
||
| 312 | * |
||
| 313 | * @param string $path |
||
| 314 | */ |
||
| 315 | private function mkdir($path) |
||
| 321 | |||
| 322 | } |
||
| 323 |