Xml   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C terminate() 0 47 7
A getName() 0 3 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Formater\Violations;
11
use Hal\Application\Formater\FormaterInterface;
12
use Hal\Application\Rule\Validator;
13
use Hal\Component\Bounds\BoundsInterface;
14
use Hal\Component\Result\ResultCollection;
15
16
17
/**
18
 * Formater for xml export
19
 *
20
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
21
 */
22
class Xml implements FormaterInterface {
23
24
    /**
25
     * Bounds
26
     *
27
     * @var BoundsInterface
28
     */
29
    private $bound;
30
31
    /**
32
     * Validator
33
     *
34
     * @var Validator
35
     */
36
    private $validator;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param Validator $validator
42
     * @param BoundsInterface $bound
43
     */
44
    public function __construct(Validator $validator, BoundsInterface $bound)
45
    {
46
        $this->bound = $bound;
47
        $this->validator = $validator;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults){
54
55
56
        // root
57
        $xml = new \DOMDocument("1.0", "UTF-8");
58
        $xml->formatOutput = true;
59
        $root = $xml->createElement("pmd");
60
        $root->setAttribute('version', '@package_version@');
61
        $root->setAttribute('timestamp', date('c'));
62
63
        // violations
64
        foreach($collection as $item) {
65
            $file = $xml->createElement('file');
66
            $file->setAttribute('name', realpath($item->getFilename()));
67
68
            $array = $item->asArray();
69
            $hasViolation = false;
70
            foreach($array as $key => $value) {
71
                $result = $this->validator->validate($key, $value);
72
                if(Validator::GOOD !== $result && Validator::UNKNOWN !== $result) {
73
                    $hasViolation = true;
74
                    $violation = $xml->createElement('violation');
75
                    $violation->setAttribute('beginline' , 1);
76
                    $violation->setAttribute('endline', $array['loc']);
77
                    $violation->setAttribute('rule', $key);
78
                    $violation->setAttribute('ruleset', $key);
79
                    $violation->setAttribute('externalInfoUrl', 'http://halleck45.github.io/PhpMetrics/documentation/index.html');
80
                    $violation->setAttribute('priority', $result == Validator::WARNING ? 0 : 1);
81
82
                    $violation->nodeValue = sprintf('the "%1$s" value (%2$s) of "%3$s" is incorrect. The configured %1$s threshold is %4$s'
83
                    , $key
84
                    , $value
85
                    , $item->getName()
86
                    , implode(', ', $this->validator->getRuleSet()->getRule($key))
87
                    );
88
                    $file->appendChild($violation);
89
                }
90
            }
91
92
            if($hasViolation) {
93
                $root->appendChild($file);
94
            }
95
        }
96
97
        $xml->appendChild($root);
98
        return $xml->saveXML();
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function getName() {
105
        return 'Violations XML';
106
    }
107
}