Complex classes like XoopsFileHandler 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 XoopsFileHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class XoopsFileHandler |
||
38 | { |
||
39 | /** |
||
40 | * folder object of the File |
||
41 | * |
||
42 | * @var XoopsFolderHandler |
||
43 | * @access public |
||
44 | */ |
||
45 | public $folder = null; |
||
46 | |||
47 | /** |
||
48 | * Filename |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | public $name = null; |
||
53 | |||
54 | /** |
||
55 | * file info |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | public $info = array(); |
||
60 | |||
61 | /** |
||
62 | * Holds the file handler resource if the file is opened |
||
63 | * |
||
64 | * @var resource |
||
65 | */ |
||
66 | public $handle = null; |
||
67 | |||
68 | /** |
||
69 | * enable locking for file reading and writing |
||
70 | * |
||
71 | * @var boolean |
||
72 | */ |
||
73 | public $lock = null; |
||
74 | |||
75 | /** |
||
76 | * Constructor |
||
77 | * |
||
78 | * @param string $path Path to file |
||
79 | * @param boolean $create Create file if it does not exist (if true) |
||
80 | * @param integer $mode Mode to apply to the folder holding the file |
||
81 | * |
||
82 | * @todo reconsider validity this class, if valid add exceptions to __construct() |
||
83 | */ |
||
84 | 45 | public function __construct($path, $create = false, $mode = 0755) |
|
103 | |||
104 | |||
105 | /** |
||
106 | * Closes the current file if it is opened |
||
107 | */ |
||
108 | 45 | public function __destruct() |
|
112 | |||
113 | /** |
||
114 | * Creates the File. |
||
115 | * |
||
116 | * @return boolean Success |
||
117 | */ |
||
118 | 11 | public function create() |
|
128 | |||
129 | /** |
||
130 | * Opens the current file with a given $mode |
||
131 | * |
||
132 | * @param string $mode A valid 'fopen' mode string (r|w|a ...) |
||
133 | * @param boolean $force If true then the file will be re-opened even if |
||
134 | * its already opened, otherwise it won't |
||
135 | * |
||
136 | * @return boolean True on success, false on failure |
||
137 | */ |
||
138 | 5 | public function open($mode = 'r', $force = false) |
|
139 | { |
||
140 | 5 | if (!$force && is_resource($this->handle)) { |
|
141 | 2 | return true; |
|
142 | } |
||
143 | 5 | if ($this->exists() === false) { |
|
144 | 3 | if ($this->create() === false) { |
|
145 | return false; |
||
146 | } |
||
147 | } |
||
148 | 5 | $this->handle = fopen($this->pwd(), $mode); |
|
149 | 5 | if (is_resource($this->handle)) { |
|
150 | 5 | return true; |
|
151 | } |
||
152 | return false; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Return the contents of this File as a string. |
||
157 | * |
||
158 | * @param string|bool $bytes where to start |
||
159 | * @param string $mode mode of file access |
||
160 | * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't |
||
161 | * |
||
162 | * @return mixed string on success, false on failure |
||
163 | */ |
||
164 | 21 | public function read($bytes = false, $mode = 'rb', $force = false) |
|
192 | |||
193 | /** |
||
194 | * Sets or gets the offset for the currently opened file. |
||
195 | * |
||
196 | * @param mixed $offset The $offset in bytes to seek. If set to false then the current offset is returned. |
||
197 | * @param integer $seek PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to |
||
198 | * |
||
199 | * @return mixed True on success, false on failure (set mode), |
||
200 | * false on failure or integer offset on success (get mode) |
||
201 | */ |
||
202 | 1 | public function offset($offset = false, $seek = SEEK_SET) |
|
215 | |||
216 | /** |
||
217 | * Prepares a ascii string for writing |
||
218 | * fixes line endings |
||
219 | * |
||
220 | * @param string $data Data to prepare for writing. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 1 | public function prepare($data) |
|
232 | |||
233 | /** |
||
234 | * Write given data to this File. |
||
235 | * |
||
236 | * @param string $data Data to write to this File. |
||
237 | * @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}. |
||
238 | * @param bool $force force the file to open |
||
239 | * |
||
240 | * @return bool Success |
||
241 | */ |
||
242 | 3 | public function write($data, $mode = 'w', $force = false) |
|
260 | |||
261 | /** |
||
262 | * Append given data string to this File. |
||
263 | * |
||
264 | * @param string $data Data to write |
||
265 | * @param bool $force force the file to open |
||
266 | * |
||
267 | * @return bool Success |
||
268 | */ |
||
269 | 1 | public function append($data, $force = false) |
|
273 | |||
274 | /** |
||
275 | * Closes the current file if it is opened. |
||
276 | * |
||
277 | * @return boolean True if closing was successful or file was already closed, otherwise false |
||
278 | */ |
||
279 | 45 | public function close() |
|
286 | |||
287 | /** |
||
288 | * Deletes the File. |
||
289 | * |
||
290 | * @return boolean Success |
||
291 | */ |
||
292 | 5 | public function delete() |
|
299 | |||
300 | /** |
||
301 | * Returns the File information array |
||
302 | * |
||
303 | * @return array The File information |
||
304 | */ |
||
305 | 9 | public function info() |
|
315 | |||
316 | /** |
||
317 | * Returns the File extension. |
||
318 | * |
||
319 | * @return string The File extension |
||
320 | */ |
||
321 | 9 | public function ext() |
|
331 | |||
332 | /** |
||
333 | * Returns the File name without extension. |
||
334 | * |
||
335 | * @return string The File name without extension. |
||
336 | */ |
||
337 | 1 | public function name() |
|
349 | |||
350 | /** |
||
351 | * makes filename safe for saving |
||
352 | * |
||
353 | * @param string $name the name of the file to make safe if different from $this->name |
||
354 | * @param string $ext the extension of the file |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | 9 | public function safe($name = null, $ext = null) |
|
368 | |||
369 | /** |
||
370 | * Get md5 Checksum of file with previous check of Filesize |
||
371 | * |
||
372 | * @param integer $maxsize in MB or true to force |
||
373 | * |
||
374 | * @return string md5 Checksum {@link http://php.net/md5_file See md5_file()} |
||
375 | */ |
||
376 | 1 | public function md5($maxsize = 5) |
|
388 | |||
389 | /** |
||
390 | * Returns the full path of the File. |
||
391 | * |
||
392 | * @return string Full path to file |
||
393 | */ |
||
394 | 45 | public function pwd() |
|
398 | |||
399 | /** |
||
400 | * Returns true if the File exists. |
||
401 | * |
||
402 | * @return bool true if it exists, false otherwise |
||
403 | */ |
||
404 | 45 | public function exists() |
|
409 | |||
410 | /** |
||
411 | * Returns the "chmod" (permissions) of the File. |
||
412 | * |
||
413 | * @return string Permissions for the file |
||
414 | */ |
||
415 | 1 | public function perms() |
|
422 | |||
423 | /** |
||
424 | * Returns the Filesize, either in bytes or in human-readable format. |
||
425 | * |
||
426 | * @return bool|int filesize as int or as a human-readable string |
||
427 | */ |
||
428 | 2 | public function size() |
|
436 | |||
437 | /** |
||
438 | * Returns true if the File is writable. |
||
439 | * |
||
440 | * @return boolean true if its writable, false otherwise |
||
441 | */ |
||
442 | 1 | public function writable() |
|
446 | |||
447 | /** |
||
448 | * Returns true if the File is executable. |
||
449 | * |
||
450 | * @return boolean true if its executable, false otherwise |
||
451 | */ |
||
452 | 1 | public function executable() |
|
456 | |||
457 | /** |
||
458 | * Returns true if the File is readable. |
||
459 | * |
||
460 | * @return boolean true if file is readable, false otherwise |
||
461 | */ |
||
462 | 1 | public function readable() |
|
466 | |||
467 | /** |
||
468 | * Returns the File's owner. |
||
469 | * |
||
470 | * @return int|bool the Fileowner |
||
471 | */ |
||
472 | 1 | public function owner() |
|
479 | |||
480 | /** |
||
481 | * Returns the File group. |
||
482 | * |
||
483 | * @return int|bool the Filegroup |
||
484 | */ |
||
485 | 1 | public function group() |
|
492 | |||
493 | /** |
||
494 | * Returns last access time. |
||
495 | * |
||
496 | * @return int|bool timestamp Timestamp of last access time |
||
497 | */ |
||
498 | 1 | public function lastAccess() |
|
505 | |||
506 | /** |
||
507 | * Returns last modified time. |
||
508 | * |
||
509 | * @return int|bool timestamp Timestamp of last modification |
||
510 | */ |
||
511 | 1 | public function lastChange() |
|
518 | |||
519 | /** |
||
520 | * Returns the current folder. |
||
521 | * |
||
522 | * @return XoopsFolderHandler Current folder |
||
523 | */ |
||
524 | 1 | public function folder() |
|
528 | } |
||
529 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.