Completed
Push — master ( bdaaad...4f4372 )
by personal
04:35 queued 45s
created

Reporter::generate()   C

Complexity

Conditions 9
Paths 25

Size

Total Lines 92
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 60
nc 25
nop 1
dl 0
loc 92
rs 5.1048
c 0
b 0
f 0

How to fix   Long Method   

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\Report\Cli;
3
4
use Hal\Application\Config\Config;
5
use Hal\Metric\Consolidated;
6
use Hal\Metric\Metrics;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class Reporter
10
{
11
12
    /**
13
     * @var Config
14
     */
15
    private $config;
16
17
    /**
18
     * @var OutputInterface
19
     */
20
    private $output;
21
22
    /**
23
     * Reporter constructor.
24
     * @param Config $config
25
     * @param OutputInterface $output
26
     */
27
    public function __construct(Config $config, OutputInterface $output)
28
    {
29
        $this->config = $config;
30
        $this->output = $output;
31
    }
32
33
34
    public function generate(Metrics $metrics)
35
    {
36
        if ($this->config->has('quiet')) {
37
            return;
38
        }
39
40
        // grouping results
41
        $consolidated = new Consolidated($metrics);
42
        $sum = $consolidated->getSum();
43
        $avg = $consolidated->getAvg();
44
45
46
        $methodsByClass = $locByClass = $locByMethod = 0;
47
        if ($sum->nbClasses > 0) {
48
            $methodsByClass = round($sum->nbMethods / $sum->nbClasses, 2);
49
            $locByClass = round($sum->lloc / $sum->nbClasses);
50
        }
51
        if ($sum->nbMethods > 0) {
52
            $locByMethod = round($sum->lloc / $sum->nbMethods);
53
        }
54
55
        $out = <<<EOT
56
LOC
57
    Lines of code                               {$sum->loc}
58
    Logical lines of code                       {$sum->lloc}
59
    Comment lines of code                       {$sum->cloc}
60
    Average volume                              {$avg->volume}
61
    Average comment weight                      {$avg->commentWeight}
62
    Average intelligent content                 {$avg->commentWeight}
63
    Logical lines of code by class              {$locByClass}
64
    Logical lines of code by method             {$locByMethod}
65
66
Object oriented programming
67
    Classes                                     {$sum->nbClasses}
68
    Interface                                   {$sum->nbInterfaces}
69
    Methods                                     {$sum->nbMethods}
70
    Methods by class                            {$methodsByClass}
71
    Lack of cohesion of methods                 {$avg->lcom}
72
    Average afferent coupling                   {$avg->afferentCoupling}
73
    Average efferent coupling                   {$avg->efferentCoupling}
74
    Average instability                         {$avg->instability}
75
76
Complexity
77
    Average Cyclomatic complexity by class      {$avg->ccn}
78
    Average Relative system complexity          {$avg->relativeSystemComplexity}
79
    Average Difficulty                          {$avg->difficulty}
80
    
81
Bugs
82
    Average bugs by class                       {$avg->bugs}
83
    Average defects by class (Kan)              {$avg->kanDefect}
84
85
Violations
86
    Critical                                    {$sum->violations->critical}
87
    Error                                       {$sum->violations->error}
88
    Warning                                     {$sum->violations->warning}
89
    Information                                 {$sum->violations->information}
90
91
EOT;
92
93
        // git
94
        if ($this->config->has('git')) {
95
            $commits = [];
96
            foreach ($consolidated->getFiles() as $name => $file) {
97
                $commits[$name] = $file['gitChanges'];
98
            }
99
            arsort($commits);
100
            $commits = array_slice($commits, 0, 10);
101
102
            $out .= "\nTop 10 commited files";
103
            foreach ($commits as $file => $nb) {
104
                $out .= sprintf("\n    %d    %s", $nb, $file);
105
            }
106
            if (0 === sizeof($commits)) {
107
                $out .= "\n    NA";
108
            }
109
        }
110
111
        // Junit
112
        if ($this->config->has('junit')) {
113
            $out .= <<<EOT
114
            
115
Unit testing
116
    Number of unit tests                        {$metrics->get('unitTesting')->get('nbTests')}
0 ignored issues
show
Bug introduced by
The method get cannot be called on $metrics->get('unitTesting') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
117
    Classes called by tests                     {$metrics->get('unitTesting')->get('nbCoveredClasses')}
0 ignored issues
show
Bug introduced by
The method get cannot be called on $metrics->get('unitTesting') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
118
    Classes called by tests (percent)           {$metrics->get('unitTesting')->get('percentCoveredClasses')} %
0 ignored issues
show
Bug introduced by
The method get cannot be called on $metrics->get('unitTesting') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
119
EOT;
120
        }
121
122
        $out .= "\n\n";
123
        $this->output->write($out);
124
125
    }
126
127
}
128