Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class File { |
||
14 | use \PHPDaemon\Traits\ClassWatchdog; |
||
15 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
16 | |||
17 | /** |
||
18 | * @var integer Priority |
||
19 | */ |
||
20 | public $priority = 10; |
||
21 | |||
22 | /** |
||
23 | * @var integer Chunk size |
||
24 | */ |
||
25 | public $chunkSize = 4096; |
||
26 | |||
27 | /** |
||
28 | * @var string $stat hash Stat |
||
29 | */ |
||
30 | protected $stat; |
||
31 | |||
32 | /** |
||
33 | * @var array Cache of statvfs() |
||
34 | */ |
||
35 | protected $statvfs; |
||
36 | |||
37 | /** |
||
38 | * @var integer Current offset |
||
39 | */ |
||
40 | public $offset = 0; |
||
41 | |||
42 | /** |
||
43 | * @var string Cache key |
||
44 | */ |
||
45 | public $fdCacheKey; |
||
46 | |||
47 | /** |
||
48 | * @var boolean Append? |
||
49 | */ |
||
50 | public $append; |
||
51 | |||
52 | /** |
||
53 | * @var string Path |
||
54 | */ |
||
55 | public $path; |
||
56 | |||
57 | /** |
||
58 | * @var boolean Writing? |
||
59 | */ |
||
60 | public $writing = false; |
||
61 | |||
62 | /** |
||
63 | * @var boolean Closed? |
||
64 | */ |
||
65 | public $closed = false; |
||
66 | |||
67 | /** |
||
68 | * @var object File descriptor |
||
69 | */ |
||
70 | protected $fd; |
||
71 | |||
72 | /** |
||
73 | * @var PHPDaemon\Structures\StackCallbacks Stack of callbacks called when writing is done |
||
74 | */ |
||
75 | protected $onWriteOnce; |
||
76 | |||
77 | /** |
||
78 | * File constructor |
||
79 | * @param resource $fd Descriptor |
||
80 | * @param string $path Path |
||
81 | */ |
||
82 | public function __construct($fd, $path) { |
||
83 | $this->fd = $fd; |
||
|
|||
84 | $this->path = $path; |
||
85 | $this->onWriteOnce = new StackCallbacks; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get file descriptor |
||
90 | * @return resource File descriptor |
||
91 | */ |
||
92 | public function getFd() { |
||
93 | return $this->fd; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Converts string of flags to integer or standard text representation |
||
98 | * @param string $mode Mode |
||
99 | * @param boolean $text Text? |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public static function convertFlags($mode, $text = false) { |
||
103 | $plus = strpos($mode, '+') !== false; |
||
104 | $sync = strpos($mode, 's') !== false; |
||
105 | $type = strtr($mode, ['b' => '', '+' => '', 's' => '', '!' => '']); |
||
106 | if ($text) { |
||
107 | return $type; |
||
108 | } |
||
109 | $types = [ |
||
110 | 'r' => $plus ? EIO_O_RDWR : EIO_O_RDONLY, |
||
111 | 'w' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_CREAT | EIO_O_TRUNC, |
||
112 | 'a' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_CREAT | EIO_O_APPEND, |
||
113 | 'x' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_EXCL | EIO_O_CREAT, |
||
114 | 'c' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_CREAT, |
||
115 | ]; |
||
116 | $m = $types[$type]; |
||
117 | if ($sync) { |
||
118 | $m |= EIO_O_FSYNC; |
||
119 | } |
||
120 | return $m; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Truncates this file |
||
125 | * @param integer $offset Offset. Default is 0 |
||
126 | * @param callable $cb Callback |
||
127 | * @param integer $pri Priority |
||
128 | * @return resource|boolean |
||
129 | */ |
||
130 | View Code Duplication | public function truncate($offset = 0, $cb = null, $pri = EIO_PRI_DEFAULT) { |
|
131 | $cb = CallbackWrapper::forceWrap($cb); |
||
132 | if (!$this->fd || $this->fd === -1) { |
||
133 | if ($cb) { |
||
134 | $cb($this, false); |
||
135 | } |
||
136 | return false; |
||
137 | } |
||
138 | if (!FileSystem::$supported) { |
||
139 | $fp = fopen($this->path, 'r+'); |
||
140 | $r = $fp && ftruncate($fp, $offset); |
||
141 | if ($cb) { |
||
142 | $cb($this, $r); |
||
143 | } |
||
144 | return $r; |
||
145 | } |
||
146 | return eio_ftruncate($this->fd, $offset, $pri, $cb, $this); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Stat() |
||
151 | * @param callable $cb Callback |
||
152 | * @param integer $pri Priority |
||
153 | * @return resource|boolean |
||
154 | */ |
||
155 | public function stat($cb, $pri = EIO_PRI_DEFAULT) { |
||
156 | $cb = CallbackWrapper::forceWrap($cb); |
||
157 | if (!$this->fd || $this->fd === -1) { |
||
158 | if ($cb) { |
||
159 | $cb($this, false); |
||
160 | } |
||
161 | return false; |
||
162 | } |
||
163 | View Code Duplication | if (!FileSystem::$supported) { |
|
164 | $cb($this, FileSystem::statPrepare(fstat($this->fd))); |
||
165 | return false; |
||
166 | } |
||
167 | if ($this->stat) { |
||
168 | $cb($this, $this->stat); |
||
169 | return true; |
||
170 | } |
||
171 | return eio_fstat($this->fd, $pri, function ($file, $stat) use ($cb) { |
||
172 | $stat = FileSystem::statPrepare($stat); |
||
173 | $file->stat = $stat; |
||
174 | $cb($file, $stat); |
||
175 | }, $this); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Stat() non-cached |
||
180 | * @param callable $cb Callback |
||
181 | * @param integer $pri Priority |
||
182 | * @return resource|boolean |
||
183 | */ |
||
184 | public function statRefresh($cb, $pri = EIO_PRI_DEFAULT) { |
||
185 | $cb = CallbackWrapper::forceWrap($cb); |
||
186 | if (!$this->fd || $this->fd === -1) { |
||
187 | if ($cb) { |
||
188 | $cb($this, false); |
||
189 | } |
||
190 | return false; |
||
191 | } |
||
192 | View Code Duplication | if (!FileSystem::$supported) { |
|
193 | $cb($this, FileSystem::statPrepare(fstat($this->fd))); |
||
194 | return true; |
||
195 | } |
||
196 | return eio_fstat($this->fd, $pri, function ($file, $stat) use ($cb) { |
||
197 | $stat = FileSystem::statPrepare($stat); |
||
198 | $file->stat = $stat; |
||
199 | $cb($file, $stat); |
||
200 | }, $this); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Statvfs() |
||
205 | * @param callable $cb Callback |
||
206 | * @param integer $pri Priority |
||
207 | * @return resource|boolean |
||
208 | */ |
||
209 | public function statvfs($cb, $pri = EIO_PRI_DEFAULT) { |
||
210 | $cb = CallbackWrapper::forceWrap($cb); |
||
211 | if (!$this->fd) { |
||
212 | if ($cb) { |
||
213 | $cb($this, false); |
||
214 | } |
||
215 | return false; |
||
216 | } |
||
217 | if (!FileSystem::$supported) { |
||
218 | if ($cb) { |
||
219 | $cb($this, false); |
||
220 | } |
||
221 | return false; |
||
222 | } |
||
223 | if ($this->statvfs) { |
||
224 | $cb($this, $this->statvfs); |
||
225 | return true; |
||
226 | } |
||
227 | return eio_fstatvfs($this->fd, $pri, function ($file, $stat) use ($cb) { |
||
228 | $file->statvfs = $stat; |
||
229 | $cb($file, $stat); |
||
230 | }, $this); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Sync() |
||
235 | * @param callable $cb Callback |
||
236 | * @param integer $pri Priority |
||
237 | * @return resource|false |
||
238 | */ |
||
239 | View Code Duplication | public function sync($cb, $pri = EIO_PRI_DEFAULT) { |
|
253 | |||
254 | /** |
||
255 | * Datasync() |
||
256 | * @param callable $cb Callback |
||
257 | * @param integer $pri Priority |
||
258 | * @return resource|false |
||
259 | */ |
||
260 | View Code Duplication | public function datasync($cb, $pri = EIO_PRI_DEFAULT) { |
|
274 | |||
275 | /** |
||
276 | * Writes data to file |
||
277 | * @param string $data Data |
||
278 | * @param callable $cb Callback |
||
279 | * @param integer $offset Offset |
||
280 | * @param integer $pri Priority |
||
281 | * @return resource|false |
||
282 | */ |
||
283 | public function write($data, $cb = null, $offset = null, $pri = EIO_PRI_DEFAULT) { |
||
284 | $cb = CallbackWrapper::forceWrap($cb); |
||
285 | if (!$this->fd) { |
||
286 | if ($cb) { |
||
287 | $cb($this, false); |
||
288 | } |
||
289 | return false; |
||
290 | } |
||
291 | if ($data === '') { |
||
292 | if ($cb) { |
||
293 | $cb($this, 0); |
||
294 | } |
||
295 | return false; |
||
296 | } |
||
297 | if (!FileSystem::$supported) { |
||
298 | if ($offset !== null) { |
||
299 | fseek($data, $offset); |
||
300 | } |
||
301 | $r = fwrite($this->fd, $data); |
||
302 | if ($cb) { |
||
303 | $cb($this, $r); |
||
304 | } |
||
305 | return false; |
||
306 | } |
||
307 | if ($cb !== null) { |
||
308 | $this->onWriteOnce->push($cb); |
||
309 | } |
||
310 | $l = strlen($data); |
||
311 | if ($offset === null) { |
||
312 | $offset = $this->offset; |
||
313 | $this->offset += $l; |
||
314 | } |
||
315 | $this->writing = true; |
||
316 | $res = eio_write($this->fd, $data, $l, $offset, $pri, function ($file, $result) { |
||
317 | $this->writing = false; |
||
318 | $this->onWriteOnce->executeAll($file, $result); |
||
319 | }, $this); |
||
320 | return $res; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Changes ownership of this file |
||
325 | * @param integer $uid User ID |
||
326 | * @param integer $gid Group ID |
||
327 | * @param callable $cb Callback |
||
328 | * @param integer $pri Priority |
||
329 | * @return resource|false |
||
330 | */ |
||
331 | View Code Duplication | public function chown($uid, $gid = -1, $cb, $pri = EIO_PRI_DEFAULT) { |
|
332 | $cb = CallbackWrapper::forceWrap($cb); |
||
333 | if (!$this->fd) { |
||
334 | if ($cb) { |
||
335 | $cb($this, false); |
||
336 | } |
||
337 | return false; |
||
338 | } |
||
339 | if (!FileSystem::$supported) { |
||
340 | $r = chown($this->path, $uid); |
||
341 | if ($gid !== -1) { |
||
342 | $r = $r && chgrp($this->path, $gid); |
||
343 | } |
||
344 | if ($cb) { |
||
345 | $cb($this, $r); |
||
346 | } |
||
347 | return false; |
||
348 | } |
||
349 | return eio_fchown($this->fd, $uid, $gid, $pri, $cb, $this); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * touch() |
||
354 | * @param integer $mtime Last modification time |
||
355 | * @param integer $atime Last access time |
||
356 | * @param callable $cb Callback |
||
357 | * @param integer $pri Priority |
||
358 | * @return resource|false |
||
359 | */ |
||
360 | public function touch($mtime, $atime = null, $cb = null, $pri = EIO_PRI_DEFAULT) { |
||
361 | $cb = CallbackWrapper::forceWrap($cb); |
||
362 | if (!$this->fd) { |
||
363 | if ($cb) { |
||
364 | $cb($this, false); |
||
365 | } |
||
366 | return false; |
||
367 | } |
||
368 | if (!FileSystem::$supported) { |
||
369 | $r = touch($this->path, $mtime, $atime); |
||
370 | if ($cb) { |
||
371 | $cb($this, $r); |
||
372 | } |
||
373 | return false; |
||
374 | } |
||
375 | return eio_futime($this->fd, $atime, $mtime, $pri, $cb, $this); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Clears cache of stat() and statvfs() |
||
380 | * @return void |
||
381 | */ |
||
382 | public function clearStatCache() { |
||
386 | |||
387 | /** |
||
388 | * Reads data from file |
||
389 | * @param integer $length Length |
||
390 | * @param integer $offset Offset |
||
391 | * @param callable $cb Callback |
||
392 | * @param integer $pri Priority |
||
393 | * @return boolean |
||
394 | */ |
||
395 | public function read($length, $offset = null, $cb = null, $pri = EIO_PRI_DEFAULT) { |
||
396 | $cb = CallbackWrapper::forceWrap($cb); |
||
422 | |||
423 | /** |
||
424 | * sendfile() |
||
425 | * @param mixed $outfd File descriptor |
||
426 | * @param callable $cb Callback |
||
427 | * @param callable $startCb Start callback |
||
428 | * @param integer $offset Offset |
||
429 | * @param integer $length Length |
||
430 | * @param integer $pri Priority |
||
431 | * @return boolean Success |
||
432 | */ |
||
433 | public function sendfile($outfd, $cb, $startCb = null, $offset = 0, $length = null, $pri = EIO_PRI_DEFAULT) { |
||
504 | |||
505 | /** |
||
506 | * readahead() |
||
507 | * @param integer $length Length |
||
508 | * @param integer $offset Offset |
||
509 | * @param callable $cb Callback |
||
510 | * @param integer $pri Priority |
||
511 | * @return resource|false |
||
512 | */ |
||
513 | public function readahead($length, $offset = null, $cb = null, $pri = EIO_PRI_DEFAULT) { |
||
537 | |||
538 | /** |
||
539 | * Generates closure-callback for readAll |
||
540 | * @param callable $cb |
||
541 | * @param integer $size |
||
542 | * @param integer &$offset |
||
543 | * @param integer &$pri |
||
544 | * @param string &$buf |
||
545 | * @return callable |
||
546 | */ |
||
547 | View Code Duplication | protected function readAllGenHandler($cb, $size, &$offset, &$pri, &$buf) { |
|
561 | |||
562 | /** |
||
563 | * Reads whole file |
||
564 | * @param callable $cb Callback |
||
565 | * @param integer $pri Priority |
||
566 | * @return boolean Success |
||
567 | */ |
||
568 | public function readAll($cb, $pri = EIO_PRI_DEFAULT) { |
||
590 | |||
591 | /** |
||
592 | * Generates closure-callback for readAllChunked |
||
593 | * @param callable $cb |
||
594 | * @param callable $chunkcb |
||
595 | * @param integer $size |
||
596 | * @param integer $offset |
||
597 | * @param integer $pri |
||
598 | * @return callable |
||
599 | */ |
||
600 | View Code Duplication | protected function readAllChunkedGenHandler($cb, $chunkcb, $size, &$offset, $pri) { |
|
612 | |||
613 | /** |
||
614 | * Reads file chunk-by-chunk |
||
615 | * @param callable $cb Callback |
||
616 | * @param callable $chunkcb Callback of chunk |
||
617 | * @param integer $pri Priority |
||
618 | * @return resource|false |
||
619 | */ |
||
620 | public function readAllChunked($cb = null, $chunkcb = null, $pri = EIO_PRI_DEFAULT) { |
||
638 | |||
639 | /** |
||
640 | * toString handler |
||
641 | * @return string |
||
642 | */ |
||
643 | public function __toString() { |
||
646 | |||
647 | /** |
||
648 | * Set chunk size |
||
649 | * @param integer $n Chunk size |
||
650 | * @return void |
||
651 | */ |
||
652 | public function setChunkSize($n) { |
||
655 | |||
656 | /** |
||
657 | * Move pointer to arbitrary position |
||
658 | * @param integer $offset Offset |
||
659 | * @param callable $cb Callback |
||
660 | * @param integer $pri Priority |
||
661 | * @return resource|false |
||
662 | */ |
||
663 | public function seek($offset, $cb, $pri = EIO_PRI_DEFAULT) { |
||
671 | |||
672 | /** |
||
673 | * Get current pointer position |
||
674 | * @return integer |
||
675 | */ |
||
676 | public function tell() { |
||
682 | |||
683 | /** |
||
684 | * Close the file |
||
685 | * @return resource|false |
||
686 | */ |
||
687 | public function close() { |
||
707 | |||
708 | /** |
||
709 | * Destructor |
||
710 | */ |
||
711 | public function __destruct() { |
||
714 | } |
||
715 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..