ErrorCRAP::mapIssues()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 5
nop 2
dl 0
loc 35
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * CRAP
5
 *
6
 * PHP Version 5.3.2
7
 *
8
 * Copyright (c) 2007-2010, Mayflower GmbH
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 *
15
 *   * Redistributions of source code must retain the above copyright
16
 *     notice, this list of conditions and the following disclaimer.
17
 *
18
 *   * Redistributions in binary form must reproduce the above copyright
19
 *     notice, this list of conditions and the following disclaimer in
20
 *     the documentation and/or other materials provided with the
21
 *     distribution.
22
 *
23
 *   * Neither the name of Mayflower GmbH nor the names of his
24
 *     contributors may be used to endorse or promote products derived
25
 *     from this software without specific prior written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
 * POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @category PHP_CodeBrowser
41
 *
42
 * @author Simon Kohlmeyer <[email protected]>
43
 *
44
 * @copyright 2007-2010 Mayflower GmbH
45
 *
46
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
47
 *
48
 * @version SVN: $Id$
49
 *
50
 * @link http://www.phpunit.de/
51
 *
52
 * @since File available since  0.2.0
53
 */
54
55
namespace PHPCodeBrowser\Plugins;
56
57
use DOMElement;
58
use DOMNodeList;
59
use PHPCodeBrowser\AbstractPlugin;
60
use PHPCodeBrowser\Issue;
61
62
/**
63
 * ErrorCRAP
64
 *
65
 * @category PHP_CodeBrowser
66
 *
67
 * @author Simon Kohlmeyer <[email protected]>
68
 *
69
 * @copyright 2007-2010 Mayflower GmbH
70
 *
71
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
72
 *
73
 * @version Release: @package_version@
74
 *
75
 * @link http://www.phpunit.de/
76
 *
77
 * @since Class available since  0.2.0
78
 */
79
class ErrorCRAP extends AbstractPlugin
80
{
81
    /**
82
     * Name of this plugin.
83
     * Used to read issues from XML.
84
     *
85
     * @var string
86
     */
87
    protected $pluginName = 'coverage';
88
89
    /**
90
     * Name of the attribute that holds the number of the first line
91
     * of the issue.
92
     *
93
     * @var string
94
     */
95
    protected $lineStartAttr = 'num';
96
97
    /**
98
     * Name of the attribute that holds the number of the last line
99
     * of the issue.
100
     *
101
     * @var string
102
     */
103
    protected $lineEndAttr = 'num';
104
105
    /**
106
     * Default string to use as source for issue.
107
     *
108
     * @var string
109
     */
110
    protected $source = 'CRAP';
111
112
    /**
113
     * The detailed mapper method for each single plugin, returning an array
114
     * of Issue objects.
115
     * This method provides a default behaviour an can be overloaded to
116
     * implement special behavior for other plugins.
117
     *
118
     * @param \DOMNode $element  The XML plugin node with its errors
119
     * @param string   $filename Name of the file to return issues for.
120
     *
121
     * @return array<Issue>
122
     */
123
    public function mapIssues(\DOMNode $element, string $filename): array
124
    {
125
        $errorList = [];
126
127
        foreach ($element->childNodes as $child) {
128
            if (!($child instanceof DOMElement)
129
                || 'line' !== $child->nodeName
130
                || 'method' !== $child->getAttribute('type')
131
            ) {
132
                continue;
133
            }
134
135
            $crap = $child->getAttribute('crap');
136
137
            if (!$crap) {
138
                continue;
139
            }
140
141
            if (\array_key_exists('threshold', $this->options)
142
                && $crap <= $this->options['threshold']
143
            ) {
144
                continue;
145
            }
146
147
            $errorList[] = new Issue(
148
                $filename,
149
                $this->getLineStart($child),
150
                $this->getLineEnd($child),
151
                $this->getSource(),
152
                $crap,
153
                $crap >= 30 ? 'Error' : 'Notice'
154
            );
155
        }
156
157
        return $errorList;
158
    }
159
160
    /**
161
     * Get an array with all files that have issues.
162
     *
163
     * @return array
164
     */
165
    public function getFilesWithIssues(): array
166
    {
167
        $fileNames  = [];
168
        $issueNodes = $this->issueXml->query(
169
            '/*/'.$this->pluginName.'/*/file[@name]'
170
        );
171
172
        foreach ($issueNodes as $node) {
173
            if (!($node instanceof DOMElement)) {
174
                continue;
175
            }
176
177
            $fileNames[] = $node->getAttribute('name');
178
        }
179
180
        return \array_unique($fileNames);
181
    }
182
183
    /**
184
     * Get all DOMNodes that represent issues for a specific file.
185
     *
186
     * @param string $filename Name of the file to get nodes for.
187
     *
188
     * @return DOMNodeList
189
     */
190
    protected function getIssueNodes(string $filename): DOMNodeList
191
    {
192
        return $this->issueXml->query(
193
            '/*/'.$this->pluginName.'/*/file[@name="'.$filename.'"]'
194
        );
195
    }
196
}
197