Passed
Push — main ( b747fe...be0e2a )
by Siad
06:21
created

PhpCSTask::main()   F

Complexity

Conditions 15
Paths 385

Size

Total Lines 63
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 15.0242

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 63
ccs 40
cts 42
cp 0.9524
rs 2.7708
cc 15
nc 385
nop 0
crap 15.0242

How to fix   Long Method    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
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Task\Optional;
22
23
use Phing\Exception\BuildException;
24
use Phing\Project;
25
use Phing\Io\File;
26
use Phing\Task;
27
use Phing\Task\System\Element\LogLevelAware;
28
use Phing\Task\System\ExecTask;
29
use Phing\Type\Commandline;
30
use Phing\Type\Element\FileSetAware;
31
32
/**
33
 * A PHP code sniffer task. Checking the style of one or more PHP source files.
34
 *
35
 * @author Siad Ardroumli <[email protected]>
36
 */
37
class PhpCSTask extends Task
38
{
39
    use LogLevelAware;
40
    use FileSetAware;
41
42
    /**
43
     * The.
44
     *
45
     * @var array
46
     */
47
    protected $files = [];
48
49
    /**
50
     * A php source code filename or directory.
51
     *
52
     * @var File
53
     */
54
    private $file;
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 $standard = '';
70
71
    /** @var string */
72
    private $outfile = '';
73
74
    /** @var string */
75
    private $format = '';
76
77
    protected $formatters = [];
78
79
    /** @var string */
80
    private $bin = 'phpcs';
81
82 5
    public function __construct()
83
    {
84 5
        $this->cmd = new Commandline();
85 5
        $this->logLevelName = 'info';
86 5
        parent::__construct();
87 5
    }
88
89 4
    public function getCommandline(): Commandline
90
    {
91 4
        return $this->cmd;
92
    }
93
94
    public function setCache(bool $cache): void
95
    {
96
        $this->cache = $cache;
97
    }
98
99 5
    public function setIgnoreAnnotations(bool $ignore): void
100
    {
101 5
        $this->ignoreAnnotations = $ignore;
102 5
    }
103
104 5
    public function setCheckreturn(bool $checkreturn): void
105
    {
106 5
        $this->checkreturn = $checkreturn;
107 5
    }
108
109 5
    public function setBin(string $bin): void
110
    {
111 5
        $this->bin = $bin;
112 5
    }
113
114 1
    public function setFormat(string $format): void
115
    {
116 1
        $this->format = $format;
117 1
        $this->project->log("Format set to $format", Project::MSG_VERBOSE);
118 1
    }
119
120
    /**
121
     * Create object for nested formatter element.
122
     * @return CodeSnifferFormatterElement
0 ignored issues
show
Bug introduced by
The type Phing\Task\Optional\CodeSnifferFormatterElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
123
     */
124 1
    public function createFormatter()
125
    {
126 1
        $num = array_push(
127 1
            $this->formatters,
128 1
            new PhpCSTaskFormatterElement()
129
        );
130
131 1
        return $this->formatters[$num - 1];
132
    }
133
134
135
    public function setStandard(string $standard): void
136
    {
137
        $this->standard = $standard;
138
        $this->project->log("Standard set to $standard", Project::MSG_VERBOSE);
139
    }
140
141 1
    public function setFile(File $file): void
142
    {
143 1
        $this->file = $file;
144 1
    }
145
146 1
    public function setOutfile(string $outfile): void
147
    {
148 1
        $this->outfile = $outfile;
149 1
        $this->project->log("Outfile set to $outfile", Project::MSG_VERBOSE);
150 1
    }
151
152 5
    public function main()
153
    {
154 5
        if (null === $this->file && 0 == count($this->filesets)) {
155 1
            throw new BuildException('Missing both attribute "file" and "fileset".');
156
        }
157 4
        if (null === $this->file) {
158
            // check filesets, and compile a list of files for phpcs to analyse
159 3
            foreach ($this->filesets as $fileset) {
160 3
                $files = $fileset->getIterator();
161 3
                foreach ($files as $file) {
162 3
                    $this->files[] = $file;
163
                }
164
            }
165
        }
166
167 4
        $toExecute = $this->getCommandline();
168
169 4
        $this->cache
170
            ? $toExecute->createArgument()->setValue('--cache')
171 4
            : $toExecute->createArgument()->setValue('--no-cache');
172
173 4
        if ($this->ignoreAnnotations) {
174 4
            $toExecute->createArgument()->setValue('--ignore-annotations');
175
        }
176 4
        if ($this->format !== '') {
177 1
            $toExecute->createArgument()->setValue(' --report=' . $this->format);
178
        }
179 4
        if ($this->standard !== '') {
180
            $toExecute->createArgument()->setValue(' --standard=' . $this->standard);
181
        }
182 4
        if ($this->outfile !== '') {
183 1
            $toExecute->createArgument()->setValue(' --report-file=' . $this->outfile);
184
        }
185
186 4
        foreach ($this->formatters as $formatter) {
187 1
            $formatterReportFile = ($formatter->getUseFile() ? $formatter->getOutFile() : null);
188 1
            $formatterType = $formatter->getType();
189 1
            $this->project->log(
190 1
                "Generate report of type \"{$formatterType}\" with report written to $formatterReportFile",
191 1
                Project::MSG_VERBOSE
192
            );
193 1
            $toExecute->createArgument()->setValue(' --report-' . $formatterType . '=' . $formatterReportFile);
194
        }
195
196 4
        if (null !== $this->file) {
197 1
            $toExecute->createArgument()->setFile($this->file);
198
        } else {
199 3
            foreach ($this->files as $file) {
200 3
                $toExecute->createArgument()->setFile(new File($file));
201
            }
202
        }
203
204 4
        $exe = new ExecTask();
205 4
        $exe->setProject($this->getProject());
206 4
        $exe->setLocation($this->getLocation());
207 4
        $exe->setOwningTarget($this->target);
208 4
        $exe->setTaskName($this->getTaskName());
209 4
        $exe->setExecutable($this->bin);
210 4
        $exe->setCheckreturn($this->checkreturn);
211 4
        $exe->setLevel($this->logLevelName);
212 4
        $exe->setExecutable($toExecute->getExecutable());
213 4
        $exe->createArg()->setLine(implode(' ', $toExecute->getArguments()));
214 4
        $exe->main();
215 4
    }
216
}
217