Conditions | 10 |
Paths | 20 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
71 | public function recursiveRemoveDirectory($directory, $empty = false) |
||
72 | { |
||
73 | // if the path has a slash at the end we remove it here |
||
74 | if (substr($directory, -1) === '/') { |
||
75 | $directory = substr($directory, 0, -1); |
||
76 | } |
||
77 | |||
78 | // if the path is not valid or is not a directory ... |
||
79 | // ... if the path is not readable |
||
80 | if (!is_dir($directory) || !is_readable($directory)) { |
||
81 | return false; |
||
82 | } |
||
83 | |||
84 | // we open the directory |
||
85 | $handle = opendir($directory); |
||
86 | |||
87 | if (!$handle) { |
||
88 | throw new RuntimeException(sprintf('Directory <%s> error', $directory)); |
||
89 | } |
||
90 | |||
91 | $skip = array(".", ".."); |
||
92 | |||
93 | // and scan through the items inside |
||
94 | while (false !== ($file = readdir($handle))) { |
||
95 | // if the filepointer is not the current directory |
||
96 | // or the parent directory |
||
97 | if (in_array($file, $skip)) { |
||
98 | continue; |
||
99 | } |
||
100 | |||
101 | // we build the new path to delete |
||
102 | $path = $directory . '/' . $file; |
||
103 | |||
104 | // if the new path is a directory |
||
105 | // don't recursively delete symlinks - just remove the actual link |
||
106 | // this is helpful for extensions sym-linked from vendor directory |
||
107 | // previous behaviour would wipe out the files in the vendor directory |
||
108 | if (!is_link($path) && is_dir($path)) { |
||
109 | // we call this function with the new path |
||
110 | $this->recursiveRemoveDirectory($path); |
||
111 | |||
112 | // if the new path is a file |
||
113 | } else { |
||
114 | // we remove the file |
||
115 | unlink($path); |
||
116 | } |
||
117 | } |
||
118 | closedir($handle); |
||
119 | |||
120 | // if the option not empty |
||
121 | if (!$empty) { |
||
122 | return rmdir($directory); |
||
123 | } |
||
124 | |||
125 | // return success |
||
126 | return true; |
||
127 | } |
||
128 | |||
145 |
If you suppress an error, we recommend checking for the error condition explicitly: