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 | 49 | { |
|
| 89 | 1 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
|
| 90 | 1 | } |
|
| 91 | 49 | $this->path = self::$runtimePath . '/addendum'; |
|
| 92 | 49 | $this->metaClass = $metaClass; |
|
| 93 | 49 | $this->component = $component; |
|
| 94 | 49 | if (empty($options)) |
|
| 95 | 49 | { |
|
| 96 | 3 | $this->instanceId = Addendum::DefaultInstanceId; |
|
| 97 | 3 | } |
|
| 98 | 47 | elseif ($options instanceof Addendum) |
|
| 99 | { |
||
| 100 | 47 | $this->instanceId = $options->getInstanceId(); |
|
| 101 | 47 | } |
|
| 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() |
|
| 168 | |||
| 169 | 51 | public function get() |
|
| 225 | |||
| 226 | 46 | public function set($data) |
|
| 238 | |||
| 239 | public function remove() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Clear entire cache |
||
| 253 | * @return boolean |
||
| 254 | */ |
||
| 255 | 3 | public function clear() |
|
| 260 | |||
| 261 | 1 | private function clearCurrentPath() |
|
| 266 | |||
| 267 | 4 | private function clearPath($path) |
|
| 297 | |||
| 298 | 53 | private function getFilename() |
|
| 315 | |||
| 316 | 51 | private function getCacheKey() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Convert slash separated class name to dot separated name. |
||
| 331 | * @param string $className |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | 53 | private function classToFile($className) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Recursively create dir with proper permissions. |
||
| 341 | * |
||
| 342 | * @param string $path |
||
| 343 | */ |
||
| 344 | 2 | private function mkdir($path) |
|
| 350 | |||
| 351 | } |
||
| 352 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.