Total Complexity | 135 |
Total Lines | 830 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like XoopsFolderHandler 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.
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 XoopsFolderHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class XoopsFolderHandler |
||
53 | { |
||
54 | /** |
||
55 | * Path to Folder. |
||
56 | * |
||
57 | * @var string |
||
58 | * @access public |
||
59 | */ |
||
60 | public $path; |
||
61 | |||
62 | /** |
||
63 | * Sortedness. |
||
64 | * |
||
65 | * @var boolean |
||
66 | * @access public |
||
67 | */ |
||
68 | public $sort = false; |
||
69 | |||
70 | /** |
||
71 | * mode to be used on create. |
||
72 | * |
||
73 | * @var boolean |
||
74 | * @access public |
||
75 | */ |
||
76 | public $mode = '0755'; |
||
77 | |||
78 | /** |
||
79 | * holds messages from last method. |
||
80 | * |
||
81 | * @var array |
||
82 | * @access private |
||
83 | */ |
||
84 | public $messages = []; |
||
85 | |||
86 | /** |
||
87 | * holds errors from last method. |
||
88 | * |
||
89 | * @var array |
||
90 | * @access private |
||
91 | */ |
||
92 | public $errors = []; |
||
93 | |||
94 | /** |
||
95 | * holds array of complete directory paths. |
||
96 | * |
||
97 | * @var array |
||
98 | * @access private |
||
99 | */ |
||
100 | public $directories; |
||
101 | |||
102 | /** |
||
103 | * holds array of complete file paths. |
||
104 | * |
||
105 | * @var array |
||
106 | * @access private |
||
107 | */ |
||
108 | public $files; |
||
109 | |||
110 | /** |
||
111 | * Constructor. |
||
112 | * |
||
113 | * @param bool|string $path Path to folder |
||
114 | * @param boolean $create Create folder if not found |
||
115 | * @param mixed $mode Mode (CHMOD) to apply to created folder, false to ignore |
||
116 | */ |
||
117 | public function __construct($path = false, $create = true, $mode = false) |
||
118 | { |
||
119 | if (empty($path)) { |
||
120 | $path = XOOPS_VAR_PATH . '/caches/xoops_cache'; |
||
121 | } |
||
122 | if ($mode) { |
||
123 | $this->mode = intval($mode, 8); |
||
|
|||
124 | } |
||
125 | if (!file_exists($path) && $create == true) { |
||
126 | $this->create($path, $this->mode); |
||
127 | } |
||
128 | if (!$this->isAbsolute($path)) { |
||
129 | $path = realpath($path); |
||
130 | } |
||
131 | $this->cd($path); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Return current path. |
||
136 | * |
||
137 | * @return string Current path |
||
138 | * @access public |
||
139 | */ |
||
140 | public function pwd() |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Change directory to $desired_path. |
||
147 | * |
||
148 | * @param string $path Path to the directory to change to |
||
149 | * |
||
150 | * @return string|false The new path. Returns false on failure |
||
151 | * @access public |
||
152 | */ |
||
153 | public function cd($path) |
||
154 | { |
||
155 | $path = $this->realpath($path); |
||
156 | if (is_string($path) && is_dir($path) && file_exists($path)) { |
||
157 | return $this->path = $path; |
||
158 | } |
||
159 | |||
160 | return false; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Returns an array of the contents of the current directory, or false on failure. |
||
165 | * The returned array holds two arrays: one of dirs and one of files. |
||
166 | * |
||
167 | * @param boolean $sort |
||
168 | * @param mixed $exceptions either an array or boolean true will no grab dot files |
||
169 | * |
||
170 | * @return mixed Contents of current directory as an array, false on failure |
||
171 | * @access public |
||
172 | */ |
||
173 | public function read($sort = true, $exceptions = false) |
||
207 | ]; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Returns an array of all matching files in current directory. |
||
212 | * |
||
213 | * @param string $regexp_pattern Preg_match pattern (Defaults to: .*) |
||
214 | * @param bool $sort |
||
215 | * |
||
216 | * @return array Files that match given pattern |
||
217 | * @access public |
||
218 | */ |
||
219 | public function find($regexp_pattern = '.*', $sort = false) |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Returns an array of all matching files in and below current directory. |
||
238 | * |
||
239 | * @param string $pattern Preg_match pattern (Defaults to: .*) |
||
240 | * @param bool $sort |
||
241 | * |
||
242 | * @return array Files matching $pattern |
||
243 | * @access public |
||
244 | */ |
||
245 | public function findRecursive($pattern = '.*', $sort = false) |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Private helper function for findRecursive. |
||
256 | * |
||
257 | * @param string $pattern Pattern to match against |
||
258 | * @param bool $sort |
||
259 | * |
||
260 | * @return array Files matching pattern |
||
261 | * @access private |
||
262 | */ |
||
263 | public function _findRecursive($pattern, $sort = false) |
||
264 | { |
||
265 | [$dirs, $files] = $this->read($sort); |
||
266 | $found = []; |
||
267 | foreach ($files as $file) { |
||
268 | if (preg_match("/^{$pattern}$/i", $file)) { |
||
269 | $found[] = $this->addPathElement($this->path, $file); |
||
270 | } |
||
271 | } |
||
272 | $start = $this->path; |
||
273 | foreach ($dirs as $dir) { |
||
274 | $this->cd($this->addPathElement($start, $dir)); |
||
275 | $newFound = $this->findRecursive($pattern); |
||
276 | |||
277 | foreach ($newFound as $item) { |
||
278 | $found[] = $item; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | return $found; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Returns true if given $path is a Windows path. |
||
287 | * |
||
288 | * @param string $path Path to check |
||
289 | * |
||
290 | * @return boolean true if windows path, false otherwise |
||
291 | * @access public |
||
292 | * @static |
||
293 | */ |
||
294 | public function isWindowsPath($path) |
||
295 | { |
||
296 | if (preg_match('/^[A-Z]:\\\\/i', (string) $path)) { |
||
297 | return true; |
||
298 | } |
||
299 | |||
300 | return false; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Returns true if given $path is an absolute path. |
||
305 | * |
||
306 | * @param string $path Path to check |
||
307 | * |
||
308 | * @return bool |
||
309 | * @access public |
||
310 | * @static |
||
311 | */ |
||
312 | public function isAbsolute($path) |
||
313 | { |
||
314 | $match = preg_match('/^\\//', $path) || preg_match('/^[A-Z]:\\//i', $path); |
||
315 | |||
316 | return $match; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.) |
||
321 | * |
||
322 | * @param string $path Path to check |
||
323 | * |
||
324 | * @return string Set of slashes ("\\" or "/") |
||
325 | * @access public |
||
326 | * @static |
||
327 | */ |
||
328 | public function normalizePath($path) |
||
329 | { |
||
330 | if ($this->isWindowsPath($path)) { |
||
331 | return '\\'; |
||
332 | } |
||
333 | |||
334 | return '/'; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.) |
||
339 | * |
||
340 | * @param string $path Path to check |
||
341 | * |
||
342 | * @return string Set of slashes ("\\" or "/") |
||
343 | * @access public |
||
344 | * @static |
||
345 | */ |
||
346 | public function correctSlashFor($path) |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Returns $path with added terminating slash (corrected for Windows or other OS). |
||
357 | * |
||
358 | * @param string $path Path to check |
||
359 | * |
||
360 | * @return string Path with ending slash |
||
361 | * @access public |
||
362 | * @static |
||
363 | */ |
||
364 | public function slashTerm($path) |
||
365 | { |
||
366 | if ($this->isSlashTerm($path)) { |
||
367 | return $path; |
||
368 | } |
||
369 | |||
370 | return $path . $this->correctSlashFor($path); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Returns $path with $element added, with correct slash in-between. |
||
375 | * |
||
376 | * @param string $path Path |
||
377 | * @param string $element Element to and at end of path |
||
378 | * |
||
379 | * @return string Combined path |
||
380 | * @access public |
||
381 | * @static |
||
382 | */ |
||
383 | public function addPathElement($path, $element) |
||
384 | { |
||
385 | return $this->slashTerm($path) . $element; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Returns true if the File is in a given XoopsPath. |
||
390 | * |
||
391 | * @param string $path |
||
392 | * |
||
393 | * @return bool |
||
394 | * @access public |
||
395 | */ |
||
396 | public function inXoopsPath($path = '') |
||
397 | { |
||
398 | $dir = substr($this->slashTerm(XOOPS_ROOT_PATH), 0, -1); |
||
399 | $newdir = $dir . $path; |
||
400 | |||
401 | return $this->inPath($newdir); |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Returns true if the File is in given path. |
||
406 | * |
||
407 | * @param string $path |
||
408 | * @param bool $reverse |
||
409 | * |
||
410 | * @return bool |
||
411 | * @access public |
||
412 | */ |
||
413 | public function inPath($path = '', $reverse = false) |
||
426 | } |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Change the mode on a directory structure recursively. |
||
431 | * |
||
432 | * @param string $path The path to chmod |
||
433 | * @param bool|int $mode octal value 0755 |
||
434 | * @param boolean $recursive chmod recursively |
||
435 | * @param array $exceptions array of files, directories to skip |
||
436 | * |
||
437 | * @return boolean Returns TRUE on success, FALSE on failure |
||
438 | * @access public |
||
439 | */ |
||
440 | public function chmod($path, $mode = false, $recursive = true, $exceptions = []) |
||
441 | { |
||
442 | if (!$mode) { |
||
443 | $mode = $this->mode; |
||
444 | } |
||
445 | if ($recursive === false && is_dir($path)) { |
||
446 | if (chmod($path, intval($mode, 8))) { |
||
447 | $this->messages[] = sprintf('%s changed to %s', $path, $mode); |
||
448 | |||
449 | return true; |
||
450 | } else { |
||
451 | $this->errors[] = sprintf('%s NOT changed to %s', $path, $mode); |
||
452 | |||
453 | return false; |
||
454 | } |
||
455 | } |
||
456 | if (is_dir($path)) { |
||
457 | [$paths] = $this->tree($path); |
||
458 | foreach ($paths as $key => $fullpath) { |
||
459 | $check = explode('/', $fullpath); |
||
460 | $count = count($check); |
||
461 | |||
462 | if (in_array($check[$count - 1], $exceptions)) { |
||
463 | continue; |
||
464 | } |
||
465 | |||
466 | if (chmod($fullpath, intval($mode, 8))) { |
||
467 | $this->messages[] = sprintf('%s changed to %s', $fullpath, $mode); |
||
468 | } else { |
||
469 | $this->errors[] = sprintf('%s NOT changed to %s', $fullpath, $mode); |
||
470 | } |
||
471 | } |
||
472 | if (empty($this->errors)) { |
||
473 | return true; |
||
474 | } |
||
475 | } |
||
476 | |||
477 | return false; |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Returns an array of nested directories and files in each directory |
||
482 | * |
||
483 | * @param string $path the directory path to build the tree from |
||
484 | * @param boolean $hidden return hidden files and directories |
||
485 | * @param string $type either file or dir. null returns both files and directories |
||
486 | * |
||
487 | * @return mixed array of nested directories and files in each directory |
||
488 | * @access public |
||
489 | */ |
||
490 | public function tree($path, $hidden = true, $type = null) |
||
491 | { |
||
492 | $path = rtrim($path, '/'); |
||
493 | $this->files = []; |
||
494 | $this->directories = [ |
||
495 | $path, |
||
496 | ]; |
||
497 | $directories = []; |
||
498 | while (count($this->directories)) { |
||
499 | $dir = array_pop($this->directories); |
||
500 | $this->_tree($dir, $hidden); |
||
501 | $directories[] = $dir; |
||
502 | } |
||
503 | if ($type === null) { |
||
504 | return [ |
||
505 | $directories, |
||
506 | $this->files, |
||
507 | ]; |
||
508 | } |
||
509 | if ($type === 'dir') { |
||
510 | return $directories; |
||
511 | } |
||
512 | |||
513 | return $this->files; |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * Private method to list directories and files in each directory |
||
518 | * |
||
519 | * @param string $path |
||
520 | * @param $hidden |
||
521 | * |
||
522 | * @internal param $ $ = boolean $hidden |
||
523 | * @access private |
||
524 | */ |
||
525 | public function _tree($path, $hidden) |
||
526 | { |
||
527 | if (is_dir($path)) { |
||
528 | $dirHandle = opendir($path); |
||
529 | while (false !== ($item = readdir($dirHandle))) { |
||
530 | $found = false; |
||
531 | if (($hidden === true && $item !== '.' && $item !== '..') || ($hidden === false && !preg_match('/^\\.(.*)$/', $item))) { |
||
532 | $found = $path . '/' . $item; |
||
533 | } |
||
534 | if ($found !== false) { |
||
535 | if (is_dir($found)) { |
||
536 | $this->directories[] = $found; |
||
537 | } else { |
||
538 | $this->files[] = $found; |
||
539 | } |
||
540 | } |
||
541 | } |
||
542 | closedir($dirHandle); |
||
543 | } |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Create a directory structure recursively. |
||
548 | * |
||
549 | * @param string $pathname The directory structure to create |
||
550 | * @param bool|int $mode octal value 0755 |
||
551 | * |
||
552 | * @return boolean Returns TRUE on success, FALSE on failure |
||
553 | * @access public |
||
554 | */ |
||
555 | public function create($pathname, $mode = false) |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * Returns the size in bytes of this Folder. |
||
588 | * |
||
589 | * @return int $size |
||
590 | * @access public |
||
591 | */ |
||
592 | public function dirsize() |
||
593 | { |
||
594 | $size = 0; |
||
595 | $directory = $this->slashTerm($this->path); |
||
596 | $stack = [$directory]; |
||
597 | $count = count($stack); |
||
598 | for ($i = 0, $j = $count; $i < $j; ++$i) { |
||
599 | if (is_file($stack[$i])) { |
||
600 | $size += filesize($stack[$i]); |
||
601 | } else { |
||
602 | if (is_dir($stack[$i])) { |
||
603 | $dir = dir($stack[$i]); |
||
604 | if ($dir) { |
||
605 | while (false !== ($entry = $dir->read())) { |
||
606 | if ($entry === '.' || $entry === '..') { |
||
607 | continue; |
||
608 | } |
||
609 | $add = $stack[$i] . $entry; |
||
610 | if (is_dir($stack[$i] . $entry)) { |
||
611 | $add = $this->slashTerm($add); |
||
612 | } |
||
613 | $stack[] = $add; |
||
614 | } |
||
615 | $dir->close(); |
||
616 | } |
||
617 | } |
||
618 | } |
||
619 | $j = count($stack); |
||
620 | } |
||
621 | |||
622 | return $size; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Recursively Remove directories if system allow. |
||
627 | * |
||
628 | * @param string $path Path of directory to delete |
||
629 | * |
||
630 | * @return boolean Success |
||
631 | * @access public |
||
632 | */ |
||
633 | public function delete($path) |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Copies files and directories from one directory to another |
||
676 | * |
||
677 | * @param array|string $options An array of options or a string representing the target directory |
||
678 | * If a string is provided, it will be used as the target directory and other options will be set to their default values |
||
679 | * @return bool Returns true on success, false on failure |
||
680 | */ |
||
681 | public function copy($options = []) |
||
767 | } |
||
768 | |||
769 | /** |
||
770 | * Recursive directory move. |
||
771 | * |
||
772 | * @param array|string $options (to, from, chmod, skip) |
||
773 | * |
||
774 | * @return string|boolean Success |
||
775 | * @access public |
||
776 | */ |
||
777 | public function move($options) |
||
800 | } |
||
801 | |||
802 | /** |
||
803 | * get messages from latest method |
||
804 | * |
||
805 | * @return array |
||
806 | * @access public |
||
807 | */ |
||
808 | public function messages() |
||
811 | } |
||
812 | |||
813 | /** |
||
814 | * get error from latest method |
||
815 | * |
||
816 | * @return array |
||
817 | * @access public |
||
818 | */ |
||
819 | public function errors() |
||
820 | { |
||
821 | return $this->errors; |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * Get the real path (taking ".." and such into account) |
||
826 | * |
||
827 | * @param string $path Path to resolve |
||
828 | * |
||
829 | * @return string|false The resolved path |
||
830 | */ |
||
831 | public function realpath($path) |
||
832 | { |
||
833 | $path = trim($path); |
||
834 | if (strpos($path, '..') === false) { |
||
835 | if (!$this->isAbsolute($path)) { |
||
836 | $path = $this->addPathElement($this->path, $path); |
||
837 | } |
||
838 | |||
839 | return $path; |
||
840 | } |
||
841 | $parts = explode('/', $path); |
||
842 | $newparts = []; |
||
843 | $newpath = $path[0] === '/' ? '/' : ''; |
||
844 | while (($part = array_shift($parts)) !== null) { |
||
845 | if ($part === '.' || $part == '') { |
||
846 | continue; |
||
847 | } |
||
848 | if ($part === '..') { |
||
849 | if (count($newparts) > 0) { |
||
850 | array_pop($newparts); |
||
851 | continue; |
||
852 | } else { |
||
853 | return false; |
||
854 | } |
||
855 | } |
||
856 | $newparts[] = $part; |
||
857 | } |
||
858 | $newpath .= implode('/', $newparts); |
||
859 | if ((strlen($path) > 1) && $path[strlen($path) - 1] === '/') { |
||
860 | $newpath .= '/'; |
||
861 | } |
||
862 | |||
863 | return $newpath; |
||
864 | } |
||
865 | |||
866 | /** |
||
867 | * Returns true if given $path ends in a slash (i.e. is slash-terminated). |
||
868 | * |
||
869 | * @param string $path Path to check |
||
870 | * |
||
871 | * @return boolean true if path ends with slash, false otherwise |
||
872 | * @access public |
||
873 | * @static |
||
874 | */ |
||
875 | public function isSlashTerm($path) |
||
882 | } |
||
883 | } |
||
884 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.