Completed
Push — master ( 86ed77...44594f )
by Insolita
02:18
created

DefaultController::actionSummary()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 12
nop 1
dl 0
loc 26
rs 9.3222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by solly [18.10.17 4:41]
4
 */
5
6
namespace insolita\codestat\controllers;
7
8
use function count;
9
use insolita\codestat\CodeStatModule;
10
use insolita\codestat\helpers\Output;
11
use League\CLImate\CLImate;
12
use yii\base\Module;
13
use yii\console\Controller;
14
use yii\console\ExitCode;
15
16
class DefaultController extends Controller
17
{
18
    public $defaultAction = 'summary';
19
    /**
20
     * @var CodeStatModule|Module
21
     **/
22
    public $module;
23
    
24
    public $color = true;
25
    
26
    protected $climate;
27
    
28
    public function __construct($id, Module $module, CLImate $CLImate, array $config = [])
29
    {
30
        $this->climate = $CLImate;
31
        parent::__construct($id, $module, $config);
32
    }
33
34
    /**
35
     *  Show summary from partial phploc statistic information per each defined group
36
     * @param bool $showErrors
37
     * @return int
38
     */
39
    public function actionSummary(bool $showErrors = false)
40
    {
41
        $service = $this->module->statService;
42
        $summary = $service->makeStatistic($this->module->prepareFiles());
0 ignored issues
show
Bug introduced by
The method makeStatistic() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        /** @scrutinizer ignore-call */ 
43
        $summary = $service->makeStatistic($this->module->prepareFiles());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        foreach ($summary as $name => &$row) {
44
            $row = ['Group' => $name] + $row;
45
        }
46
        $total = $service->summaryStatistic($summary);
47
        $total = ['Group' => 'Total'] + $total;
48
        $summary = $summary + [$total];
49
        if ($this->color) {
50
            $summary = $this->colorize(array_values($summary));
51
        }
52
        $this->headline('YII-2 Code Statistic', 'lightYellow');
53
        $this->climate->table($summary);
54
55
        if($showErrors !== true){
56
           return ExitCode::OK;
57
        }
58
        $this->headline('Failed for resolve', 'lightYellow');
59
        if(!count($service->errorList())){
60
            $this->climate->info('Errors not found');
61
        }else{
62
            $this->climate->table($service->errorList());
63
        }
64
        return ExitCode::OK;
65
    }
66
67
    /**
68
     *  Return full phploc statistic per each defined group
69
     * @param string|null $groupName
70
     * @return int
71
     */
72
    public function actionAdvanced(?string $groupName = null):int
73
    {
74
        $service = $this->module->statService;
75
        $statistic = $service->makeAdvancedStatistic($this->module->prepareFiles());
76
        $this->headline('YII-2 Code Statistic', 'green');
77
78
        if($groupName !==null){
79
            if(!isset($statistic[$groupName])){
80
                $this->stderr('Undefined group '.$groupName);
81
                return ExitCode::DATAERR;
82
            }
83
            $this->headline($groupName, 'lightYellow');
84
            $this->climate->table($statistic[$groupName]);
85
            return ExitCode::OK;
86
        }
87
88
        foreach ($statistic as $group =>$data){
89
            $this->headline($group, 'lightYellow');
90
            $this->climate->table($data);
91
            if(!$this->confirm('Show next group?')){
92
                break;
93
            }
94
        }
95
        return ExitCode::OK;
96
    }
97
98
    /**
99
     * Return  phploc statistic for all files
100
     * @return int
101
     */
102
    public function actionCommon():int
103
    {
104
        $service = $this->module->statService;
105
        $statistic = $service->makeCommonStatistic($this->module->prepareFiles());
106
        $this->headline('YII-2 Code Statistic', 'green');
107
        $this->climate->table($statistic);
108
        return ExitCode::OK;
109
    }
110
111
    /**
112
     * Show files that will be processed accordingly module configuration
113
     */
114
    public function actionListFiles()
115
    {
116
        Output::info('The following files will be processed accordingly module configuration');
117
        $files = $this->module->prepareFiles();
118
        Output::arrayList($files);
119
        Output::separator();
120
        Output::info('Total: ' . count($files));
121
    }
122
    
123
    protected function colorize(array $summary)
124
    {
125
        $colorized = [];
126
        foreach ($summary as $i => $row) {
127
            foreach ($row as $key => $value) {
128
                if ($key === 'Group') {
129
                    $value = $this->wrap($value, 'yellow');
130
                }
131
                if ($i == count($summary) - 1) {
132
                    $value = $this->wrap($value, 'light_cyan');
133
                }
134
                $key = $this->wrap($key, 'green');
135
                $colorized[$i][$key] = (string)$value;
136
            }
137
        }
138
        return $colorized;
139
    }
140
    
141
    protected function wrap($string, $color)
142
    {
143
        return "<bold><$color>$string</$color></bold>";
144
    }
145
    
146
    protected function headline($string, $color)
147
    {
148
        $this->climate->green()->border('=', 110)->$color()->tab(4)->out($string);
149
    }
150
}
151