Completed
Push — master ( bdaaad...4f4372 )
by personal
06:57 queued 04:36
created

Consolidated::getFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Hal\Metric;
3
4
use Hal\Violation\Violation;
5
6
class Consolidated
7
{
8
    /**
9
     * @var object
10
     */
11
    private $avg;
12
13
    /**
14
     * @var object
15
     */
16
    private $sum;
17
18
    /**
19
     * @var array
20
     */
21
    private $classes = [];
22
23
    /**
24
     * @var array
25
     */
26
    private $files = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $project = [];
32
33
    /**
34
     * Consolided constructor.
35
     * @param Metrics $metrics
36
     */
37
    public function __construct(Metrics $metrics)
38
    {
39
        // grouping results
40
        $classes = [];
41
        $functions = [];
42
        $files = [];
43
        $project = [];
44
        $nbInterfaces = 0;
45
        foreach ($metrics->all() as $key => $item) {
46
            if ($item instanceof ClassMetric) {
47
                $classes[] = $item->all();;
48
            }
49
            if ($item instanceof InterfaceMetric) {
50
                $nbInterfaces++;
51
            }
52
            if ($item instanceof FunctionMetric) {
53
                $functions[$key] = $item->all();;
54
            }
55
            if ($item instanceof FileMetric) {
56
                $files[$key] = $item->all();;
57
            }
58
            if ($item instanceof ProjectMetric) {
59
                $project[$key] = $item->all();;
60
            }
61
        }
62
63
        // sums
64
        $sum = (object)[
65
            'loc' => 0,
66
            'cloc' => 0,
67
            'lloc' => 0,
68
            'nbMethods' => 0,
69
        ];
70
        $avg = (object)[
71
            'ccn' => [],
72
            'bugs' => [],
73
            'kanDefect' => [],
74
            'relativeSystemComplexity' => [],
75
            'relativeDataComplexity' => [],
76
            'relativeStructuralComplexity' => [],
77
            'volume' => [],
78
            'commentWeight' => [],
79
            'intelligentContent' => [],
80
            'lcom' => [],
81
            'instability' => [],
82
            'afferentCoupling' => [],
83
            'efferentCoupling' => [],
84
            'difficulty' => [],
85
            'lcom' => [],
86
            'mi' => [],
87
        ];
88
89
        foreach ($metrics->all() as $key => $item) {
90
            $sum->loc += $item->get('loc');
91
            $sum->lloc += $item->get('lloc');
92
            $sum->cloc += $item->get('cloc');
93
            $sum->nbMethods += $item->get('nbMethods');
94
95
            foreach ($avg as $k => $a) {
96
                array_push($avg->$k, $item->get($k));
97
            }
98
        }
99
        $sum->nbClasses = sizeof($classes) - $nbInterfaces;
100
        $sum->nbInterfaces = $nbInterfaces;
101
102
        foreach ($avg as &$a) {
103 View Code Duplication
            if (sizeof($a) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
                $a = round(array_sum($a) / sizeof($a), 2);
105
            } else {
106
                $a = 0;
107
            }
108
        }
109
110
        // sums of violations
111
        $violations = [
112
            'total' => 0,
113
            'information' => 0,
114
            'warning' => 0,
115
            'error' => 0,
116
            'critical' => 0,
117
        ];
118
        $map = [
119
            Violation::INFO => 'information',
120
            Violation::WARNING => 'warning',
121
            Violation::ERROR => 'error',
122
            Violation::CRITICAL => 'critical',
123
        ];
124
        foreach ($classes as $class) {
125
            foreach ($class['violations'] as $violation) {
126
                $violations['total']++;
127
                $name = $map[$violation->getLevel()];
128
                $violations[$name]++;
129
            }
130
        }
131
        $sum->violations = (object)$violations;
132
133
134
        $this->avg = $avg;
135
        $this->sum = $sum;
136
        $this->classes = $classes;
137
        $this->files = $files;
138
        $this->project = $project;
139
    }
140
141
    /**
142
     * @return object
143
     */
144
    public function getAvg()
145
    {
146
        return $this->avg;
147
    }
148
149
    /**
150
     * @return object
151
     */
152
    public function getSum()
153
    {
154
        return $this->sum;
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function getClasses()
161
    {
162
        return $this->classes;
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    public function getFiles()
169
    {
170
        return $this->files;
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    public function getProject()
177
    {
178
        return $this->project;
179
    }
180
}
181