Completed
Push — master ( e1173d...cdf0e4 )
by Oskar
05:16 queued 03:02
created

Consolidated::__construct()   F

Complexity

Conditions 17
Paths 1296

Size

Total Lines 137
Code Lines 107

Duplication

Lines 17
Ratio 12.41 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 107
c 1
b 0
f 0
nc 1296
nop 1
dl 17
loc 137
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /** @var array */
34
    private $packages;
35
36
    /**
37
     * Consolided constructor.
38
     * @param Metrics $metrics
39
     */
40
    public function __construct(Metrics $metrics)
41
    {
42
        $classMetrics = [];
43
44
        // grouping results
45
        $classes = [];
46
        $functions = [];
47
        $files = [];
48
        $packages = [];
49
        $project = [];
50
        $nbInterfaces = 0;
51
        foreach ($metrics->all() as $key => $item) {
52
            $classItem = get_class($item);
53
            if (ClassMetric::class === $classItem) {
54
                $classes[] = $item->all();
55
                $classMetrics[] = $item;
56
            } elseif (InterfaceMetric::class === $classItem) {
57
                $nbInterfaces++;
58
            } elseif (FunctionMetric::class === $classItem) {
59
                $functions[$key] = $item->all();
60
            } elseif (FileMetric::class === $classItem) {
61
                $files[$key] = $item->all();
62
            } elseif (ProjectMetric::class === $classItem) {
63
                $project[$key] = $item->all();
64
            } elseif (PackageMetric::class === $classItem) {
65
                $packages[$key] = $item->all();
66
            }
67
        }
68
69
        // sums
70
        $sum = (object)[
71
            'loc' => 0,
72
            'cloc' => 0,
73
            'lloc' => 0,
74
            'nbMethods' => 0,
75
        ];
76
        $avg = (object)[
77
            'ccn' => [],
78
            'bugs' => [],
79
            'kanDefect' => [],
80
            'relativeSystemComplexity' => [],
81
            'relativeDataComplexity' => [],
82
            'relativeStructuralComplexity' => [],
83
            'volume' => [],
84
            'commentWeight' => [],
85
            'intelligentContent' => [],
86
            'lcom' => [],
87
            'instability' => [],
88
            'afferentCoupling' => [],
89
            'efferentCoupling' => [],
90
            'difficulty' => [],
91
            'mi' => [],
92
        ];
93
94
        foreach ($classMetrics as $key => $item) {
95
            $sum->loc += $item->get('loc');
96
            $sum->lloc += $item->get('lloc');
97
            $sum->cloc += $item->get('cloc');
98
            $sum->nbMethods += $item->get('nbMethods');
99
100
            foreach ($avg as $k => $a) {
101
                array_push($avg->$k, $item->get($k));
102
            }
103
        }
104
        $sum->nbClasses = count($classes);
105
        $sum->nbInterfaces = $nbInterfaces;
106
        $sum->nbPackages = count($packages);
107
108
        foreach ($avg as &$a) {
109 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...
110
                $a = round(array_sum($a) / sizeof($a), 2);
111
            } else {
112
                $a = 0;
113
            }
114
        }
115
116
        $avg->distance = 0;
117
        $avg->incomingCDep = 0;
118
        $avg->incomingPDep = 0;
119
        $avg->outgoingCDep = 0;
120
        $avg->outgoingPDep = 0;
121
        $avg->classesPerPackage = 0;
122
        foreach (array_keys($packages) as $eachName) {
123
            /* @var $eachPackage PackageMetric */
124
            $eachPackage = $metrics->get($eachName);
125
            $avg->distance += $eachPackage->getDistance();
126
            $avg->incomingCDep += count($eachPackage->getIncomingClassDependencies());
127
            $avg->incomingPDep += count($eachPackage->getIncomingPackageDependencies());
128
            $avg->outgoingCDep += count($eachPackage->getOutgoingClassDependencies());
129
            $avg->outgoingPDep += count($eachPackage->getOutgoingPackageDependencies());
130
            $avg->classesPerPackage += count($eachPackage->getClasses());
131
        }
132
        $avg->distance = round($avg->distance / count($packages), 2);
133
        $avg->incomingCDep = round($avg->incomingCDep / count($packages), 2);
134
        $avg->incomingPDep = round($avg->incomingPDep / count($packages), 2);
135
        $avg->outgoingCDep = round($avg->outgoingCDep / count($packages), 2);
136
        $avg->outgoingPDep = round($avg->outgoingPDep / count($packages), 2);
137
        $avg->classesPerPackage = round($avg->classesPerPackage / count($packages), 2);
138
139
        // sums of violations
140
        $violations = [
141
            'total' => 0,
142
            'information' => 0,
143
            'warning' => 0,
144
            'error' => 0,
145
            'critical' => 0,
146
        ];
147
        $map = [
148
            Violation::INFO => 'information',
149
            Violation::WARNING => 'warning',
150
            Violation::ERROR => 'error',
151
            Violation::CRITICAL => 'critical',
152
        ];
153
        foreach ($classes as $class) {
154 View Code Duplication
            foreach ($class['violations'] as $violation) {
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...
155
                $violations['total']++;
156
                $name = $map[$violation->getLevel()];
157
                $violations[$name]++;
158
            }
159
        }
160 View Code Duplication
        foreach ($packages as $package) {
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...
161
            foreach ($package['violations'] as $violation) {
162
                $violations['total']++;
163
                $name = $map[$violation->getLevel()];
164
                $violations[$name]++;
165
            }
166
        }
167
        $sum->violations = (object)$violations;
168
169
170
        $this->avg = $avg;
171
        $this->sum = $sum;
172
        $this->classes = $classes;
173
        $this->files = $files;
174
        $this->project = $project;
175
        $this->packages = $packages;
176
    }
177
178
    /**
179
     * @return object
180
     */
181
    public function getAvg()
182
    {
183
        return $this->avg;
184
    }
185
186
    /**
187
     * @return object
188
     */
189
    public function getSum()
190
    {
191
        return $this->sum;
192
    }
193
194
    /**
195
     * @return array
196
     */
197
    public function getClasses()
198
    {
199
        return $this->classes;
200
    }
201
202
    /**
203
     * @return array
204
     */
205
    public function getFiles()
206
    {
207
        return $this->files;
208
    }
209
210
    /**
211
     * @return array
212
     */
213
    public function getProject()
214
    {
215
        return $this->project;
216
    }
217
218
    /**
219
     * @return array
220
     */
221
    public function getPackages()
222
    {
223
        return $this->packages;
224
    }
225
}
226