| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
6 |
* All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
* Redistribution and use in source and binary forms, with or without
|
|
|
9 |
* modification, are permitted provided that the following conditions
|
|
|
10 |
* are met:
|
|
|
11 |
*
|
|
|
12 |
* * Redistributions of source code must retain the above copyright
|
|
|
13 |
* notice, this list of conditions and the following disclaimer.
|
|
|
14 |
*
|
|
|
15 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
16 |
* notice, this list of conditions and the following disclaimer in
|
|
|
17 |
* the documentation and/or other materials provided with the
|
|
|
18 |
* distribution.
|
|
|
19 |
*
|
|
|
20 |
* * Neither the name of Sebastian Bergmann nor the names of his
|
|
|
21 |
* contributors may be used to endorse or promote products derived
|
|
|
22 |
* from this software without specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
25 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
26 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
27 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
28 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
29 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
30 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
31 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
32 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
33 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
34 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
35 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
36 |
*
|
|
|
37 |
* @category Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
40 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
41 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
42 |
* @link http://www.phpunit.de/
|
|
|
43 |
* @since File available since Release 3.2.0
|
|
|
44 |
*/
|
|
|
45 |
|
|
|
46 |
require_once 'PHPUnit/Runner/Version.php';
|
|
|
47 |
require_once 'PHPUnit/Util/Metrics/Project.php';
|
|
|
48 |
require_once 'PHPUnit/Util/Log/PMD/Rule/Class.php';
|
|
|
49 |
require_once 'PHPUnit/Util/Log/PMD/Rule/File.php';
|
|
|
50 |
require_once 'PHPUnit/Util/Log/PMD/Rule/Function.php';
|
|
|
51 |
require_once 'PHPUnit/Util/Log/PMD/Rule/Project.php';
|
|
|
52 |
require_once 'PHPUnit/Util/Class.php';
|
|
|
53 |
require_once 'PHPUnit/Util/CodeCoverage.php';
|
|
|
54 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
55 |
require_once 'PHPUnit/Util/FilterIterator.php';
|
|
|
56 |
require_once 'PHPUnit/Util/Printer.php';
|
|
|
57 |
|
|
|
58 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Generates an XML logfile with software metrics information using the
|
|
|
62 |
* PMD format "documented" at
|
|
|
63 |
* http://svn.atlassian.com/fisheye/browse/~raw,r=7084/public/contrib/bamboo/bamboo-pmd-plugin/trunk/src/test/resources/test-pmd-report.xml
|
|
|
64 |
*
|
|
|
65 |
* @category Testing
|
|
|
66 |
* @package PHPUnit
|
|
|
67 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
68 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
69 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
70 |
* @version Release: 3.4.15
|
|
|
71 |
* @link http://www.phpunit.de/
|
|
|
72 |
* @since Class available since Release 3.2.0
|
|
|
73 |
*/
|
|
|
74 |
class PHPUnit_Util_Log_PMD extends PHPUnit_Util_Printer
|
|
|
75 |
{
|
|
|
76 |
protected $added;
|
|
|
77 |
|
|
|
78 |
protected $rules = array(
|
|
|
79 |
'project' => array(),
|
|
|
80 |
'file' => array(),
|
|
|
81 |
'class' => array(),
|
|
|
82 |
'function' => array()
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Constructor.
|
|
|
87 |
*
|
|
|
88 |
* @param mixed $out
|
|
|
89 |
* @param array $configuration
|
|
|
90 |
* @throws InvalidArgumentException
|
|
|
91 |
*/
|
|
|
92 |
public function __construct($out = NULL, array $configuration = array())
|
|
|
93 |
{
|
|
|
94 |
parent::__construct($out);
|
|
|
95 |
$this->loadClasses($configuration);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* @param PHPUnit_Framework_TestResult $result
|
|
|
100 |
*/
|
|
|
101 |
public function process(PHPUnit_Framework_TestResult $result)
|
|
|
102 |
{
|
|
|
103 |
$codeCoverage = $result->getCodeCoverageInformation();
|
|
|
104 |
$summary = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage);
|
|
|
105 |
$files = array_keys($summary);
|
|
|
106 |
$metrics = new PHPUnit_Util_Metrics_Project($files, $summary);
|
|
|
107 |
|
|
|
108 |
$document = new DOMDocument('1.0', 'UTF-8');
|
|
|
109 |
$document->formatOutput = TRUE;
|
|
|
110 |
|
|
|
111 |
$pmd = $document->createElement('pmd');
|
|
|
112 |
$pmd->setAttribute('version', 'PHPUnit ' . PHPUnit_Runner_Version::id());
|
|
|
113 |
$document->appendChild($pmd);
|
|
|
114 |
|
|
|
115 |
foreach ($this->rules['project'] as $ruleName => $rule) {
|
|
|
116 |
$result = $rule->apply($metrics);
|
|
|
117 |
|
|
|
118 |
if ($result !== NULL) {
|
|
|
119 |
$this->addViolation(
|
|
|
120 |
$result,
|
|
|
121 |
$pmd,
|
|
|
122 |
$rule
|
|
|
123 |
);
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
foreach ($metrics->getFiles() as $fileName => $fileMetrics) {
|
|
|
128 |
$xmlFile = $document->createElement('file');
|
|
|
129 |
$xmlFile->setAttribute('name', $fileName);
|
|
|
130 |
|
|
|
131 |
$this->added = FALSE;
|
|
|
132 |
|
|
|
133 |
foreach ($this->rules['file'] as $ruleName => $rule) {
|
|
|
134 |
$result = $rule->apply($fileMetrics);
|
|
|
135 |
|
|
|
136 |
if ($result !== NULL) {
|
|
|
137 |
$this->addViolation(
|
|
|
138 |
$result,
|
|
|
139 |
$xmlFile,
|
|
|
140 |
$rule
|
|
|
141 |
);
|
|
|
142 |
|
|
|
143 |
$this->added = TRUE;
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
foreach ($fileMetrics->getClasses() as $className => $classMetrics) {
|
|
|
148 |
if (!$classMetrics->getClass()->isInterface()) {
|
|
|
149 |
$classStartLine = $classMetrics->getClass()->getStartLine();
|
|
|
150 |
$classEndLine = $classMetrics->getClass()->getEndLine();
|
|
|
151 |
$classPackage = $classMetrics->getPackage();
|
|
|
152 |
|
|
|
153 |
foreach ($this->rules['class'] as $ruleName => $rule) {
|
|
|
154 |
$result = $rule->apply($classMetrics);
|
|
|
155 |
|
|
|
156 |
if ($result !== NULL) {
|
|
|
157 |
$this->addViolation(
|
|
|
158 |
$result,
|
|
|
159 |
$xmlFile,
|
|
|
160 |
$rule,
|
|
|
161 |
$classStartLine,
|
|
|
162 |
$classEndLine,
|
|
|
163 |
$classPackage,
|
|
|
164 |
$className
|
|
|
165 |
);
|
|
|
166 |
|
|
|
167 |
$this->added = TRUE;
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
foreach ($classMetrics->getMethods() as $methodName => $methodMetrics) {
|
|
|
172 |
if (!$methodMetrics->getMethod()->isAbstract()) {
|
|
|
173 |
$this->processFunctionOrMethod($xmlFile, $methodMetrics, $classPackage);
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
foreach ($fileMetrics->getFunctions() as $functionName => $functionMetrics) {
|
|
|
180 |
$this->processFunctionOrMethod($xmlFile, $functionMetrics);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
if ($this->added) {
|
|
|
184 |
$pmd->appendChild($xmlFile);
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
$this->write($document->saveXML());
|
|
|
189 |
$this->flush();
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* @param string $violation
|
|
|
194 |
* @param DOMElement $element
|
|
|
195 |
* @param PHPUnit_Util_Log_PMD_Rule $rule
|
|
|
196 |
* @param integer $line
|
|
|
197 |
* @param integer $toLine
|
|
|
198 |
* @param string $package
|
|
|
199 |
* @param string $class
|
|
|
200 |
* @param string $method
|
|
|
201 |
*/
|
|
|
202 |
protected function addViolation($violation, DOMElement $element, PHPUnit_Util_Log_PMD_Rule $rule, $line = '', $toLine = '', $package = '', $class = '', $method = '', $function = '')
|
|
|
203 |
{
|
|
|
204 |
$violationXml = $element->appendChild(
|
|
|
205 |
$element->ownerDocument->createElement('violation', $violation)
|
|
|
206 |
);
|
|
|
207 |
|
|
|
208 |
$violationXml->setAttribute('rule', $rule->getName());
|
|
|
209 |
$violationXml->setAttribute('priority', $rule->getPriority());
|
|
|
210 |
|
|
|
211 |
if (!empty($line)) {
|
|
|
212 |
$violationXml->setAttribute('line', $line);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
if (!empty($toLine)) {
|
|
|
216 |
$violationXml->setAttribute('to-line', $toLine);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
if (empty($package)) {
|
|
|
220 |
$package = 'global';
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
if (!empty($package)) {
|
|
|
224 |
$violationXml->setAttribute('package', $package);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
if (!empty($class)) {
|
|
|
228 |
$violationXml->setAttribute('class', $class);
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
if (!empty($method)) {
|
|
|
232 |
$violationXml->setAttribute('method', $method);
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
if (!empty($function)) {
|
|
|
236 |
$violationXml->setAttribute('function', $function);
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
protected function processFunctionOrMethod(DOMElement $element, $metrics, $package = '')
|
|
|
241 |
{
|
|
|
242 |
$scope = '';
|
|
|
243 |
|
|
|
244 |
if ($metrics->getFunction() instanceof ReflectionMethod) {
|
|
|
245 |
$scope = $metrics->getFunction()->getDeclaringClass()->getName();
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
$startLine = $metrics->getFunction()->getStartLine();
|
|
|
249 |
$endLine = $metrics->getFunction()->getEndLine();
|
|
|
250 |
$name = $metrics->getFunction()->getName();
|
|
|
251 |
|
|
|
252 |
foreach ($this->rules['function'] as $ruleName => $rule) {
|
|
|
253 |
$result = $rule->apply($metrics);
|
|
|
254 |
|
|
|
255 |
if ($result !== NULL) {
|
|
|
256 |
$this->addViolation(
|
|
|
257 |
$result,
|
|
|
258 |
$element,
|
|
|
259 |
$rule,
|
|
|
260 |
$startLine,
|
|
|
261 |
$endLine,
|
|
|
262 |
$package,
|
|
|
263 |
$scope,
|
|
|
264 |
$name
|
|
|
265 |
);
|
|
|
266 |
|
|
|
267 |
$this->added = TRUE;
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
protected function loadClasses(array $configuration)
|
|
|
273 |
{
|
|
|
274 |
$basedir = dirname(__FILE__) . DIRECTORY_SEPARATOR .
|
|
|
275 |
'PMD' . DIRECTORY_SEPARATOR . 'Rule';
|
|
|
276 |
|
|
|
277 |
$dirs = array(
|
|
|
278 |
$basedir . DIRECTORY_SEPARATOR . 'Class',
|
|
|
279 |
$basedir . DIRECTORY_SEPARATOR . 'File',
|
|
|
280 |
$basedir . DIRECTORY_SEPARATOR . 'Function',
|
|
|
281 |
$basedir . DIRECTORY_SEPARATOR . 'Project'
|
|
|
282 |
);
|
|
|
283 |
|
|
|
284 |
foreach ($dirs as $dir) {
|
|
|
285 |
if (file_exists($dir)) {
|
|
|
286 |
$iterator = new PHPUnit_Util_FilterIterator(
|
|
|
287 |
new RecursiveIteratorIterator(
|
|
|
288 |
new RecursiveDirectoryIterator($dir)
|
|
|
289 |
),
|
|
|
290 |
'.php'
|
|
|
291 |
);
|
|
|
292 |
|
|
|
293 |
foreach ($iterator as $file) {
|
|
|
294 |
include_once $file->getPathname();
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
$classes = get_declared_classes();
|
|
|
300 |
|
|
|
301 |
foreach ($classes as $className) {
|
|
|
302 |
$class = new ReflectionClass($className);
|
|
|
303 |
|
|
|
304 |
if (!$class->isAbstract() && $class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule')) {
|
|
|
305 |
$rule = explode('_', $className);
|
|
|
306 |
$rule = $rule[count($rule)-1];
|
|
|
307 |
|
|
|
308 |
if (isset($configuration[$className])) {
|
|
|
309 |
$object = new $className(
|
|
|
310 |
$configuration[$className]['threshold'],
|
|
|
311 |
$configuration[$className]['priority']
|
|
|
312 |
);
|
|
|
313 |
} else {
|
|
|
314 |
$object = new $className;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_Project')) {
|
|
|
318 |
$this->rules['project'][$rule] = $object;
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_File')) {
|
|
|
322 |
$this->rules['file'][$rule] = $object;
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
else if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_Class')) {
|
|
|
326 |
$this->rules['class'][$rule] = $object;
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
else if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_Function')) {
|
|
|
330 |
$this->rules['function'][$rule] = $object;
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
}
|
|
|
336 |
?>
|