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 |
||
10 | class AdapterFile implements iAdapter |
||
11 | { |
||
12 | const CACHE_FILE_PREFIX = '__'; |
||
13 | const CACHE_FILE_SUBFIX = '.php.cache'; |
||
14 | |||
15 | /** |
||
16 | * @var bool |
||
17 | */ |
||
18 | public $installed = false; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $cacheDir; |
||
24 | |||
25 | /** |
||
26 | * @var iSerializer |
||
27 | */ |
||
28 | protected $serializer; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $fileMode = '0755'; |
||
34 | |||
35 | /** |
||
36 | * @param string $cacheDir |
||
37 | */ |
||
38 | 9 | public function __construct($cacheDir = null) |
|
52 | |||
53 | /** |
||
54 | * Recursively creates & chmod directories. |
||
55 | * |
||
56 | * @param string $path |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | 9 | protected function createCacheDirectory($path) |
|
106 | |||
107 | /** |
||
108 | * @param $cacheFile |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 1 | protected function deleteFile($cacheFile) |
|
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | 1 | public function exists($key) |
|
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | */ |
||
132 | 5 | public function get($key) |
|
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | public function installed() |
||
187 | |||
188 | /** |
||
189 | * @inheritdoc |
||
190 | */ |
||
191 | 1 | public function remove($key) |
|
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 1 | public function removeAll() |
|
216 | |||
217 | /** |
||
218 | * @inheritdoc |
||
219 | */ |
||
220 | 3 | public function set($key, $value) |
|
224 | |||
225 | /** |
||
226 | * @inheritdoc |
||
227 | */ |
||
228 | 4 | public function setExpired($key, $value, $ttl = 0) |
|
252 | |||
253 | /** |
||
254 | * @param string $key |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | 6 | protected function getFileName($key) |
|
262 | |||
263 | /** |
||
264 | * Set the file-mode for new cache-files. |
||
265 | * |
||
266 | * e.g. '0777', or '0755' ... |
||
267 | * |
||
268 | * @param $fileMode |
||
269 | */ |
||
270 | public function setFileMode($fileMode) |
||
274 | |||
275 | /** |
||
276 | * @param $ttl |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 5 | protected function ttlHasExpired($ttl) |
|
288 | |||
289 | /** |
||
290 | * @param $data |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 5 | protected function validateDataFromCache($data) |
|
308 | } |
||
309 |
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: