Complex classes like DriverAbstract 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 DriverAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class DriverAbstract implements ExtendedCacheItemPoolInterface |
||
29 | { |
||
30 | const DRIVER_CHECK_FAILURE = '%s is not installed or misconfigured, cannot continue.'; |
||
31 | const DRIVER_TAGS_KEY_PREFIX = '_TAG_'; |
||
32 | const DRIVER_DATA_WRAPPER_INDEX = 'd'; |
||
33 | const DRIVER_TIME_WRAPPER_INDEX = 't'; |
||
34 | const DRIVER_TAGS_WRAPPER_INDEX = 'g'; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | public $extension_dir = '_extensions'; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | public $tmp = []; |
||
45 | |||
46 | /** |
||
47 | * @var array default options, this will be merge to Driver's Options |
||
48 | */ |
||
49 | public $config = []; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | public $fallback = false; |
||
55 | |||
56 | /** |
||
57 | * @var mixed Instance of driver service |
||
58 | */ |
||
59 | public $instance; |
||
60 | |||
61 | |||
62 | public function __destruct() |
||
69 | |||
70 | /** |
||
71 | * @param $keyword |
||
72 | * @return string |
||
73 | */ |
||
74 | protected function encodeFilename($keyword) |
||
80 | |||
81 | /** |
||
82 | * @param $config_name |
||
83 | * @param string $value |
||
84 | */ |
||
85 | public function setup($config_name, $value = '') |
||
96 | |||
97 | |||
98 | /** |
||
99 | * @param $file |
||
100 | * @return string |
||
101 | * @throws \Exception |
||
102 | */ |
||
103 | protected function readfile($file) |
||
124 | |||
125 | /** |
||
126 | * Encode data types such as object/array |
||
127 | * for driver that does not support |
||
128 | * non-scalar value |
||
129 | * @param $data |
||
130 | * @return string |
||
131 | */ |
||
132 | protected function encode($data) |
||
136 | |||
137 | /** |
||
138 | * Decode data types such as object/array |
||
139 | * for driver that does not support |
||
140 | * non-scalar value |
||
141 | * @param $value |
||
142 | * @return mixed |
||
143 | */ |
||
144 | protected function decode($value) |
||
153 | |||
154 | /** |
||
155 | * Check phpModules or CGI |
||
156 | * @return bool |
||
157 | */ |
||
158 | protected function isPHPModule() |
||
170 | |||
171 | |||
172 | /** |
||
173 | * @param $class |
||
174 | * @return bool |
||
175 | */ |
||
176 | protected function isExistingDriver($class) |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @param $tag |
||
184 | * @return string |
||
185 | */ |
||
186 | protected function _getTagName($tag) |
||
190 | |||
191 | /** |
||
192 | * @param \phpFastCache\Cache\ExtendedCacheItemInterface $item |
||
193 | * @return array |
||
194 | */ |
||
195 | public function driverPreWrap(ExtendedCacheItemInterface $item) |
||
203 | |||
204 | /** |
||
205 | * @param array $wrapper |
||
206 | * @return mixed |
||
207 | */ |
||
208 | public function driverUnwrapData(array $wrapper) |
||
212 | |||
213 | /** |
||
214 | * @param array $wrapper |
||
215 | * @return mixed |
||
216 | */ |
||
217 | public function driverUnwrapTags(array $wrapper) |
||
221 | |||
222 | |||
223 | /** |
||
224 | * @param array $wrapper |
||
225 | * @return \DateTime |
||
226 | */ |
||
227 | public function driverUnwrapTime(array $wrapper) |
||
231 | |||
232 | /** |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getDriverName() |
||
241 | |||
242 | /** |
||
243 | * @param \phpFastCache\Cache\ExtendedCacheItemInterface $item |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function driverWriteTags(ExtendedCacheItemInterface $item) |
||
303 | |||
304 | /** |
||
305 | * @param $key |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getTagKey($key) |
||
312 | |||
313 | /** |
||
314 | * @param $key |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getTagKeys(array $keys) |
||
325 | |||
326 | /** |
||
327 | * @param string $tagName |
||
328 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
329 | * @throws InvalidArgumentException |
||
330 | */ |
||
331 | public function getItemsByTag($tagName) |
||
346 | |||
347 | /** |
||
348 | * @param array $tagNames |
||
349 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
350 | * @throws InvalidArgumentException |
||
351 | */ |
||
352 | public function getItemsByTags(array $tagNames) |
||
361 | |||
362 | /** |
||
363 | * @param string $tagName |
||
364 | * @return bool|null |
||
365 | * @throws InvalidArgumentException |
||
366 | */ |
||
367 | public function deleteItemsByTag($tagName) |
||
383 | |||
384 | /** |
||
385 | * @param array $tagNames |
||
386 | * @return bool|null |
||
387 | * @throws InvalidArgumentException |
||
388 | */ |
||
389 | public function deleteItemsByTags(array $tagNames) |
||
401 | |||
402 | /** |
||
403 | * Abstract Drivers Methods |
||
404 | */ |
||
405 | |||
406 | /** |
||
407 | * @param string $key |
||
408 | * @return array [ |
||
409 | * 'd' => 'THE ITEM DATA' |
||
410 | * 't' => 'THE ITEM DATE EXPIRATION' |
||
411 | * 'g' => 'THE ITEM TAGS' |
||
412 | * ] |
||
413 | * |
||
414 | */ |
||
415 | abstract public function driverRead($key); |
||
416 | |||
417 | /** |
||
418 | * @param \Psr\Cache\CacheItemInterface $item |
||
419 | * @return mixed |
||
420 | */ |
||
421 | abstract public function driverWrite(CacheItemInterface $item); |
||
422 | |||
423 | /** |
||
424 | * @return bool |
||
425 | */ |
||
426 | abstract public function driverClear(); |
||
427 | |||
428 | /** |
||
429 | * @return bool |
||
430 | */ |
||
431 | abstract public function driverConnect(); |
||
432 | |||
433 | /** |
||
434 | * @param \Psr\Cache\CacheItemInterface $item |
||
435 | * @return bool |
||
436 | */ |
||
437 | abstract public function driverDelete(CacheItemInterface $item); |
||
438 | |||
439 | /** |
||
440 | * @param \Psr\Cache\CacheItemInterface $item |
||
441 | * @return bool |
||
442 | */ |
||
443 | abstract public function driverIsHit(CacheItemInterface $item); |
||
444 | |||
445 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.