Cli   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 167
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B terminate() 0 78 4
A getRow() 0 5 3
A getStyle() 0 13 4
A formatTime() 0 7 1
A getName() 0 3 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Formater\Details;
11
use Hal\Application\Formater\FormaterInterface;
12
use Hal\Application\Rule\Validator;
13
use Hal\Component\Bounds\BoundsInterface;
14
use Hal\Component\Bounds\Result\ResultInterface;
15
use Hal\Component\Result\ResultCollection;
16
use Symfony\Component\Console\Helper\TableHelper;
17
use Symfony\Component\Console\Output\BufferedOutput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
21
/**
22
 * Formater for cli usage
23
 *
24
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
25
 */
26
class Cli implements FormaterInterface {
27
28
    /**
29
     * Validator
30
     *
31
     * @var \Hal\Application\Rule\Validator
32
     */
33
    private $validator;
34
35
    /**
36
     * Bounds
37
     *
38
     * @var BoundsInterface
39
     */
40
    private $bound;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param Validator $validator
46
     * @param BoundsInterface $bound
47
     */
48
    public function __construct(Validator $validator, BoundsInterface $bound)
49
    {
50
        $this->bound = $bound;
51
        $this->validator = $validator;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults){
58
59
        $output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true);
60
//        $output->write(str_pad("\x0D", 80, "\x20"));
61
//        $output->writeln('');
62
63
64
        // overview
65
        $total = $this->bound->calculate($collection);
66
        $output->writeln(sprintf(
67
            '<info>%d</info> files have been analyzed. Read and understand these <info>%s</info> lines of code will take around <info>%s</info>.'
68
            , sizeof($collection, COUNT_NORMAL)
69
            , $total->getSum('loc')
70
            , $this->formatTime($total->getSum('time'))
71
        ));
72
73
74
        $output->writeln('<info>Average for each module:</info>');
75
        $output->writeln('');
76
77
        $hasOOP = null !== $total->getSum('instability');
78
79
        $table = new TableHelper();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Console\Helper\TableHelper has been deprecated with message: since version 2.5, to be removed in 3.0 Use {@link Table} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
80
        $table
81
            ->setHeaders(array_merge(
82
                array(
83
                    'Name'
84
                    , 'Complexity'
85
                    , 'Myer Distance'
86
                    , 'Maintainability'
87
                    , 'LLOC'
88
                    , 'Comment weight'
89
                    , 'Vocabulary'
90
                    , 'Volume'
91
                    , 'Bugs'
92
                    , 'Difficulty'
93
                )
94
                , ($hasOOP ? array(
95
                    'lcom'
96
                    , 'SysComplexity'
97
                    , 'Instability'
98
                    , 'Abstractness'
99
                    , 'ce'
100
                    , 'ca'
101
                    ) : array())
102
103
            ))
104
            ->setLayout(TableHelper::LAYOUT_DEFAULT);
105
106
        foreach($groupedResults as $result) {
107
            $table->addRow(array_merge(
108
                array(
109
                    str_repeat('  ', $result->getDepth()).$result->getName()
110
                    , $this->getRow($result->getBounds(), 'cyclomaticComplexity', 'average', 0)
111
                    , $this->getRow($result->getBounds(), 'myerDistance', 'average', 0)
112
                    , $this->getRow($result->getBounds(), 'maintainabilityIndex', 'average', 0)
113
                    , $this->getRow($result->getBounds(), 'logicalLoc', 'sum', 0)
114
                    , $this->getRow($result->getBounds(), 'commentWeight', 'average', 0)
115
                    , $this->getRow($result->getBounds(), 'vocabulary', 'average', 0)
116
                    , $this->getRow($result->getBounds(), 'volume', 'average', 0)
117
                    , $this->getRow($result->getBounds(), 'bugs', 'sum', 2)
118
                    , $this->getRow($result->getBounds(), 'difficulty', 'average', 0)
119
                )
120
                , ($hasOOP ? array(
121
                    $this->getRow($result->getBounds(), 'lcom', 'average', 2)
122
                    , $this->getRow($result->getBounds(), 'rsysc', 'average', 2)
123
                    , $result->getInstability()->getInstability()
124
                    , $result->getAbstractness()->getAbstractness()
125
                    , $this->getRow($result->getBounds(), 'efferentCoupling', 'average', 2)
126
                    , $this->getRow($result->getBounds(), 'afferentCoupling', 'average', 2)
127
                    ) : array())
128
                )
129
            );
130
        }
131
        $table->render($output);
132
133
        return $output->fetch();
134
    }
135
136
    /**
137
     * Get formated row
138
     *
139
     * @param ResultInterface $bound
140
     * @param string $key
141
     * @param string $type
142
     * @param integer $round
143
     * @return string
144
     */
145
    private function getRow(ResultInterface $bound, $key, $type, $round) {
146
        $value = $bound->get($type, $key);
147
        $value = !is_null($value) ? round($bound->get($type, $key), $round) : '?';
148
        return sprintf('<%1$s>%2$s</%1$s>', $this->getStyle(($type == 'average' ? $key : null), $value), $value);
149
    }
150
151
    /**
152
     * Get style, according score
153
     *
154
     * @param string $key
155
     * @param double $value
156
     * @return string
157
     */
158
    private function getStyle($key, $value) {
159
        $score = $this->validator->validate($key, $value);
160
161
        switch($score) {
162
            case Validator::GOOD:
163
                return 'fg=green';
164
            case Validator::WARNING:
165
                return 'bg=yellow;fg=black';
166
            case Validator::CRITICAL:
167
                return 'bg=red;fg=white';
168
        }
169
        return 'fg=white';
170
    }
171
172
    /**
173
     * Format time in text
174
     *
175
     * @param null|double $v
176
     * @return string
177
     */
178
    private function formatTime($v) {
179
        return sprintf('%s hour(s), %s minute(s) and %s second(s)'
180
            , gmdate('H', $v)
181
            , gmdate('m', $v)
182
            , gmdate('s', $v)
183
        );
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189
    public function getName() {
190
        return 'Detailled CLI';
191
    }
192
}