Conditions | 15 |
Paths | 41 |
Total Lines | 60 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 4 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
28 | public static function findFiles($dir, $options = [], $type = GetAction::TYPE_IMAGES) |
||
29 | { |
||
30 | if (!is_dir($dir)) { |
||
31 | throw new InvalidParamException('The dir argument must be a directory.'); |
||
32 | } |
||
33 | $dir = rtrim($dir, DIRECTORY_SEPARATOR); |
||
34 | if (isset($options['url'])) { |
||
35 | $options['url'] = rtrim($options['url'], '/'); |
||
36 | } |
||
37 | if (!isset($options['basePath'])) { |
||
38 | $options['basePath'] = realpath($dir); |
||
39 | // this should also be done only once |
||
40 | $options = self::normalizeOptions($options); |
||
41 | } |
||
42 | $list = []; |
||
43 | $handle = opendir($dir); |
||
44 | if ($handle === false) { |
||
45 | // @codeCoverageIgnoreStart |
||
46 | throw new InvalidParamException('Unable to open directory: ' . $dir); |
||
47 | // @codeCoverageIgnoreEnd |
||
48 | } |
||
49 | while (($file = readdir($handle)) !== false) { |
||
50 | if ($file === '.' || $file === '..') { |
||
51 | continue; |
||
52 | } |
||
53 | $path = $dir . DIRECTORY_SEPARATOR . $file; |
||
54 | if (static::filterPath($path, $options)) { |
||
55 | if (is_file($path)) { |
||
56 | if (isset($options['url'])) { |
||
57 | $url = str_replace([$options['basePath'], '\\'], [$options['url'], '/'], static::normalizePath($path)); |
||
58 | |||
59 | if ($type === GetAction::TYPE_IMAGES) { |
||
60 | $list[] = [ |
||
61 | 'title' => $file, |
||
62 | 'thumb' => $url, |
||
63 | 'image' => $url |
||
64 | ]; |
||
65 | } elseif ($type === GetAction::TYPE_FILES) { |
||
66 | $size = self::getFileSize($path); |
||
67 | $list[] = [ |
||
68 | 'title' => $file, |
||
69 | 'name' => $file, |
||
70 | 'link' => $url, |
||
71 | 'size' => $size |
||
72 | ]; |
||
73 | } else { |
||
74 | $list[] = $path; |
||
75 | } |
||
76 | } else { |
||
77 | $list[] = $path; |
||
78 | } |
||
79 | } elseif (!isset($options['recursive']) || $options['recursive']) { |
||
80 | $list = array_merge($list, static::findFiles($path, $options, $type)); |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | closedir($handle); |
||
85 | |||
86 | return $list; |
||
87 | } |
||
88 | |||
188 |
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.