5
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
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
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.
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.
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
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';
58
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
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
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.14
71
* @link http://www.phpunit.de/
72
* @since Class available since Release 3.2.0
74
class PHPUnit_Util_Log_PMD extends PHPUnit_Util_Printer
78
protected $rules = array(
89
* @param array $configuration
90
* @throws InvalidArgumentException
92
public function __construct($out = NULL, array $configuration = array())
94
parent::__construct($out);
95
$this->loadClasses($configuration);
99
* @param PHPUnit_Framework_TestResult $result
101
public function process(PHPUnit_Framework_TestResult $result)
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);
108
$document = new DOMDocument('1.0', 'UTF-8');
109
$document->formatOutput = TRUE;
111
$pmd = $document->createElement('pmd');
112
$pmd->setAttribute('version', 'PHPUnit ' . PHPUnit_Runner_Version::id());
113
$document->appendChild($pmd);
115
foreach ($this->rules['project'] as $ruleName => $rule) {
116
$result = $rule->apply($metrics);
118
if ($result !== NULL) {
127
foreach ($metrics->getFiles() as $fileName => $fileMetrics) {
128
$xmlFile = $document->createElement('file');
129
$xmlFile->setAttribute('name', $fileName);
131
$this->added = FALSE;
133
foreach ($this->rules['file'] as $ruleName => $rule) {
134
$result = $rule->apply($fileMetrics);
136
if ($result !== NULL) {
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();
153
foreach ($this->rules['class'] as $ruleName => $rule) {
154
$result = $rule->apply($classMetrics);
156
if ($result !== NULL) {
171
foreach ($classMetrics->getMethods() as $methodName => $methodMetrics) {
172
if (!$methodMetrics->getMethod()->isAbstract()) {
173
$this->processFunctionOrMethod($xmlFile, $methodMetrics, $classPackage);
179
foreach ($fileMetrics->getFunctions() as $functionName => $functionMetrics) {
180
$this->processFunctionOrMethod($xmlFile, $functionMetrics);
184
$pmd->appendChild($xmlFile);
188
$this->write($document->saveXML());
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
202
protected function addViolation($violation, DOMElement $element, PHPUnit_Util_Log_PMD_Rule $rule, $line = '', $toLine = '', $package = '', $class = '', $method = '', $function = '')
204
$violationXml = $element->appendChild(
205
$element->ownerDocument->createElement('violation', $violation)
208
$violationXml->setAttribute('rule', $rule->getName());
209
$violationXml->setAttribute('priority', $rule->getPriority());
212
$violationXml->setAttribute('line', $line);
215
if (!empty($toLine)) {
216
$violationXml->setAttribute('to-line', $toLine);
219
if (empty($package)) {
223
if (!empty($package)) {
224
$violationXml->setAttribute('package', $package);
227
if (!empty($class)) {
228
$violationXml->setAttribute('class', $class);
231
if (!empty($method)) {
232
$violationXml->setAttribute('method', $method);
235
if (!empty($function)) {
236
$violationXml->setAttribute('function', $function);
240
protected function processFunctionOrMethod(DOMElement $element, $metrics, $package = '')
244
if ($metrics->getFunction() instanceof ReflectionMethod) {
245
$scope = $metrics->getFunction()->getDeclaringClass()->getName();
248
$startLine = $metrics->getFunction()->getStartLine();
249
$endLine = $metrics->getFunction()->getEndLine();
250
$name = $metrics->getFunction()->getName();
252
foreach ($this->rules['function'] as $ruleName => $rule) {
253
$result = $rule->apply($metrics);
255
if ($result !== NULL) {
272
protected function loadClasses(array $configuration)
274
$basedir = dirname(__FILE__) . DIRECTORY_SEPARATOR .
275
'PMD' . DIRECTORY_SEPARATOR . 'Rule';
278
$basedir . DIRECTORY_SEPARATOR . 'Class',
279
$basedir . DIRECTORY_SEPARATOR . 'File',
280
$basedir . DIRECTORY_SEPARATOR . 'Function',
281
$basedir . DIRECTORY_SEPARATOR . 'Project'
284
foreach ($dirs as $dir) {
285
if (file_exists($dir)) {
286
$iterator = new PHPUnit_Util_FilterIterator(
287
new RecursiveIteratorIterator(
288
new RecursiveDirectoryIterator($dir)
293
foreach ($iterator as $file) {
294
include_once $file->getPathname();
299
$classes = get_declared_classes();
301
foreach ($classes as $className) {
302
$class = new ReflectionClass($className);
304
if (!$class->isAbstract() && $class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule')) {
305
$rule = explode('_', $className);
306
$rule = $rule[count($rule)-1];
308
if (isset($configuration[$className])) {
309
$object = new $className(
310
$configuration[$className]['threshold'],
311
$configuration[$className]['priority']
314
$object = new $className;
317
if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_Project')) {
318
$this->rules['project'][$rule] = $object;
321
if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_File')) {
322
$this->rules['file'][$rule] = $object;
325
else if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_Class')) {
326
$this->rules['class'][$rule] = $object;
329
else if ($class->isSubclassOf('PHPUnit_Util_Log_PMD_Rule_Function')) {
330
$this->rules['function'][$rule] = $object;
b'\\ No newline at end of file'