|
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\Ext; |
|
22
|
|
|
|
|
23
|
|
|
use Phing\Exception\BuildException; |
|
24
|
|
|
use Phing\Io\File; |
|
25
|
|
|
use Phing\Project; |
|
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
|
|
|
protected $formatters = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* A php source code filename or directory. |
|
53
|
|
|
* |
|
54
|
|
|
* @var File |
|
55
|
|
|
*/ |
|
56
|
|
|
private $file; |
|
57
|
|
|
|
|
58
|
|
|
/** @var Commandline */ |
|
59
|
|
|
private $cmd; |
|
60
|
|
|
|
|
61
|
|
|
/** @var bool */ |
|
62
|
|
|
private $cache = false; |
|
63
|
|
|
|
|
64
|
|
|
/** @var bool */ |
|
65
|
|
|
private $ignoreAnnotations = false; |
|
66
|
|
|
|
|
67
|
|
|
/** @var bool */ |
|
68
|
|
|
private $checkreturn = false; |
|
69
|
|
|
|
|
70
|
|
|
/** @var string */ |
|
71
|
|
|
private $standard = ''; |
|
72
|
|
|
|
|
73
|
|
|
/** @var string */ |
|
74
|
|
|
private $outfile = ''; |
|
75
|
|
|
|
|
76
|
|
|
/** @var string */ |
|
77
|
|
|
private $format = ''; |
|
78
|
|
|
|
|
79
|
|
|
/** @var string */ |
|
80
|
|
|
private $bin = 'phpcs'; |
|
81
|
|
|
|
|
82
|
6 |
|
public function __construct() |
|
83
|
|
|
{ |
|
84
|
6 |
|
$this->cmd = new Commandline(); |
|
85
|
6 |
|
$this->logLevelName = 'info'; |
|
86
|
6 |
|
parent::__construct(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
public function getCommandline(): Commandline |
|
90
|
|
|
{ |
|
91
|
5 |
|
return $this->cmd; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function setCache(bool $cache): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->cache = $cache; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
public function setIgnoreAnnotations(bool $ignore): void |
|
100
|
|
|
{ |
|
101
|
6 |
|
$this->ignoreAnnotations = $ignore; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
6 |
|
public function setCheckreturn(bool $checkreturn): void |
|
105
|
|
|
{ |
|
106
|
6 |
|
$this->checkreturn = $checkreturn; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
6 |
|
public function setBin(string $bin): void |
|
110
|
|
|
{ |
|
111
|
6 |
|
$this->bin = $bin; |
|
112
|
|
|
} |
|
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
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Create object for nested formatter element. |
|
122
|
|
|
* |
|
123
|
|
|
* @return CodeSnifferFormatterElement |
|
|
|
|
|
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function createFormatter() |
|
126
|
|
|
{ |
|
127
|
1 |
|
$num = array_push( |
|
128
|
1 |
|
$this->formatters, |
|
129
|
1 |
|
new PhpCSTaskFormatterElement() |
|
130
|
1 |
|
); |
|
131
|
|
|
|
|
132
|
1 |
|
return $this->formatters[$num - 1]; |
|
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
|
|
|
} |
|
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
|
|
|
} |
|
151
|
|
|
|
|
152
|
6 |
|
public function main() |
|
153
|
|
|
{ |
|
154
|
6 |
|
if (null === $this->file && 0 == count($this->filesets)) { |
|
155
|
1 |
|
throw new BuildException('Missing both attribute "file" and "fileset".'); |
|
156
|
|
|
} |
|
157
|
5 |
|
if (null === $this->file) { |
|
158
|
|
|
// check filesets, and compile a list of files for phpcs to analyse |
|
159
|
4 |
|
foreach ($this->filesets as $fileset) { |
|
160
|
4 |
|
$files = $fileset->getIterator(); |
|
161
|
4 |
|
foreach ($files as $file) { |
|
162
|
4 |
|
$this->files[] = $file; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
5 |
|
$toExecute = $this->getCommandline(); |
|
168
|
|
|
|
|
169
|
5 |
|
$this->cache |
|
170
|
|
|
? $toExecute->createArgument()->setValue('--cache') |
|
171
|
5 |
|
: $toExecute->createArgument()->setValue('--no-cache'); |
|
172
|
|
|
|
|
173
|
5 |
|
if ($this->ignoreAnnotations) { |
|
174
|
5 |
|
$toExecute->createArgument()->setValue('--ignore-annotations'); |
|
175
|
|
|
} |
|
176
|
5 |
|
if ('' !== $this->format) { |
|
177
|
1 |
|
$toExecute->createArgument()->setValue(' --report=' . $this->format); |
|
178
|
|
|
} |
|
179
|
5 |
|
if ('' !== $this->standard) { |
|
180
|
|
|
$toExecute->createArgument()->setValue(' --standard=' . $this->standard); |
|
181
|
|
|
} |
|
182
|
5 |
|
if ('' !== $this->outfile) { |
|
183
|
1 |
|
$toExecute->createArgument()->setValue(' --report-file=' . $this->outfile); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
5 |
|
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
|
1 |
|
); |
|
193
|
1 |
|
$toExecute->createArgument()->setValue(' --report-' . $formatterType . '=' . $formatterReportFile); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
5 |
|
if (null !== $this->file) { |
|
197
|
1 |
|
$toExecute->createArgument()->setFile($this->file); |
|
198
|
|
|
} else { |
|
199
|
4 |
|
foreach ($this->files as $file) { |
|
200
|
4 |
|
$toExecute->createArgument()->setFile(new File($file)); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
5 |
|
$exe = new ExecTask(); |
|
205
|
5 |
|
$exe->setProject($this->getProject()); |
|
206
|
5 |
|
$exe->setLocation($this->getLocation()); |
|
207
|
5 |
|
$exe->setOwningTarget($this->target); |
|
208
|
5 |
|
$exe->setTaskName($this->getTaskName()); |
|
209
|
5 |
|
$exe->setExecutable($this->bin); |
|
210
|
5 |
|
$exe->setCheckreturn($this->checkreturn); |
|
211
|
5 |
|
$exe->setLevel($this->logLevelName); |
|
212
|
5 |
|
$exe->setExecutable($toExecute->getExecutable()); |
|
213
|
5 |
|
$exe->createArg()->setLine(implode(' ', $toExecute->getArguments())); |
|
214
|
5 |
|
$exe->main(); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths