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 | 52 | public function __construct($metaClass = null, $component = null, $options = null) |
|
88 | { |
||
89 | 52 | if (null === self::$runtimePath) |
|
90 | { |
||
91 | 1 | self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
|
92 | } |
||
93 | 52 | $this->path = self::$runtimePath . '/addendum'; |
|
94 | 52 | $this->metaClass = $metaClass; |
|
95 | 52 | $this->component = $component; |
|
96 | 52 | if (empty($options)) |
|
97 | { |
||
98 | 3 | $this->instanceId = Addendum::DefaultInstanceId; |
|
99 | } |
||
100 | elseif ($options instanceof Addendum) |
||
101 | { |
||
102 | 50 | $this->instanceId = $options->getInstanceId(); |
|
103 | } |
||
104 | elseif ($options instanceof MetaOptions) |
||
105 | { |
||
106 | $this->instanceId = $options->instanceId; |
||
107 | } |
||
108 | else |
||
109 | { |
||
110 | throw new UnexpectedValueException('Unknown options'); |
||
111 | } |
||
112 | 52 | $this->prepare(); |
|
113 | 52 | $this->addendum = Addendum::fly($this->instanceId); |
|
114 | 52 | $this->nsCache = new NsCache(dirname($this->getFilename()), $this->addendum); |
|
115 | 52 | } |
|
116 | |||
117 | /** |
||
118 | * Set working component |
||
119 | * @param AnnotatedInterface|object|string $component |
||
120 | */ |
||
121 | 50 | public function setComponent($component = null) |
|
127 | |||
128 | 31 | public function setOptions(MetaOptions $options = null) |
|
133 | |||
134 | 52 | private function prepare() |
|
135 | { |
||
136 | 52 | $fileDir = dirname($this->getFilename()); |
|
137 | 52 | if (isset(self::$prepared[$fileDir]) && self::$prepared[$fileDir]) |
|
138 | { |
||
139 | 50 | return true; |
|
140 | } |
||
141 | 5 | if (!file_exists($this->path)) |
|
142 | { |
||
143 | 1 | 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 | 1 | if (is_writable(self::$runtimePath)) |
|
156 | { |
||
157 | 1 | $this->mkdir($this->path); |
|
158 | } |
||
159 | 1 | 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 | 5 | if (!file_exists($fileDir)) |
|
165 | { |
||
166 | 2 | $this->mkdir($fileDir); |
|
167 | } |
||
168 | 5 | self::$prepared[$fileDir] = true; |
|
169 | 5 | } |
|
170 | |||
171 | 50 | public function get() |
|
172 | { |
||
173 | 50 | $this->prepare(); |
|
174 | 50 | $fileName = $this->getFilename(); |
|
175 | |||
176 | 50 | if (NsCache::$addeNs && !$this->nsCache->valid()) |
|
177 | { |
||
178 | 1 | $this->clearCurrentPath(); |
|
179 | 1 | return false; |
|
180 | } |
||
181 | 50 | $key = $this->getCacheKey(); |
|
182 | 50 | if (isset(self::$cache[$key])) |
|
183 | { |
||
184 | 50 | return self::$cache[$key]; |
|
185 | } |
||
186 | |||
187 | 49 | $data = SoftIncluder::includeFile($fileName); |
|
188 | |||
189 | // Only false means not existing cache. |
||
190 | // NOTE: Cache might have valid `empty` value, ie. empty array. |
||
191 | 49 | if (false === $data) |
|
192 | { |
||
193 | 49 | 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 | 49 | public function set($data) |
|
234 | |||
235 | public function remove() |
||
236 | { |
||
237 | $fileName = $this->getFilename(); |
||
238 | $key = $this->getCacheKey(); |
||
239 | unset(self::$cache[$key]); |
||
240 | if (file_exists($fileName)) |
||
241 | { |
||
242 | return unlink($fileName); |
||
243 | } |
||
244 | return false; |
||
245 | } |
||
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) |
|
264 | { |
||
265 | 4 | if (!file_exists($path)) |
|
266 | { |
||
267 | return false; |
||
268 | } |
||
269 | 4 | foreach (new \DirectoryIterator($path) as $dir) |
|
270 | { |
||
271 | 4 | if ($dir->isDot() || !$dir->isDir()) |
|
272 | { |
||
273 | 4 | continue; |
|
274 | } |
||
275 | 3 | foreach (new \DirectoryIterator($dir->getPathname()) as $file) |
|
276 | { |
||
277 | 3 | if (!$file->isFile()) |
|
278 | { |
||
279 | 3 | continue; |
|
280 | } |
||
281 | |||
282 | // Skip ns cache file, or it might regenerate over and over |
||
283 | // ns file cache is replaced when needed by NsCache |
||
284 | 2 | if ($file->getBasename() === NsCache::FileName) |
|
285 | { |
||
286 | 2 | continue; |
|
287 | } |
||
288 | 3 | unlink($file->getPathname()); |
|
289 | } |
||
290 | } |
||
291 | 4 | self::$prepared[$path] = false; |
|
292 | 4 | } |
|
293 | |||
294 | 52 | private function getFilename() |
|
295 | { |
||
296 | 52 | if (!empty($this->fileName)) |
|
297 | { |
||
298 | 52 | return $this->fileName; |
|
299 | } |
||
300 | 52 | if (is_object($this->component)) |
|
301 | { |
||
302 | 30 | $className = get_class($this->component); |
|
303 | } |
||
304 | 52 | elseif (is_string($this->component) || null === $this->component) |
|
305 | { |
||
306 | 52 | $className = $this->component; |
|
307 | } |
||
308 | else |
||
309 | { |
||
310 | throw new UnexpectedValueException(sprintf('Expected string or object or null got: `%s`', gettype($className))); |
||
311 | } |
||
312 | $params = [ |
||
313 | 52 | (string) $this->path, |
|
314 | 52 | (string) $this->classToFile($this->metaClass), |
|
315 | 52 | (string) $this->instanceId, |
|
316 | 52 | (string) str_replace('\\', '/', $this->classToFile($className)) |
|
317 | ]; |
||
318 | 52 | $this->fileName = vsprintf('%s/%s@%s/%s.php', $params); |
|
319 | 52 | return $this->fileName; |
|
320 | } |
||
321 | |||
322 | 50 | private function getCacheKey() |
|
323 | { |
||
324 | 50 | if (is_object($this->component)) |
|
325 | { |
||
326 | 30 | $className = get_class($this->component); |
|
327 | } |
||
328 | else |
||
329 | { |
||
330 | 50 | $className = $this->component; |
|
331 | } |
||
332 | 50 | return sprintf('%s@%s@%s@%s.php', $this->classToFile(static::class), $this->classToFile($this->metaClass), $this->instanceId, str_replace('\\', '/', $this->classToFile($className))); |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Convert slash separated class name to dot separated name. |
||
337 | * @param string $className |
||
338 | * @return string |
||
339 | */ |
||
340 | 52 | 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 |