These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Robo\Task\Filesystem; |
||
3 | |||
4 | use Robo\Common\ResourceExistenceChecker; |
||
5 | use Robo\Result; |
||
6 | |||
7 | /** |
||
8 | * Deletes all files from specified dir, ignoring git files. |
||
9 | * |
||
10 | * ``` php |
||
11 | * <?php |
||
12 | * $this->taskCleanDir(['tmp','logs'])->run(); |
||
13 | * // as shortcut |
||
14 | * $this->_cleanDir('app/cache'); |
||
15 | * ?> |
||
16 | * ``` |
||
17 | */ |
||
18 | class CleanDir extends BaseDir |
||
19 | { |
||
20 | use ResourceExistenceChecker; |
||
21 | |||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | View Code Duplication | public function run() |
|
0 ignored issues
–
show
|
|||
26 | { |
||
27 | if (!$this->checkResources($this->dirs, 'dir')) { |
||
28 | return Result::error($this, 'Source directories are missing!'); |
||
29 | } |
||
30 | foreach ($this->dirs as $dir) { |
||
31 | $this->emptyDir($dir); |
||
32 | $this->printTaskInfo("Cleaned {dir}", ['dir' => $dir]); |
||
33 | } |
||
34 | return Result::success($this); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param string $path |
||
39 | */ |
||
40 | protected function emptyDir($path) |
||
41 | { |
||
42 | $iterator = new \RecursiveIteratorIterator( |
||
43 | new \RecursiveDirectoryIterator($path), |
||
44 | \RecursiveIteratorIterator::CHILD_FIRST |
||
45 | ); |
||
46 | |||
47 | foreach ($iterator as $path) { |
||
48 | if ($path->isDir()) { |
||
49 | $dir = (string)$path; |
||
50 | if (basename($dir) === '.' || basename($dir) === '..') { |
||
51 | continue; |
||
52 | } |
||
53 | $this->fs->remove($dir); |
||
54 | } else { |
||
55 | $file = (string)$path; |
||
56 | if (basename($file) === '.gitignore' || basename($file) === '.gitkeep') { |
||
57 | continue; |
||
58 | } |
||
59 | $this->fs->remove($file); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.