Passed
Push — main ( b7649a...398f47 )
by Michiel
06:32
created

PhpCSTask::main()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11.4067

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 44
ccs 22
cts 29
cp 0.7586
rs 7.6666
c 0
b 0
f 0
cc 10
nc 17
nop 0
crap 11.4067

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Task\Optional;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\File;
24
use Phing\Task;
25
use Phing\Task\System\Element\LogLevelAware;
26
use Phing\Task\System\ExecTask;
27
use Phing\Type\Commandline;
28
use Phing\Type\Element\FileSetAware;
29
30
/**
31
 * A PHP code sniffer task. Checking the style of one or more PHP source files.
32
 *
33
 * @author  Siad Ardroumli <[email protected]>Ex
34
 * @package phing.tasks.ext
35
 */
36
class PhpCSTask extends Task
37
{
38
    use LogLevelAware;
39
    use FileSetAware;
40
41
    /**
42
     * A php source code filename or directory
43
     *
44
     * @var File
45
     */
46
    private $file;
47
48
    /**
49
     * The
50
     *
51
     * @var array
52
     */
53
    protected $files = [];
54
55
56
    /** @var Commandline */
57
    private $cmd;
58
59
    /** @var bool */
60
    private $cache = false;
61
62
    /** @var bool */
63
    private $ignoreAnnotations = false;
64
65
    /** @var bool */
66
    private $checkreturn = false;
67
68
    /** @var string */
69
    private $bin = 'phpcs';
70
71 2
    public function __construct()
72
    {
73 2
        $this->cmd = new Commandline();
74 2
        $this->logLevelName = 'info';
75 2
        parent::__construct();
76 2
    }
77
78 1
    public function getCommandline(): Commandline
79
    {
80 1
        return $this->cmd;
81
    }
82
83
    /**
84
     * @param bool $cache
85
     */
86
    public function setCache(bool $cache): void
87
    {
88
        $this->cache = $cache;
89
    }
90
91
    /**
92
     * @param bool $ignore
93
     */
94 2
    public function setIgnoreAnnotations(bool $ignore): void
95
    {
96 2
        $this->ignoreAnnotations = $ignore;
97 2
    }
98
99
    /**
100
     * @param bool $checkreturn
101
     */
102 2
    public function setCheckreturn(bool $checkreturn): void
103
    {
104 2
        $this->checkreturn = $checkreturn;
105 2
    }
106
107
    /**
108
     * @param string $bin
109
     */
110 2
    public function setBin(string $bin): void
111
    {
112 2
        $this->bin = $bin;
113 2
    }
114
115
    /**
116
     * @param File $file
117
     */
118 1
    public function setFile(File $file): void
119
    {
120 1
        $this->file = $file;
121 1
    }
122
123 2
    public function main()
124
    {
125 2
        if ($this->file === null && count($this->filesets) == 0) {
126 1
            throw new BuildException('Missing both attribute "file" and "fileset".');
127
        }
128 1
        if ($this->file === null) {
129
            // check filesets, and compile a list of files for phpcs to analyse
130
            foreach ($this->filesets as $fileset) {
131
                $files = $fileset->getIterator();
132
                foreach ($files as $file) {
133
                    $this->files[] = $file;
134
                }
135
            }
136
        }
137
138 1
        $toExecute = $this->getCommandline();
139
140 1
        $this->cache
141
            ? $toExecute->createArgument()->setValue('--cache')
142 1
            : $toExecute->createArgument()->setValue('--no-cache');
143
144 1
        if ($this->ignoreAnnotations) {
145 1
            $toExecute->createArgument()->setValue('--ignore-annotations');
146
        }
147
148 1
        if ($this->file !== null) {
149 1
            $toExecute->createArgument()->setFile($this->file);
150
        } else {
151
            foreach ($this->files as $file) {
152
                $toExecute->createArgument()->setFile(new File($file));
153
            }
154
        }
155
156 1
        $exe = new ExecTask();
157 1
        $exe->setProject($this->getProject());
158 1
        $exe->setLocation($this->getLocation());
159 1
        $exe->setOwningTarget($this->target);
160 1
        $exe->setTaskName($this->getTaskName());
161 1
        $exe->setExecutable($this->bin);
162 1
        $exe->setCheckreturn($this->checkreturn);
163 1
        $exe->setLevel($this->logLevelName);
164 1
        $exe->setExecutable($toExecute->getExecutable());
165 1
        $exe->createArg()->setLine(implode(' ', $toExecute->getArguments()));
166 1
        $exe->main();
167 1
    }
168
}
169