Completed
Push — master ( a07752...066857 )
by Matthias
03:28
created

Converter::convertMethods()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 3
eloc 17
nc 3
nop 1
1
<?php
2
3
namespace Cauditor;
4
5
/**
6
 * @author Matthias Mullie <[email protected]>
7
 * @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved.
8
 * @license LICENSE MIT
9
 */
10
class Converter
11
{
12
    /**
13
     * Project-wide totals.
14
     *
15
     * @var array
16
     */
17
    protected $totals = array();
18
19
    /**
20
     * Processes XML node by node, extracts the relevant metrics & writes them
21
     * piecemeal to a JSON file.
22
     *
23
     * @param XMLReader $reader XML document to read from.
24
     *
25
     * @return string
26
     *
27
     * @throws Exception
28
     */
29
    public function convert(XMLReader $reader)
30
    {
31
        $reader->readNext('metrics');
32
33
        $this->totals = array();
34
        $packages = $this->convertPackages($reader);
35
36
        $data = array(
37
                'loc' => $reader->getAttribute('eloc'),
38
                'noc' => $reader->getAttribute('noc'),
39
                'nom' => $reader->getAttribute('nom'),
40
            ) + $this->totals;
41
42
        $json = json_encode($data);
43
        // data will be stored as json, but we need to inject children, so
44
        // don't write the closing `]` & `}` yet
45
        return substr($json, 0, -1).',"children":['.$packages.']}';
46
    }
47
48
    protected function convertPackages(XMLReader $reader)
49
    {
50
        $i = 0;
51
        $content = '';
52
        while ($reader->readNext('package', 'metrics')) {
53
            $data = array('name' => $reader->getAttribute('name'));
54
55
            $this->addToTotals($data);
56
57
            $json = json_encode($data);
58
            // add `,` between multiple nodes
59
            $json = $i++ === 0 ? $json : ','.$json;
60
            // data will be stored as json, but we need to fetch children, so
61
            // don't write the closing `]` & `}` yet
62
            $content .= substr($json, 0, -1).',"children":['.$this->convertClasses($reader).']}';
63
        }
64
65
        return $content;
66
    }
67
68
    protected function convertClasses(XMLReader $reader)
69
    {
70
        $i = 0;
71
        $content = '';
72
        while ($reader->readNext('class', 'package')) {
73
            $data = array(
74
                'name' => $reader->getAttribute('name'),
75
                'loc' => (int) $reader->getAttribute('eloc'),
76
                'ca' => (int) $reader->getAttribute('ca'),
77
                'ce' => (int) $reader->getAttribute('ce'),
78
                'i' => (float) number_format((int) $reader->getAttribute('ce') / (((int) $reader->getAttribute('ce') + (int) $reader->getAttribute('ca')) ?: 1), 2),
79
                'dit' => (int) $reader->getAttribute('dit'),
80
            );
81
82
            $this->addToTotals($data);
83
84
            $json = json_encode($data);
85
            // add `,` between multiple nodes
86
            $json = $i++ === 0 ? $json : ','.$json;
87
            // data will be stored as json, but we need to fetch children, so
88
            // don't write the closing `]` & `}` yet
89
            $content .= substr($json, 0, -1).',"children":['.$this->convertMethods($reader).']}';
90
        }
91
92
        return $content;
93
    }
94
95
    protected function convertMethods(XMLReader $reader)
96
    {
97
        $i = 0;
98
        $content = '';
99
        while ($reader->readNext('method', 'class')) {
100
            $data = array(
101
                'name' => $reader->getAttribute('name'),
102
                'loc' => (int) $reader->getAttribute('eloc'),
103
                'ccn' => (int) $reader->getAttribute('ccn2'),
104
                'npath' => (int) $reader->getAttribute('npath'),
105
                'he' => (float) number_format($reader->getAttribute('he'), 2),
106
                'hi' => (float) number_format($reader->getAttribute('hi'), 2),
107
                'mi' => (float) number_format($reader->getAttribute('mi'), 2),
108
            );
109
110
            $this->addToTotals($data);
111
112
            $json = json_encode($data);
113
            // add `,` between multiple nodes
114
            $json = $i++ === 0 ? $json : ','.$json;
115
116
            $content .= $json;
117
        }
118
119
        return $content;
120
    }
121
122
    protected function addToTotals(array $data)
123
    {
124
        // don't need these, obviously...
125
        unset($data['name'], $data['loc']);
126
127
        foreach ($data as $metric => $value) {
128
            if (!isset($this->totals[$metric])) {
129
                $this->totals[$metric] = 0;
130
            }
131
132
            $this->totals[$metric] += $value;
133
        }
134
    }
135
}
136