Complex classes like AdapterFile 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 AdapterFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class AdapterFile implements iAdapter |
||
13 | { |
||
14 | const CACHE_FILE_PREFIX = '__'; |
||
15 | const CACHE_FILE_SUBFIX = '.php.cache'; |
||
16 | |||
17 | /** |
||
18 | * @var bool |
||
19 | */ |
||
20 | public $installed = false; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $cacheDir; |
||
26 | |||
27 | /** |
||
28 | * @var iSerializer |
||
29 | */ |
||
30 | protected $serializer; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $fileMode = '0755'; |
||
36 | |||
37 | /** |
||
38 | * @param string $cacheDir |
||
39 | */ |
||
40 | 11 | public function __construct($cacheDir = null) |
|
54 | |||
55 | /** |
||
56 | * Recursively creates & chmod directories. |
||
57 | * |
||
58 | * @param string $path |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | 11 | protected function createCacheDirectory($path): bool |
|
108 | |||
109 | /** |
||
110 | * @param $cacheFile |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | 3 | protected function deleteFile($cacheFile): bool |
|
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | 1 | public function exists(string $key): bool |
|
132 | |||
133 | /** |
||
134 | * @inheritdoc |
||
135 | */ |
||
136 | 7 | public function get(string $key) |
|
180 | |||
181 | /** |
||
182 | * @inheritdoc |
||
183 | */ |
||
184 | public function installed(): bool |
||
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 3 | public function remove(string $key): bool |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 1 | public function removeAll(): bool |
|
217 | |||
218 | /** |
||
219 | * @inheritdoc |
||
220 | */ |
||
221 | 3 | public function set(string $key, $value): bool |
|
225 | |||
226 | /** |
||
227 | * @inheritdoc |
||
228 | */ |
||
229 | 6 | public function setExpired(string $key, $value, int $ttl = 0): bool |
|
253 | |||
254 | /** |
||
255 | * @param string $key |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | 8 | protected function getFileName(string $key): string |
|
263 | |||
264 | /** |
||
265 | * Set the file-mode for new cache-files. |
||
266 | * |
||
267 | * e.g. '0777', or '0755' ... |
||
268 | * |
||
269 | * @param $fileMode |
||
270 | */ |
||
271 | public function setFileMode($fileMode) |
||
275 | |||
276 | /** |
||
277 | * @param $ttl |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | 7 | protected function ttlHasExpired(int $ttl): bool |
|
289 | |||
290 | /** |
||
291 | * @param mixed $data |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | 7 | protected function validateDataFromCache($data): bool |
|
309 | } |
||
310 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: