Total Complexity | 81 |
Total Lines | 403 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AppendTask 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 AppendTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
69 | class AppendTask extends Task |
||
70 | { |
||
71 | use FileListAware; |
||
72 | use FileSetAware; |
||
73 | use FilterChainAware; |
||
74 | |||
75 | /** |
||
76 | * Append stuff to this file. |
||
77 | */ |
||
78 | private $to; |
||
79 | |||
80 | /** |
||
81 | * Explicit file to append. |
||
82 | */ |
||
83 | private $file; |
||
84 | |||
85 | /** |
||
86 | * Text to append. (cannot be used in conjunction w/ files or filesets). |
||
87 | */ |
||
88 | private $text; |
||
89 | |||
90 | private $filtering = true; |
||
91 | |||
92 | /** |
||
93 | * @var TextElement |
||
94 | */ |
||
95 | private $header; |
||
96 | |||
97 | /** |
||
98 | * @var TextElement |
||
99 | */ |
||
100 | private $footer; |
||
101 | |||
102 | private $append = true; |
||
103 | |||
104 | private $fixLastLine = false; |
||
105 | |||
106 | private $overwrite = true; |
||
107 | |||
108 | private $eolString; |
||
109 | |||
110 | private $skipSanitize = false; |
||
111 | |||
112 | public function setFiltering(bool $filtering): void |
||
113 | { |
||
114 | $this->filtering = $filtering; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param bool $overwrite |
||
119 | */ |
||
120 | public function setOverwrite($overwrite): void |
||
121 | { |
||
122 | $this->overwrite = $overwrite; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * The more conventional naming for method to set destination file. |
||
127 | */ |
||
128 | public function setDestFile(File $f): void |
||
129 | { |
||
130 | $this->to = $f; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Sets the behavior when the destination exists. If set to |
||
135 | * <code>true</code> the task will append the stream data an |
||
136 | * {@link Appendable} resource; otherwise existing content will be |
||
137 | * overwritten. Defaults to <code>false</code>. |
||
138 | * |
||
139 | * @param bool $append if true append output |
||
140 | */ |
||
141 | public function setAppend(bool $append): void |
||
142 | { |
||
143 | $this->append = $append; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Specify the end of line to find and to add if |
||
148 | * not present at end of each input file. This attribute |
||
149 | * is used in conjunction with fixlastline. |
||
150 | * |
||
151 | * @param string $crlf the type of new line to add - |
||
152 | * cr, mac, lf, unix, crlf, or dos |
||
153 | */ |
||
154 | public function setEol($crlf): void |
||
155 | { |
||
156 | $s = $crlf; |
||
157 | if ('cr' === $s || 'mac' === $s) { |
||
158 | $this->eolString = "\r"; |
||
159 | } elseif ('lf' === $s || 'unix' === $s) { |
||
160 | $this->eolString = "\n"; |
||
161 | } elseif ('crlf' === $s || 'dos' === $s) { |
||
162 | $this->eolString = "\r\n"; |
||
163 | } else { |
||
164 | $this->eolString = $this->getProject()->getProperty('line.separator'); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Sets specific file to append. |
||
170 | */ |
||
171 | public function setFile(File $f): void |
||
172 | { |
||
173 | $this->file = $f; |
||
174 | } |
||
175 | |||
176 | public function createPath(): Path |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Sets text to append. (cannot be used in conjunction w/ files or filesets). |
||
186 | */ |
||
187 | public function setText(string $txt): void |
||
188 | { |
||
189 | $this->text = $txt; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Sets text to append. Supports CDATA. |
||
194 | */ |
||
195 | public function addText(string $txt): void |
||
196 | { |
||
197 | $this->text .= $txt; |
||
198 | } |
||
199 | |||
200 | public function addHeader(TextElement $headerToAdd): void |
||
201 | { |
||
202 | $this->header = $headerToAdd; |
||
203 | } |
||
204 | |||
205 | public function addFooter(TextElement $footerToAdd): void |
||
206 | { |
||
207 | $this->footer = $footerToAdd; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Append line.separator to files that do not end |
||
212 | * with a line.separator, default false. |
||
213 | * |
||
214 | * @param bool $fixLastLine if true make sure each input file has |
||
215 | * new line on the concatenated stream |
||
216 | */ |
||
217 | public function setFixLastLine(bool $fixLastLine): void |
||
218 | { |
||
219 | $this->fixLastLine = $fixLastLine; |
||
220 | } |
||
221 | |||
222 | public function setSkipSanitize(bool $skipSanitize): void |
||
223 | { |
||
224 | $this->skipSanitize = $skipSanitize; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Append the file(s). |
||
229 | * |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function main() |
||
233 | { |
||
234 | $this->validate(); |
||
235 | |||
236 | try { |
||
237 | if (null !== $this->to) { |
||
238 | // create a file writer to append to "to" file. |
||
239 | $writer = new FileWriter($this->to, $this->append); |
||
240 | } else { |
||
241 | $writer = new LogWriter($this); |
||
242 | } |
||
243 | |||
244 | if (null !== $this->text) { |
||
245 | // simply append the text |
||
246 | if ($this->to instanceof File) { |
||
247 | $this->log('Appending string to ' . $this->to->getPath()); |
||
248 | } |
||
249 | |||
250 | $text = $this->text; |
||
251 | if ($this->filtering) { |
||
252 | $fr = $this->getFilteredReader(new StringReader($text)); |
||
253 | $text = $fr->read(); |
||
254 | } |
||
255 | |||
256 | $text = $this->appendHeader($text); |
||
257 | $text = $this->appendFooter($text); |
||
258 | $writer->write($text); |
||
259 | } else { |
||
260 | // append explicitly-specified file |
||
261 | if (null !== $this->file) { |
||
262 | try { |
||
263 | $this->appendFile($writer, $this->file); |
||
264 | } catch (Exception $ioe) { |
||
265 | $this->log( |
||
266 | 'Unable to append contents of file ' . $this->file->getAbsolutePath() . ': ' . $ioe->getMessage(), |
||
267 | Project::MSG_WARN |
||
268 | ); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | // append any files in filesets |
||
273 | foreach ($this->filesets as $fs) { |
||
274 | try { |
||
275 | if ($fs instanceof Path) { |
||
276 | $files = $fs->listPaths(); |
||
277 | $this->appendFiles($writer, $files); |
||
278 | } elseif ($fs instanceof FileSet) { |
||
279 | $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles(); |
||
280 | $this->appendFiles($writer, $files, $fs->getDir($this->project)); |
||
281 | } |
||
282 | } catch (BuildException $be) { |
||
283 | if (false === strpos($be->getMessage(), 'is the same as the output file')) { |
||
284 | $this->log($be->getMessage(), Project::MSG_WARN); |
||
285 | } else { |
||
286 | throw new BuildException($be->getMessage()); |
||
287 | } |
||
288 | } catch (IOException $ioe) { |
||
289 | throw new BuildException($ioe); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | foreach ($this->filelists as $list) { |
||
294 | $dir = $list->getDir($this->project); |
||
295 | $files = $list->getFiles($this->project); |
||
296 | foreach ($files as $file) { |
||
297 | $this->appendFile($writer, new File($dir, $file)); |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | } catch (Exception $e) { |
||
302 | throw new BuildException($e); |
||
303 | } |
||
304 | |||
305 | $writer->close(); |
||
306 | } |
||
307 | |||
308 | private function appendHeader($string): string |
||
309 | { |
||
310 | $result = $string; |
||
311 | if (null !== $this->header) { |
||
312 | $header = $this->header->getValue(); |
||
313 | if ($this->header->filtering) { |
||
314 | $fr = $this->getFilteredReader(new StringReader($header)); |
||
315 | $header = $fr->read(); |
||
316 | } |
||
317 | |||
318 | $result = $header . $string; |
||
319 | } |
||
320 | |||
321 | return $result; |
||
322 | } |
||
323 | |||
324 | private function appendFooter($string): string |
||
325 | { |
||
326 | $result = $string; |
||
327 | if (null !== $this->footer) { |
||
328 | $footer = $this->footer->getValue(); |
||
329 | if ($this->footer->filtering) { |
||
330 | $fr = $this->getFilteredReader(new StringReader($footer)); |
||
331 | $footer = $fr->read(); |
||
332 | } |
||
333 | |||
334 | $result = $string . $footer; |
||
335 | } |
||
336 | |||
337 | return $result; |
||
338 | } |
||
339 | |||
340 | private function validate(): void |
||
341 | { |
||
342 | if (!$this->skipSanitize) { |
||
343 | $this->sanitizeText(); |
||
344 | } |
||
345 | |||
346 | if (null === $this->file && null === $this->text && 0 === count($this->filesets) && 0 === count($this->filelists)) { |
||
347 | throw new BuildException('You must specify a file, use a filelist/fileset, or specify a text value.'); |
||
348 | } |
||
349 | |||
350 | if (null !== $this->text && (null !== $this->file || count($this->filesets) > 0)) { |
||
351 | throw new BuildException('Cannot use text attribute in conjunction with file or fileset'); |
||
352 | } |
||
353 | |||
354 | if (!$this->eolString) { |
||
355 | $this->eolString = $this->getProject()->getProperty('line.separator'); |
||
356 | } |
||
357 | } |
||
358 | |||
359 | private function sanitizeText(): void |
||
360 | { |
||
361 | if (null !== $this->text && '' === trim($this->text)) { |
||
362 | $this->text = null; |
||
363 | } |
||
364 | } |
||
365 | |||
366 | private function getFilteredReader(Reader $r) |
||
367 | { |
||
368 | return FileUtils::getChainedReader($r, $this->filterChains, $this->getProject()); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Append an array of files in a directory. |
||
373 | * |
||
374 | * @param Writer $writer the FileWriter that is appending to target file |
||
375 | * @param array $files array of files to delete; can be of zero length |
||
376 | * @param File|null $dir directory to work from |
||
377 | */ |
||
378 | private function appendFiles(Writer $writer, $files, ?File $dir = null): void |
||
379 | { |
||
380 | if (!empty($files)) { |
||
381 | $this->log( |
||
382 | 'Attempting to append ' . count( |
||
383 | $files |
||
384 | ) . ' files' . (null !== $dir ? ', using basedir ' . $dir->getPath() : '') |
||
385 | ); |
||
386 | $basenameSlot = Register::getSlot('task.append.current_file'); |
||
387 | $pathSlot = Register::getSlot('task.append.current_file.path'); |
||
388 | foreach ($files as $file) { |
||
389 | try { |
||
390 | if (!$this->checkFilename($file, $dir)) { |
||
391 | continue; |
||
392 | } |
||
393 | |||
394 | if (null !== $dir) { |
||
395 | $file = is_string($file) ? new File($dir->getPath(), $file) : $file; |
||
396 | } else { |
||
397 | $file = is_string($file) ? new File($file) : $file; |
||
398 | } |
||
399 | $basenameSlot->setValue($file); |
||
400 | $pathSlot->setValue($file->getPath()); |
||
401 | $this->appendFile($writer, $file); |
||
402 | } catch (IOException $ioe) { |
||
403 | $this->log( |
||
404 | 'Unable to append contents of file ' . $file . ': ' . $ioe->getMessage(), |
||
405 | Project::MSG_WARN |
||
406 | ); |
||
407 | } catch (\InvalidArgumentException $npe) { |
||
408 | $this->log( |
||
409 | 'Unable to append contents of file ' . $file . ': ' . $npe->getMessage(), |
||
410 | Project::MSG_WARN |
||
411 | ); |
||
412 | } |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | |||
417 | private function checkFilename($filename, $dir = null): bool |
||
418 | { |
||
419 | if (null !== $dir) { |
||
420 | $f = new File($dir, $filename); |
||
421 | } else { |
||
422 | $f = new File($filename); |
||
423 | } |
||
424 | |||
425 | if (!$f->exists()) { |
||
426 | $this->log('File ' . (string) $f . ' does not exist.', Project::MSG_ERR); |
||
427 | |||
428 | return false; |
||
429 | } |
||
430 | if (null !== $this->to && $f->equals($this->to)) { |
||
431 | throw new BuildException( |
||
432 | 'Input file "' |
||
433 | . $f . '" ' |
||
434 | . 'is the same as the output file.' |
||
435 | ); |
||
436 | } |
||
437 | |||
438 | if ( |
||
439 | null !== $this->to |
||
440 | && !$this->overwrite |
||
441 | && $this->to->exists() |
||
442 | && $f->lastModified() > $this->to->lastModified() |
||
443 | ) { |
||
444 | $this->log((string) $this->to . ' is up-to-date.', Project::MSG_VERBOSE); |
||
445 | |||
446 | return false; |
||
447 | } |
||
448 | |||
449 | return true; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @throws IOException |
||
454 | */ |
||
455 | private function appendFile(Writer $writer, File $f): void |
||
472 | } |
||
473 | } |
||
474 | } |
||
475 |