Complex classes like Driver 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 Driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Driver extends DriverAbstract |
||
31 | { |
||
32 | use PathSeekerTrait, StandardPsr6StructureTrait; |
||
33 | |||
34 | /** |
||
35 | * Driver constructor. |
||
36 | * @param array $config |
||
37 | * @throws phpFastCacheDriverException |
||
38 | */ |
||
39 | public function __construct(array $config = []) |
||
47 | |||
48 | /** |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function driverCheck() |
||
55 | |||
56 | /** |
||
57 | * @param \Psr\Cache\CacheItemInterface $item |
||
58 | * @return mixed |
||
59 | * @throws \InvalidArgumentException |
||
60 | */ |
||
61 | public function driverWrite(CacheItemInterface $item) |
||
99 | |||
100 | /** |
||
101 | * @param \Psr\Cache\CacheItemInterface $key |
||
102 | * @return mixed |
||
103 | * @throws \InvalidArgumentException |
||
104 | */ |
||
105 | public function driverRead($key) |
||
127 | |||
128 | /** |
||
129 | * @param \Psr\Cache\CacheItemInterface $item |
||
130 | * @return bool |
||
131 | * @throws \InvalidArgumentException |
||
132 | */ |
||
133 | public function driverDelete(CacheItemInterface $item) |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function driverClear() |
||
154 | { |
||
155 | $return = null; |
||
156 | $path = $this->getPath(); |
||
157 | $dir = @opendir($path); |
||
158 | if (!$dir) { |
||
159 | throw new phpFastCacheDriverException("Can't read PATH:" . $path); |
||
160 | } |
||
161 | |||
162 | while ($file = readdir($dir)) { |
||
163 | if ($file != '.' && $file != '..' && is_dir($path . '/' . $file)) { |
||
164 | // read sub dir |
||
165 | $subdir = @opendir($path . '/' . $file); |
||
166 | if (!$subdir) { |
||
167 | throw new phpFastCacheDriverException("Can't read path:" . $path . '/' . $file); |
||
168 | } |
||
169 | |||
170 | while ($subdirFile = readdir($subdir)) { |
||
171 | if ($subdirFile != '.' && $subdirFile != '..') { |
||
172 | $file_path = $path . '/' . $file . '/' . $subdirFile; |
||
173 | $result = @unlink($file_path); |
||
174 | if ($return !== false) { |
||
175 | $return = $result; |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | return (bool) $return; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function driverConnect() |
||
192 | |||
193 | /** |
||
194 | * @param \Psr\Cache\CacheItemInterface $item |
||
195 | * @return bool |
||
196 | * @throws \InvalidArgumentException |
||
197 | */ |
||
198 | public function driverIsHit(CacheItemInterface $item) |
||
210 | |||
211 | /** |
||
212 | * @param string $optionName |
||
213 | * @param mixed $optionValue |
||
214 | * @return bool |
||
215 | * @throws \InvalidArgumentException |
||
216 | */ |
||
217 | public static function isValidOption($optionName, $optionValue) |
||
241 | /** |
||
242 | * @return array |
||
243 | */ |
||
244 | public static function getValidOptions() |
||
248 | |||
249 | /** |
||
250 | * @return array |
||
251 | */ |
||
252 | public static function getRequiredOptions() |
||
256 | |||
257 | /******************** |
||
258 | * |
||
259 | * PSR-6 Extended Methods |
||
260 | * |
||
261 | *******************/ |
||
262 | |||
263 | /** |
||
264 | * @return driverStatistic |
||
265 | * @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
||
266 | * @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
||
267 | */ |
||
268 | public function getStats() |
||
322 | } |
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.