Xml   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 104
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B terminate() 0 24 2
B injectsBounds() 0 34 2
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\Summary;
11
use Hal\Application\Formater\FormaterInterface;
12
use Hal\Application\Rule\Validator;
13
use Hal\Component\Bounds\BoundsInterface;
14
use Hal\Component\Bounds\Result\ResultInterface;
15
use Hal\Component\Result\ResultCollection;
16
17
18
/**
19
 * Formater for xml export
20
 *
21
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
22
 */
23
class Xml implements FormaterInterface {
24
25
    /**
26
     * Bounds
27
     *
28
     * @var BoundsInterface
29
     */
30
    private $bound;
31
32
    /**
33
     * Validator
34
     *
35
     * @var Validator
36
     */
37
    private $validator;
38
39
    /**
40
     * Constructor
41
     *
42
     * @param Validator $validator
43
     * @param BoundsInterface $bound
44
     */
45
    public function __construct(Validator $validator, BoundsInterface $bound)
46
    {
47
        $this->bound = $bound;
48
        $this->validator = $validator;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults){
55
56
        $bounds = $this->bound->calculate($collection);
57
58
        // root
59
        $xml = new \DOMDocument("1.0", "UTF-8");
60
        $xml->formatOutput = true;
61
        $root = $xml->createElement("project");
62
        $this->injectsBounds($root, $bounds);
63
64
        // modules
65
        $modules = $xml->createElement('modules');
66
        foreach($groupedResults as $result) {
67
            $module = $xml->createElement('module');
68
            $this->injectsBounds($module, $result->getBounds());
69
            $module->setAttribute('namespace', $result->getName());
70
            $modules->appendChild($module);
71
        }
72
73
        $xml->appendChild($root);
74
        $root->appendChild($modules);
75
76
        return $xml->saveXML();
77
    }
78
79
    /**
80
     * Injects bound in node
81
     *
82
     * @param \DOMElement $node
83
     * @param ResultInterface $bound
84
     */
85
    private function injectsBounds(\DOMElement $node, ResultInterface $bound) {
86
        $node->setAttribute('loc', $bound->getSum('loc'));
87
        $node->setAttribute('lloc', $bound->getSum('logicalLoc'));
88
        $node->setAttribute('cyclomaticComplexity', $bound->getAverage('cyclomaticComplexity'));
89
        $node->setAttribute('maintainabilityIndex', $bound->getAverage('maintainabilityIndex'));
90
        $node->setAttribute('volume', $bound->getAverage('volume'));
91
        $node->setAttribute('vocabulary', $bound->getAverage('vocabulary'));
92
        $node->setAttribute('difficulty', $bound->getAverage('difficulty'));
93
        $node->setAttribute('effort', $bound->getAverage('effort'));
94
        $node->setAttribute('bugs', $bound->getAverage('bugs'));
95
        $node->setAttribute('time', $bound->getAverage('time'));
96
        $node->setAttribute('intelligentContent', $bound->getAverage('intelligentContent'));
97
        $node->setAttribute('commentWeight', $bound->getAverage('commentWeight'));
98
        $node->setAttribute('length', $bound->getAverage('length'));
99
100
        $hasOOP = null !== $bound->getSum('instability');
101
        if($hasOOP) {
102
            $node->setAttribute('lcom', $bound->getAverage('lcom'));
103
            $node->setAttribute('instability', $bound->getAverage('instability'));
104
            $node->setAttribute('efferentCoupling', $bound->getAverage('efferentCoupling'));
105
            $node->setAttribute('afferentCoupling', $bound->getAverage('afferentCoupling'));
106
            $node->setAttribute('sysc', $bound->getAverage('sysc'));
107
            $node->setAttribute('rsysc', $bound->getAverage('rsysc'));
108
            $node->setAttribute('dc', $bound->getAverage('dc'));
109
            $node->setAttribute('rdc', $bound->getAverage('rdc'));
110
            $node->setAttribute('sc', $bound->getAverage('sc'));
111
            $node->setAttribute('rsc', $bound->getAverage('rsc'));
112
            $node->setAttribute('noc', $bound->getSum('noc'));
113
            $node->setAttribute('noca', $bound->getSum('noca'));
114
            $node->setAttribute('nocc', $bound->getSum('nocc'));
115
            $node->setAttribute('noi', $bound->getSum('noi'));
116
            $node->setAttribute('nom', $bound->getSum('nom'));
117
        }
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    public function getName() {
124
        return 'Summary XML';
125
    }
126
}