Passed
Push — main ( f4a49b...9bd4e4 )
by Siad
08:17
created

PhpCSTask::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
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]>Ex
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
    /** @var string */
78
    private $bin = 'phpcs';
79
80 4
    public function __construct()
81
    {
82 4
        $this->cmd = new Commandline();
83 4
        $this->logLevelName = 'info';
84 4
        parent::__construct();
85 4
    }
86
87 3
    public function getCommandline(): Commandline
88
    {
89 3
        return $this->cmd;
90
    }
91
92
    public function setCache(bool $cache): void
93
    {
94
        $this->cache = $cache;
95
    }
96
97 4
    public function setIgnoreAnnotations(bool $ignore): void
98
    {
99 4
        $this->ignoreAnnotations = $ignore;
100 4
    }
101
102 4
    public function setCheckreturn(bool $checkreturn): void
103
    {
104 4
        $this->checkreturn = $checkreturn;
105 4
    }
106
107 4
    public function setBin(string $bin): void
108
    {
109 4
        $this->bin = $bin;
110 4
    }
111
112 1
    public function setFormat(string $format): void
113
    {
114 1
        $this->format = $format;
115 1
        $this->project->log("Format set to $format", Project::MSG_VERBOSE);
116 1
    }
117
118
    public function setStandard(string $standard): void
119
    {
120
        $this->standard = $standard;
121
        $this->project->log("Standard set to $standard", Project::MSG_VERBOSE);
122
    }
123
124 1
    public function setFile(File $file): void
125
    {
126 1
        $this->file = $file;
127 1
    }
128
129 1
    public function setOutfile(string $outfile): void
130
    {
131 1
        $this->outfile = $outfile;
132 1
        $this->project->log("Outfile set to $outfile", Project::MSG_VERBOSE);
133 1
    }
134
135 4
    public function main()
136
    {
137 4
        if (null === $this->file && 0 == count($this->filesets)) {
138 1
            throw new BuildException('Missing both attribute "file" and "fileset".');
139
        }
140 3
        if (null === $this->file) {
141
            // check filesets, and compile a list of files for phpcs to analyse
142 2
            foreach ($this->filesets as $fileset) {
143 2
                $files = $fileset->getIterator();
144 2
                foreach ($files as $file) {
145 2
                    $this->files[] = $file;
146
                }
147
            }
148
        }
149
150 3
        $toExecute = $this->getCommandline();
151
152 3
        $this->cache
153
            ? $toExecute->createArgument()->setValue('--cache')
154 3
            : $toExecute->createArgument()->setValue('--no-cache');
155
156 3
        if ($this->ignoreAnnotations) {
157 3
            $toExecute->createArgument()->setValue('--ignore-annotations');
158
        }
159 3
        if ($this->format !== '') {
160 1
            $toExecute->createArgument()->setValue(' --report=' . $this->format);
161
        }
162 3
        if ($this->standard !== '') {
163
            $toExecute->createArgument()->setValue(' --standard=' . $this->standard);
164
        }
165 3
        if ($this->outfile !== '') {
166 1
            $toExecute->createArgument()->setValue(' --report-file=' . $this->outfile);
167
        }
168
169 3
        if (null !== $this->file) {
170 1
            $toExecute->createArgument()->setFile($this->file);
171
        } else {
172 2
            foreach ($this->files as $file) {
173 2
                $toExecute->createArgument()->setFile(new File($file));
174
            }
175
        }
176
177 3
        $exe = new ExecTask();
178 3
        $exe->setProject($this->getProject());
179 3
        $exe->setLocation($this->getLocation());
180 3
        $exe->setOwningTarget($this->target);
181 3
        $exe->setTaskName($this->getTaskName());
182 3
        $exe->setExecutable($this->bin);
183 3
        $exe->setCheckreturn($this->checkreturn);
184 3
        $exe->setLevel($this->logLevelName);
185 3
        $exe->setExecutable($toExecute->getExecutable());
186 3
        $exe->createArg()->setLine(implode(' ', $toExecute->getArguments()));
187 3
        $exe->main();
188 3
    }
189
}
190