Total Complexity | 68 |
Total Lines | 302 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like DeleteTask 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 DeleteTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class DeleteTask extends Task |
||
34 | { |
||
35 | use ResourceAware; |
||
36 | |||
37 | protected $file; |
||
38 | protected $dir; |
||
39 | protected $includeEmpty = false; |
||
40 | |||
41 | protected $quiet = false; |
||
42 | protected $failonerror = false; |
||
43 | protected $verbosity = Project::MSG_VERBOSE; |
||
44 | |||
45 | /** |
||
46 | * Set the name of a single file to be removed. |
||
47 | */ |
||
48 | public function setFile(File $file) |
||
49 | { |
||
50 | $this->file = $file; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Set the directory from which files are to be deleted. |
||
55 | */ |
||
56 | public function setDir(File $dir) |
||
57 | { |
||
58 | $this->dir = $dir; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Used to force listing of all names of deleted files. |
||
63 | * |
||
64 | * @param bool $verbosity |
||
65 | */ |
||
66 | public function setVerbose($verbosity) |
||
67 | { |
||
68 | if ($verbosity) { |
||
69 | $this->verbosity = Project::MSG_INFO; |
||
70 | } else { |
||
71 | $this->verbosity = Project::MSG_VERBOSE; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * If the file does not exist, do not display a diagnostic |
||
77 | * message or modify the exit status to reflect an error. |
||
78 | * This means that if a file or directory cannot be deleted, |
||
79 | * then no error is reported. This setting emulates the |
||
80 | * -f option to the Unix rm command. Default is false |
||
81 | * meaning things are verbose. |
||
82 | * |
||
83 | * @param bool $bool |
||
84 | */ |
||
85 | public function setQuiet($bool) |
||
86 | { |
||
87 | $this->quiet = $bool; |
||
88 | if ($this->quiet) { |
||
89 | $this->failonerror = false; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * this flag means 'note errors to the output, but keep going'. |
||
95 | * |
||
96 | * @param bool $bool |
||
97 | * @retujrn void |
||
98 | */ |
||
99 | public function setFailOnError($bool) |
||
100 | { |
||
101 | $this->failonerror = $bool; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Used to delete empty directories. |
||
106 | * |
||
107 | * @param bool $includeEmpty |
||
108 | */ |
||
109 | public function setIncludeEmptyDirs($includeEmpty) |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Delete the file(s). |
||
116 | * |
||
117 | * @throws BuildException |
||
118 | */ |
||
119 | public function main() |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Recursively removes a directory. |
||
237 | * |
||
238 | * @param File $d the directory to remove |
||
239 | * |
||
240 | * @throws BuildException |
||
241 | */ |
||
242 | private function removeDir($d) |
||
243 | { |
||
244 | $list = $d->listDir(); |
||
245 | if (null === $list) { |
||
246 | $list = []; |
||
247 | } |
||
248 | |||
249 | foreach ($list as $s) { |
||
250 | $f = new File($d, $s); |
||
251 | if ($f->isDirectory()) { |
||
252 | $this->removeDir($f); |
||
253 | } else { |
||
254 | $this->log('Deleting ' . $f->__toString(), $this->verbosity); |
||
255 | |||
256 | try { |
||
257 | $f->delete(); |
||
258 | } catch (Exception $e) { |
||
259 | $message = 'Unable to delete file ' . $f->__toString() . ': ' . $e->getMessage(); |
||
260 | if ($this->failonerror) { |
||
261 | throw new BuildException($message); |
||
262 | } |
||
263 | |||
264 | $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN); |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | |||
269 | try { |
||
270 | $d->delete(); |
||
271 | } catch (Exception $e) { |
||
272 | $message = 'Unable to delete directory ' . $d->__toString() . ': ' . $e->getMessage(); |
||
273 | if ($this->failonerror) { |
||
274 | throw new BuildException($message); |
||
275 | } |
||
276 | |||
277 | $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * remove an array of files in a directory, and a list of subdirectories |
||
283 | * which will only be deleted if 'includeEmpty' is true. |
||
284 | * |
||
285 | * @param File $d directory to work from |
||
286 | * @param array $files array of files to delete; can be of zero length |
||
287 | * @param array $dirs array of directories to delete; can of zero length |
||
288 | * |
||
289 | * @throws BuildException |
||
290 | */ |
||
291 | private function removeFiles(File $d, &$files, &$dirs) |
||
335 | } |
||
336 | } |
||
339 |