Passed
Push — master ( 830897...939e6f )
by Kyle
01:43 queued 35s
created

PHPMD::hasErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD;
19
20
/**
21
 * This is the main facade of the PHP PMD application
22
 */
23
class PHPMD
24
{
25
    /**
26
     * The current PHPMD version.
27
     */
28
    const VERSION = '@package_version@';
29
30
    /**
31
     * This property will be set to <b>true</b> when an error
32
     * was found in the processed source code.
33
     *
34
     * @var boolean
35
     * @since 2.10.0
36
     */
37
    private $errors = false;
38
39
    /**
40
     * List of valid file extensions for analyzed files.
41
     *
42
     * @var array(string)
43
     */
44
    private $fileExtensions = array('php', 'php3', 'php4', 'php5', 'inc');
45
46
    /**
47
     * List of exclude directory patterns.
48
     *
49
     * @var array(string)
50
     */
51
    private $ignorePatterns = array('.git', '.svn', 'CVS', '.bzr', '.hg', 'SCCS');
52
53
    /**
54
     * The input source file or directory.
55
     *
56
     * @var string
57
     */
58
    private $input;
59
60
    /**
61
     * This property will be set to <b>true</b> when a violation
62
     * was found in the processed source code.
63
     *
64
     * @var boolean
65
     * @since 0.2.5
66
     */
67
    private $violations = false;
68
69
    /**
70
     * Additional options for PHPMD or one of it's parser backends.
71
     *
72
     * @var array
73
     * @since 1.2.0
74
     */
75 7
    private $options = array();
76
77 7
    /**
78
     * This method will return <b>true</b> when the processed source code
79
     * contains errors.
80
     *
81
     * @return boolean
82
     * @since 2.10.0
83
     */
84
    public function hasErrors()
85 11
    {
86
        return $this->errors;
87 11
    }
88
89
    /**
90
     * This method will return <b>true</b> when the processed source code
91
     * contains violations.
92
     *
93
     * @return boolean
94
     * @since 0.2.5
95
     */
96 11
    public function hasViolations()
97
    {
98 11
        return $this->violations;
99
    }
100
101
    /**
102
     * Returns the input source file or directory path.
103
     *
104
     * @return string
105
     */
106
    public function getInput()
107
    {
108
        return $this->input;
109
    }
110
111
    /**
112
     * Returns an array with valid php source file extensions.
113
     *
114
     * @return string[]
115
     * @since 0.2.0
116
     */
117
    public function getFileExtensions()
118 11
    {
119
        return $this->fileExtensions;
120 11
    }
121
122
    /**
123
     * Sets a list of filename extensions for valid php source code files.
124
     *
125
     * @param array<string> $fileExtensions Extensions without leading dot.
126
     * @return void
127
     */
128
    public function setFileExtensions(array $fileExtensions)
129
    {
130
        $this->fileExtensions = $fileExtensions;
131
    }
132
133
    /**
134
     * Returns an array with string patterns that mark a file path as invalid.
135
     *
136
     * @return string[]
137
     * @since 0.2.0
138
     * @deprecated 3.0.0 Use getIgnorePatterns() instead, you always get a list of patterns.
139
     */
140
    public function getIgnorePattern()
141
    {
142
        return $this->getIgnorePatterns();
143 11
    }
144
145 11
    /**
146
     * Returns an array with string patterns that mark a file path invalid.
147
     *
148
     * @return string[]
149
     * @since 2.9.0
150
     */
151
    public function getIgnorePatterns()
152
    {
153
        return $this->ignorePatterns;
154 4
    }
155
156 4
    /**
157 4
     * Sets a list of ignore patterns that is used to exclude directories from
158
     * the source analysis.
159
     *
160
     * @param array<string> $ignorePatterns List of ignore patterns.
161
     * @return void
162
     * @deprecated 3.0.0 Use addIgnorePatterns() instead, both will add an not set the patterns.
163
     */
164
    public function setIgnorePattern(array $ignorePatterns)
165
    {
166
        $this->addIgnorePatterns($ignorePatterns);
167
    }
168
169
    /**
170 11
     * Add a list of ignore patterns which is used to exclude directories from
171
     * the source analysis.
172
     *
173
     * @param array<string> $ignorePatterns List of ignore patterns.
174
     * @return $this
175
     * @since 2.9.0
176
     */
177
    public function addIgnorePatterns(array $ignorePatterns)
178 11
    {
179
        $this->ignorePatterns = array_merge(
180 11
            $this->ignorePatterns,
181
            $ignorePatterns
182 11
        );
183
184 11
        return $this;
185 11
    }
186
187 11
    /**
188 11
     * Returns additional options for PHPMD or one of it's parser backends.
189
     *
190
     * @return array
191 11
     */
192 11
    public function getOptions()
193 11
    {
194
        return $this->options;
195 11
    }
196 11
197
    /**
198
     * Sets additional options for PHPMD or one of it's parser backends.
199 11
     *
200 11
     * @param array $options Additional backend or PHPMD options.
201
     * @return void
202
     */
203 11
    public function setOptions(array $options)
204 11
    {
205
        $this->options = $options;
206
    }
207 11
208 11
    /**
209
     * This method will process all files that can be found in the given input
210
     * path. It will apply rules defined in the comma-separated <b>$ruleSets</b>
211
     * argument. The result will be passed to all given renderer instances.
212
     *
213
     * @param string $inputPath
214
     * @param string $ruleSets
215
     * @param \PHPMD\AbstractRenderer[] $renderers
216
     * @param \PHPMD\RuleSetFactory $ruleSetFactory
217
     * @return void
218
     */
219
    public function processFiles(
220
        $inputPath,
221
        $ruleSets,
222
        array $renderers,
223
        RuleSetFactory $ruleSetFactory
224
    ) {
225
226
        // Merge parsed excludes
227
        $this->addIgnorePatterns($ruleSetFactory->getIgnorePattern($ruleSets));
0 ignored issues
show
Documentation introduced by
$ruleSetFactory->getIgnorePattern($ruleSets) is of type array|null, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
229
        $this->input = $inputPath;
230
231
        $report = new Report();
232
233
        $factory = new ParserFactory();
234
        $parser = $factory->create($this);
235
236
        foreach ($ruleSetFactory->createRuleSets($ruleSets) as $ruleSet) {
237
            $parser->addRuleSet($ruleSet);
238
        }
239
240
        $report->start();
241
        $parser->parse($report);
242
        $report->end();
243
244
        foreach ($renderers as $renderer) {
245
            $renderer->start();
246
        }
247
248
        foreach ($renderers as $renderer) {
249
            $renderer->renderReport($report);
250
        }
251
252
        foreach ($renderers as $renderer) {
253
            $renderer->end();
254
        }
255
256
        $this->errors = $report->hasErrors();
257
        $this->violations = !$report->isEmpty();
258
    }
259
}
260