Passed
Push — main ( e5a85d...619edc )
by Michiel
07:04
created

ZendCodeAnalyzerTask::setDisable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\ZendCodeAnalyzer;
22
23
use Phing\Exception\BuildException;
24
use Phing\Io\File;
25
use Phing\Project;
26
use Phing\Task;
27
use Phing\Type\Element\FileSetAware;
28
29
/**
30
 * ZendCodeAnalyzerTask analyze PHP source code using the ZendCodeAnalyzer included in Zend Studio 5.1
31
 *
32
 * Available warnings:
33
 * <b>zend-error</b> - %s(line %d): %s
34
 * <b>oneline-comment</b> - One-line comment ends with  tag.
35
 * <b>bool-assign</b> - Assignment seen where boolean expression is expected. Did you mean '==' instead of '='?
36
 * <b>bool-print</b> - Print statement used when boolean expression is expected.
37
 * <b>bool-array</b> - Array used when boolean expression is expected.
38
 * <b>bool-object</b> - Object used when boolean expression is expected.
39
 * <b>call-time-ref</b> - Call-time reference is deprecated. Define function as accepting parameter by reference instead.
40
 * <b>if-if-else</b> - In if-if-else construction else relates to the closest if. Use braces to make the code clearer.
41
 * <b>define-params</b> - define() requires two or three parameters.
42
 * <b>define-const</b> - First parameter for define() should be string. Maybe you forgot quotes?
43
 * <b>break-var</b> - Break/continue with variable is dangerous - break level can be out of scope.
44
 * <b>break-depth</b> - Break/continue with depth more than current nesting level.
45
 * <b>var-once</b> - Variable '%s' encountered only once. May be a typo?
46
 * <b>var-arg-unused</b> - Function argument '%s' is never used.
47
 * <b>var-global-unused</b> - Global variable '%s' is defined but never used.
48
 * <b>var-use-before-def</b> - Variable '%s' is used before it was assigned.
49
 * <b>var-use-before-def-global</b> - Global variable '%s' is used without being assigned. You are probably relying on register_globals feature of PHP. Note that this feature is off by default.
50
 * <b>var-no-global</b> - PHP global variable '%s' is used as local. Maybe you wanted to define '%s' as global?
51
 * <b>var-value-unused</b> - Value assigned to variable '%s' is never used
52
 * <b>var-ref-notmodified</b> - Function parameter '%s' is passed by reference but never modified. Consider passing by value.
53
 * <b>return-empty-val</b> - Function '%s' has both empty return and return with value.
54
 * <b>return-empty-used</b> - Function '%s' has empty return but return value is used.
55
 * <b>return-noref</b> - Function '%s' returns reference but the value is not assigned by reference. Maybe you meant '=&' instead of '='?
56
 * <b>return-end-used</b> - Control reaches the end of function '%s'(file %s, line %d) but return value is used.
57
 * <b>sprintf-miss-args</b> - Missing arguments for sprintf: format reqires %d arguments but %d are supplied.
58
 * <b>sprintf-extra-args</b> - Extra arguments for sprintf: format reqires %d arguments but %d are supplied.
59
 * <b>unreach-code</b> - Unreachable code in function '%s'.
60
 * <b>include-var</b> - include/require with user-accessible variable can be dangerous. Consider using constant instead.
61
 * <b>non-object</b> - Variable '%s' used as object, but has different type.
62
 * <b>bad-escape</b> - Bad escape sequence: \%c, did you mean \\%c?
63
 * <b>empty-cond</b> - Condition without a body
64
 * <b>expr-unused</b> - Expression result is never used
65
 *
66
 * @author  Knut Urdalen <[email protected]>
67
 * @package phing.tasks.ext
68
 */
