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 | 11 | public function __construct($cacheDir = null) |
|
52 | |||
53 | /** |
||
54 | * Recursively creates & chmod directories. |
||
55 | * |
||
56 | * @param string $path |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | 11 | protected function createCacheDirectory($path) |
|
106 | |||
107 | /** |
||
108 | * @param $cacheFile |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 3 | protected function deleteFile($cacheFile) |
|
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | 1 | public function exists($key) |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 7 | public function get($key) |
|
135 | { |
||
136 | 7 | $path = $this->getFileName($key); |
|
137 | |||
138 | if ( |
||
139 | 7 | file_exists($path) === false |
|
140 | 7 | || |
|
141 | 7 | filesize($path) === 0 |
|
142 | 7 | ) { |
|
143 | 1 | return null; |
|
144 | } |
||
145 | |||
146 | // init |
||
147 | 7 | $string = ''; |
|
148 | |||
149 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
150 | 7 | $fp = @fopen($path, 'rb'); |
|
151 | 7 | if ($fp && flock($fp, LOCK_SH | LOCK_NB)) { |
|
152 | 7 | while (!feof($fp)) { |
|
153 | 7 | $line = fgets($fp); |
|
154 | 7 | $string .= $line; |
|
155 | 7 | } |
|
156 | 7 | flock($fp, LOCK_UN); |
|
157 | 7 | } |
|
158 | 7 | fclose($fp); |
|
159 | |||
160 | 7 | if (!$string) { |
|
161 | return null; |
||
162 | } |
||
163 | |||
164 | 7 | $data = $this->serializer->unserialize(file_get_contents($path)); |
|
165 | |||
166 | 7 | if (!$data || !$this->validateDataFromCache($data)) { |
|
167 | return null; |
||
168 | } |
||
169 | |||
170 | 7 | if ($this->ttlHasExpired($data['ttl']) === true) { |
|
171 | 2 | $this->remove($key); |
|
172 | |||
173 | 2 | return null; |
|
174 | } |
||
175 | |||
176 | 6 | return $data['value']; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @inheritdoc |
||
181 | */ |
||
182 | public function installed() |
||
186 | |||
187 | /** |
||
188 | * @inheritdoc |
||
189 | */ |
||
190 | 3 | public function remove($key) |
|
196 | |||
197 | /** |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | 1 | public function removeAll() |
|
201 | { |
||
202 | 1 | if (!$this->cacheDir) { |
|
203 | return false; |
||
204 | } |
||
205 | |||
206 | 1 | $return = array(); |
|
207 | 1 | foreach (new \DirectoryIterator($this->cacheDir) as $fileInfo) { |
|
208 | 1 | if (!$fileInfo->isDot()) { |
|
209 | 1 | $return[] = unlink($fileInfo->getPathname()); |
|
210 | 1 | } |
|
211 | 1 | } |
|
212 | |||
213 | 1 | return in_array(false, $return, true) === false; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | 3 | public function set($key, $value) |
|
223 | |||
224 | /** |
||
225 | * @inheritdoc |
||
226 | */ |
||
227 | 6 | public function setExpired($key, $value, $ttl = 0) |
|
228 | { |
||
229 | 6 | $item = $this->serializer->serialize( |
|
230 | array( |
||
231 | 6 | 'value' => $value, |
|
232 | 6 | 'ttl' => $ttl ? (int)$ttl + time() : 0, |
|
233 | ) |
||
234 | 6 | ); |
|
235 | |||
236 | 6 | $octetWritten = false; |
|
237 | 6 | $cacheFile = $this->getFileName($key); |
|
238 | |||
239 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
240 | 6 | $fp = @fopen($cacheFile, 'ab'); |
|
241 | 6 | if ($fp && flock($fp, LOCK_EX | LOCK_NB)) { |
|
242 | 6 | ftruncate($fp, 0); |
|
243 | 6 | $octetWritten = fwrite($fp, $item); |
|
244 | 6 | fflush($fp); |
|
245 | 6 | flock($fp, LOCK_UN); |
|
246 | 6 | } |
|
247 | 6 | fclose($fp); |
|
248 | |||
249 | 6 | return $octetWritten !== false; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param string $key |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 8 | protected function getFileName($key) |
|
261 | |||
262 | /** |
||
263 | * Set the file-mode for new cache-files. |
||
264 | * |
||
265 | * e.g. '0777', or '0755' ... |
||
266 | * |
||
267 | * @param $fileMode |
||
268 | */ |
||
269 | public function setFileMode($fileMode) |
||
273 | |||
274 | /** |
||
275 | * @param $ttl |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | 7 | protected function ttlHasExpired($ttl) |
|
287 | |||
288 | /** |
||
289 | * @param $data |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | 7 | protected function validateDataFromCache($data) |
|
307 | } |
||
308 |
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: