RemoveTask   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 0
loc 123
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setIo() 0 6 1
C execute() 0 51 12
B removeEmptyDirectories() 0 29 5
1
<?php
2
3
namespace Webcreate\Conveyor\Task;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Finder\Finder;
7
use Webcreate\Conveyor\IO\IOInterface;
8
use Webcreate\Conveyor\Repository\Version;
9
use Webcreate\Conveyor\Task\Result\ExecuteResult;
10
use Webcreate\Conveyor\Util\FileCollection;
11
12
class RemoveTask extends Task
13
{
14
    protected $cwd;
15
16
    /**
17
     * @var IOInterface
18
     */
19
    protected $io;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
20
21
    /**
22
     * @param $cwd
23
     */
24
    function __construct($cwd)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __construct.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
25
    {
26
        $this->cwd = $cwd;
27
    }
28
29
    /**
30
     * @param IOInterface $io
31
     *
32
     * @return $this
33
     */
34
    public function setIo(IOInterface $io)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
35
    {
36
        $this->io = $io;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param string $target
43
     * @param Version $version
44
     *
45
     * @return ExecuteResult
46
     */
47
    public function execute($target, Version $version)
0 ignored issues
show
Complexity introduced by
This operation has 432 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
48
    {
49
        $filesystem = new Filesystem();
50
51
        if ($this->io && !$this->io->isVerbose()) {
52
            $this->output(sprintf('Removing: <comment>0%%</comment>'));
53
        }
54
55
        $collection = new FileCollection($this->cwd);
56
57
        if (!$this->options['files'] && $this->options['exclude']) {
58
            $collection->add('*');
59
        }
60
61
        foreach ($this->options['files'] as $file) {
62
            $collection->add($file);
63
        }
64
65
        foreach ($this->options['exclude'] as $file) {
66
            $collection->remove($file);
67
        }
68
69
        $files = $collection->toArray();
70
71
        $total = count($files);
72
        $i = 0;
73
74
        foreach ($files as $file) {
75
            $filename = sprintf('%s/%s', rtrim($this->cwd, DIRECTORY_SEPARATOR), $file);
76
77
            $filesystem->remove($filename);
78
79
            if ($this->io && !$this->io->isVerbose()) {
80
                $this->output(sprintf('Removing: <comment>%d%%</comment>', round(++$i * 100 / $total)));
81
            } else {
82
                $this->output(sprintf("Removed %s", $file));
83
            }
84
85
            // remove empty directories
86
            $this->removeEmptyDirectories($this->cwd, $file);
87
        }
88
89
        if ($this->io && !$this->io->isVerbose()) {
90
            $this->output(sprintf('Removing: <comment>%d%%</comment>', 100));
91
        }
92
93
        return new ExecuteResult(
94
            array(),
95
            $files
96
        );
97
    }
98
99
    /**
100
     * Removes empty directories
101
     *
102
     * @param $cwd
103
     * @param $file
104
     */
105
    protected function removeEmptyDirectories($cwd, $file)
106
    {
107
        $filesystem = new Filesystem();
108
109
        $filename = sprintf('%s/%s', rtrim($cwd, DIRECTORY_SEPARATOR), $file);
110
111
        while ('.' !== dirname($file)) {
112
            $file     = dirname($file);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $file. This often makes code more readable.
Loading history...
113
            $filename = dirname($filename);
114
115
            $finder = new Finder();
116
            $countFiles = $finder
117
                ->files()
118
                ->in($filename)
119
                ->ignoreDotFiles(false)
120
                ->ignoreVCS(false)
121
                ->count();
122
123
            if ($countFiles === 0) {
124
                $filesystem->remove($filename);
125
126
                if ($this->io && $this->io->isVerbose()) {
127
                    $this->output(sprintf("Removed %s", $file));
128
                }
129
            } else {
130
                return;
131
            }
132
        }
133
    }
134
}
135