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 | 49 | public function __construct($metaClass = null, $component = null, $options = null) |
|
88 | { |
||
89 | 49 | if (null === self::$runtimePath) |
|
90 | 49 | { |
|
91 | 1 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
|
92 | 1 | } |
|
93 | 49 | $this->path = self::$runtimePath . '/addendum'; |
|
94 | 49 | $this->metaClass = $metaClass; |
|
95 | 49 | $this->component = $component; |
|
96 | 49 | if (empty($options)) |
|
97 | 49 | { |
|
98 | 3 | $this->instanceId = Addendum::DefaultInstanceId; |
|
99 | 3 | } |
|
100 | 47 | elseif ($options instanceof Addendum) |
|
101 | { |
||
102 | 47 | $this->instanceId = $options->getInstanceId(); |
|
103 | 47 | } |
|
104 | elseif ($options instanceof MetaOptions) |
||
105 | { |
||
106 | $this->instanceId = $options->instanceId; |
||
107 | } |
||
108 | else |
||
109 | { |
||
110 | throw new UnexpectedValueException('Unknown options'); |
||
111 | } |
||
112 | 49 | $this->prepare(); |
|
113 | 49 | $this->addendum = Addendum::fly($this->instanceId); |
|
114 | 49 | $this->nsCache = new NsCache(dirname($this->getFilename()), $this->addendum); |
|
115 | 49 | } |
|
116 | |||
117 | /** |
||
118 | * Set working component |
||
119 | * @param AnnotatedInterface|object|string $component |
||
120 | */ |
||
121 | 54 | public function setComponent($component = null) |
|
127 | |||
128 | 36 | public function setOptions(MetaOptions $options = null) |
|
133 | |||
134 | 56 | private function prepare() |
|
170 | |||
171 | 54 | public function get() |
|
172 | { |
||
173 | 54 | $this->prepare(); |
|
174 | 54 | $fileName = $this->getFilename(); |
|
175 | |||
176 | 54 | if (NsCache::$addeNs && !$this->nsCache->valid()) |
|
177 | 54 | { |
|
178 | 1 | $this->clearCurrentPath(); |
|
179 | 1 | return false; |
|
180 | } |
||
181 | 54 | $key = $this->getCacheKey(); |
|
182 | 54 | if (isset(self::$cache[$key])) |
|
183 | 54 | { |
|
184 | 54 | return self::$cache[$key]; |
|
185 | } |
||
186 | |||
187 | 46 | $data = SoftIncluder::includeFile($fileName); |
|
188 | |||
189 | // Only false means not existing cache. |
||
190 | // NOTE: Cache might have valid `empty` value, ie. empty array. |
||
191 | 46 | if (false === $data) |
|
192 | 46 | { |
|
193 | 46 | return false; |
|
194 | } |
||
195 | |||
196 | // Purge file cache if checkMTime is enabled and file obsolete |
||
197 | if ($this->addendum->checkMTime && file_exists($fileName)) |
||
198 | { |
||
199 | $cacheTime = filemtime($fileName); |
||
200 | |||
201 | // Partial component name, split by @ and take first argument |
||
202 | if (is_string($this->component) && strstr($this->component, '@')) |
||
203 | { |
||
204 | $parts = explode('@', $this->component); |
||
205 | $componentClass = array_shift($parts); |
||
206 | } |
||
207 | else |
||
208 | { |
||
209 | $componentClass = $this->component; |
||
210 | } |
||
211 | $componentTime = filemtime((new ReflectionClass($componentClass))->getFileName()); |
||
212 | if ($componentTime > $cacheTime) |
||
213 | { |
||
214 | $this->remove(); |
||
215 | return false; |
||
216 | } |
||
217 | } |
||
218 | self::$cache[$key] = $data; |
||
219 | return $data; |
||
220 | } |
||
221 | |||
222 | 46 | public function set($data) |
|
223 | { |
||
224 | 46 | $fileName = $this->getFilename(); |
|
225 | 46 | $this->prepare(); |
|
226 | 46 | $key = $this->getCacheKey(); |
|
227 | 46 | self::$cache[$key] = $data; |
|
228 | |||
229 | 46 | file_put_contents($fileName, PhpExporter::export($data)); |
|
230 | 46 | @chmod($fileName, 0666); |
|
231 | 46 | $this->nsCache->set(); |
|
232 | 46 | return $data; |
|
233 | } |
||
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 | 56 | private function getFilename() |
|
295 | { |
||
296 | 56 | if (!empty($this->fileName)) |
|
321 | |||
322 | 54 | private function getCacheKey() |
|
334 | |||
335 | /** |
||
336 | * Convert slash separated class name to dot separated name. |
||
337 | * @param string $className |
||
338 | * @return string |
||
339 | */ |
||
340 | 56 | private function classToFile($className) |
|
344 | |||
345 | /** |
||
346 | * Recursively create dir with proper permissions. |
||
347 | * |
||
348 | * @param string $path |
||
349 | */ |
||
350 | 2 | private function mkdir($path) |
|
356 | |||
357 | } |
||
358 |