Completed
Push — master ( 3d4627...d7599a )
by personal
02:55 queued 42s
created

Consolidated   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 171
Duplicated Lines 2.92 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 171
rs 10
wmc 18
lcom 0
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 5 99 13
A getAvg() 0 4 1
A getSum() 0 4 1
A getClasses() 0 4 1
A getFiles() 0 4 1
A getProject() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
            $classItem = get_class($item);
47
            if (ClassMetric::class === $classItem) {
48
                $classes[] = $item->all();
49
            } elseif (InterfaceMetric::class === $classItem) {
50
                $nbInterfaces++;
51
            } elseif (FunctionMetric::class === $classItem) {
52
                $functions[$key] = $item->all();
53
            } elseif (FileMetric::class === $classItem) {
54
                $files[$key] = $item->all();
55
            } elseif (ProjectMetric::class === $classItem) {
56
                $project[$key] = $item->all();
57
            }
58
        }
59
60
        // sums
61
        $sum = (object)[
62
            'loc' => 0,
63
            'cloc' => 0,
64
            'lloc' => 0,
65
            'nbMethods' => 0,
66
        ];
67
        $avg = (object)[
68
            'ccn' => [],
69
            'bugs' => [],
70
            'kanDefect' => [],
71
            'relativeSystemComplexity' => [],
72
            'relativeDataComplexity' => [],
73
            'relativeStructuralComplexity' => [],
74
            'volume' => [],
75
            'commentWeight' => [],
76
            'intelligentContent' => [],
77
            'lcom' => [],
78
            'instability' => [],
79
            'afferentCoupling' => [],
80
            'efferentCoupling' => [],
81
            'difficulty' => [],
82
            'mi' => [],
83
        ];
84
85
        foreach ($metrics->all() as $key => $item) {
86
            $sum->loc += $item->get('loc');
87
            $sum->lloc += $item->get('lloc');
88
            $sum->cloc += $item->get('cloc');
89
            $sum->nbMethods += $item->get('nbMethods');
90
91
            foreach ($avg as $k => $a) {
92
                array_push($avg->$k, $item->get($k));
93
            }
94
        }
95
        $sum->nbClasses = count($classes);
96
        $sum->nbInterfaces = $nbInterfaces;
97
98
        foreach ($avg as &$a) {
99 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...
100
                $a = round(array_sum($a) / sizeof($a), 2);
101
            } else {
102
                $a = 0;
103
            }
104
        }
105
106
        // sums of violations
107
        $violations = [
108
            'total' => 0,
109
            'information' => 0,
110
            'warning' => 0,
111
            'error' => 0,
112
            'critical' => 0,
113
        ];
114
        $map = [
115
            Violation::INFO => 'information',
116
            Violation::WARNING => 'warning',
117
            Violation::ERROR => 'error',
118
            Violation::CRITICAL => 'critical',
119
        ];
120
        foreach ($classes as $class) {
121
            foreach ($class['violations'] as $violation) {
122
                $violations['total']++;
123
                $name = $map[$violation->getLevel()];
124
                $violations[$name]++;
125
            }
126
        }
127
        $sum->violations = (object)$violations;
128
129
130
        $this->avg = $avg;
131
        $this->sum = $sum;
132
        $this->classes = $classes;
133
        $this->files = $files;
134
        $this->project = $project;
135
    }
136
137
    /**
138
     * @return object
139
     */
140
    public function getAvg()
141
    {
142
        return $this->avg;
143
    }
144
145
    /**
146
     * @return object
147
     */
148
    public function getSum()
149
    {
150
        return $this->sum;
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function getClasses()
157
    {
158
        return $this->classes;
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function getFiles()
165
    {
166
        return $this->files;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getProject()
173
    {
174
        return $this->project;
175
    }
176
}
177