Passed
Push — master ( 766034...b90d2b )
by Siad
11:11
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 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
nc 17
nop 0
dl 0
loc 44
rs 7.6666
c 1
b 0
f 0
ccs 22
cts 29
cp 0.7586
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
/**
21
 * A PHP code sniffer task. Checking the style of one or more PHP source files.
22
 *
23
 * @author  Siad Ardroumli <[email protected]>
24
 * @package phing.tasks.ext
25
 */
26
class PhpCSTask extends Task
27
{
28
    use LogLevelAware;
29
    use FileSetAware;
30
31
    /**
32
     * A php source code filename or directory
33
     *
34
     * @var PhingFile
35
     */
36
    private $file;
37
38
    /**
39
     * The
40
     *
41
     * @var array
42
     */
43
    protected $files = [];
44
45
46
    /** @var Commandline */
47
    private $cmd;
48
49
    /** @var bool */
50
    private $cache = false;
51
52
    /** @var bool */
53
    private $ignoreAnnotations = false;
54
55
    /** @var bool */
56
    private $checkreturn = false;
57
58
    /** @var string */
59
    private $bin = 'phpcs';
60
61 2
    public function __construct()
62
    {
63 2
        $this->cmd = new Commandline();
64 2
        $this->logLevelName = 'info';
65 2
        parent::__construct();
66 2
    }
67
68 1
    public function getCommandline(): Commandline
69
    {
70 1
        return $this->cmd;
71
    }
72
73
    /**
74
     * @param bool $cache
75
     */
76
    public function setCache(bool $cache): void
77
    {
78
        $this->cache = $cache;
79
    }
80
81
    /**
82
     * @param bool $ignore
83
     */
84 2
    public function setIgnoreAnnotations(bool $ignore): void
85
    {
86 2
        $this->ignoreAnnotations = $ignore;
87 2
    }
88
89
    /**
90
     * @param bool $checkreturn
91
     */
92 2
    public function setCheckreturn(bool $checkreturn): void
93
    {
94 2
        $this->checkreturn = $checkreturn;
95 2
    }
96
97
    /**
98
     * @param string $bin
99
     */
100 2
    public function setBin(string $bin): void
101
    {
102 2
        $this->bin = $bin;
103 2
    }
104
105
    /**
106
     * @param PhingFile $file
107
     */
108 1
    public function setFile(PhingFile $file): void
109
    {
110 1
        $this->file = $file;
111 1
    }
112
113 2
    public function main()
114
    {
115 2
        if ($this->file === null && count($this->filesets) == 0) {
116 1
            throw new BuildException('Missing both attribute "file" and "fileset".');
117
        }
118 1
        if ($this->file === null) {
119
            // check filesets, and compile a list of files for phpcs to analyse
120
            foreach ($this->filesets as $fileset) {
121
                $files = $fileset->getIterator();
122
                foreach ($files as $file) {
123
                    $this->files[] = $file;
124
                }
125
            }
126
        }
127
128 1
        $toExecute = $this->getCommandline();
129
130 1
        $this->cache
131
            ? $toExecute->createArgument()->setValue('--cache')
132 1
            : $toExecute->createArgument()->setValue('--no-cache');
133
134 1
        if ($this->ignoreAnnotations) {
135 1
            $toExecute->createArgument()->setValue('--ignore-annotations');
136
        }
137
138 1
        if ($this->file !== null) {
139 1
            $toExecute->createArgument()->setFile($this->file);
140
        } else {
141
            foreach ($this->files as $file) {
142
                $toExecute->createArgument()->setFile(new PhingFile($file));
143
            }
144
        }
145
146 1
        $exe = new ExecTask();
147 1
        $exe->setProject($this->getProject());
148 1
        $exe->setLocation($this->getLocation());
149 1
        $exe->setOwningTarget($this->target);
150 1
        $exe->setTaskName($this->getTaskName());
151 1
        $exe->setExecutable($this->bin);
152 1
        $exe->setCheckreturn($this->checkreturn);
153 1
        $exe->setLevel($this->logLevelName);
154 1
        $exe->setExecutable($toExecute->getExecutable());
155 1
        $exe->createArg()->setLine(implode(' ', $toExecute->getArguments()));
156 1
        $exe->main();
157 1
    }
158
}
159