69
class ZendCodeAnalyzerTask extends Task
70
{
71
    use FileSetAware;
72
73
    protected $analyzerPath = ""; // Path to ZendCodeAnalyzer binary
74
    protected $file = ""; // the source file (from xml attribute)
75
    protected $counter = 0;
76
    protected $disable = [];
77
    protected $enable = [];
78
79
    private $haltonwarning = false;
80
81
    /**
82
     * File to be analyzed
83
     *
84
     * @param File $file
85
     */
86
    public function setFile(File $file)
87
    {
88
        $this->file = $file;
89
    }
90
91
    /**
92
     * Path to ZendCodeAnalyzer binary
93
     *
94
     * @param string $analyzerPath
95
     */
96
    public function setAnalyzerPath($analyzerPath)
97
    {
98
        $this->analyzerPath = $analyzerPath;
99
    }
100
101
    /**
102
     * Disable warning levels. Separate warning levels with ','
103
     *
104
     * @param string $disable
105
     */
106
    public function setDisable($disable)
107
    {
108
        $this->disable = explode(",", $disable);
109
    }
110
111
    /**
112
     * Enable warning levels. Separate warning levels with ','
113
     *
114
     * @param string $enable
115
     */
116
    public function setEnable($enable)
117
    {
118
        $this->enable = explode(",", $enable);
119
    }
120
121
    /**
122
     * Sets the haltonwarning flag
123
     *
124
     * @param boolean $value
125
     */
126
    public function setHaltonwarning($value)
127
    {
128
        $this->haltonwarning = $value;
129
    }
130
131
    /**
132
     * Analyze against PhingFile or a FileSet
133
     */
134
    public function main()
135
    {
136
        if (!isset($this->analyzerPath)) {
137
            throw new BuildException("Missing attribute 'analyzerPath'");
138
        }
139
140
        if (!isset($this->file) and count($this->filesets) == 0) {
141
            throw new BuildException("Missing either a nested fileset or attribute 'file' set");
142
        }
143
144
        if ($this->file instanceof File) {
0 ignored issues
show
introduced by
$this->file is never a sub-type of Phing\Io\File.
Loading history...
145
            $this->analyze($this->file->getPath());
146
        } else { // process filesets
147
            $project = $this->getProject();
148
149
            foreach ($this->filesets as $fs) {
150
                $ds = $fs->getDirectoryScanner($project);
151
                $files = $ds->getIncludedFiles();
152
                $dir = $fs->getDir($this->project)->getPath();
153
154
                foreach ($files as $file) {
155
                    $this->analyze($dir . DIRECTORY_SEPARATOR . $file);
156
                }
157
            }
158
        }
159
160
        $this->log("Number of findings: " . $this->counter, Project::MSG_INFO);
161
    }
162
163
    /**
164
     * Analyze file
165
     *
166
     * @param  string $file
167
     * @throws BuildException
168
     * @return void
169
     */
170
    protected function analyze($file)
171
    {
172
        if (file_exists($file)) {
173
            if (is_readable($file)) {
174
                // Construct shell command
175
                $cmd = $this->analyzerPath . " ";
176
177
                foreach ($this->enable as $enable) { // Enable warning levels
178
                    $cmd .= " --enable $enable ";
179
                }
180
181
                foreach ($this->disable as $disable) { // Disable warning levels
182
                    $cmd .= " --disable $disable ";
183
                }
184
185
                $cmd .= "$file 2>&1";
186
187
                // Execute command
188
                $result = shell_exec($cmd);
189
                $result = explode("\n", $result);
190
191
                for ($i = 2, $size = count($result); $i < ($size - 1); $i++) {
192
                    $this->counter++;
193
                    $this->log($result[$i], Project::MSG_WARN);
194
                }
195
196
                $total = count($result) - 3;
197
198
                if ($total > 0 && $this->haltonwarning) {
199
                    throw new BuildException('zendcodeanalyzer detected ' . $total . ' warning' . ($total > 1 ? 's' : '') . ' in ' . $file);
200
                }
201
            } else {
202
                throw new BuildException('Permission denied: ' . $file);
203
            }
204
        } else {
205
            throw new BuildException('File not found: ' . $file);
206
        }
207
    }
208
}
209