Passed
Push — master ( 247be1...a91530 )
by Insolita
01:41
created

DefaultController::actionListMetrics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 insolita\codestat\CodeStatModule;
9
use insolita\codestat\helpers\Output;
10
use insolita\codestat\lib\CodestatService;
11
use League\CLImate\CLImate;
12
use yii\base\Module;
13
use yii\console\Controller;
14
use yii\console\ExitCode;
15
use yii\helpers\FileHelper;
16
use function count;
17
use function file_exists;
18
19
class DefaultController extends Controller
20
{
21
    public $defaultAction = 'summary';
22
    /**
23
     * @var CodeStatModule|Module
24
     **/
25
    public $module;
26
    
27
    public $color = true;
28
    
29
    protected $climate;
30
    
31
    public function __construct($id, Module $module, CLImate $CLImate, array $config = [])
32
    {
33
        $this->climate = $CLImate;
34
        parent::__construct($id, $module, $config);
35
    }
36
37
    /**
38
     *  Show summary from partial phploc statistic information per each defined group
39
     * @param bool $showErrors
40
     * @return int
41
     */
42
    public function actionSummary(bool $showErrors = false)
43
    {
44
        $service = $this->module->statService;
45
        $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

45
        /** @scrutinizer ignore-call */ 
46
        $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...
46
        foreach ($summary as $name => &$row) {
47
            $row = ['Group' => $name] + $row;
48
        }
49
        $total = $service->summaryStatistic($summary);
50
        $total = ['Group' => 'Total'] + $total;
51
        $summary = $summary + [$total];
52
        if ($this->color) {
53
            $summary = $this->colorize(array_values($summary));
54
        }
55
        $this->headline('YII-2 Code Statistic', 'lightYellow');
56
        $this->climate->table($summary);
57
58
        if($showErrors !== true){
59
           return ExitCode::OK;
60
        }
61
        $this->headline('Failed for resolve', 'lightYellow');
62
        if(!count($service->errorList())){
63
            $this->climate->info('Errors not found');
64
        }else{
65
            Output::arrayList($service->errorList());
66
        }
67
        return ExitCode::OK;
68
    }
69
70
    /**
71
     *  Return full phploc statistic per each defined group
72
     * @param string|null $groupName
73
     * @return int
74
     */
75
    public function actionAdvanced(?string $groupName = null):int
76
    {
77
        $service = $this->module->statService;
78
        $statistic = $service->makeAdvancedStatistic($this->module->prepareFiles(), $this->module->metrics);
0 ignored issues
show
Bug introduced by
It seems like $this->module->metrics can also be of type null and object; however, parameter $metrics of insolita\codestat\lib\co...makeAdvancedStatistic() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

78
        $statistic = $service->makeAdvancedStatistic($this->module->prepareFiles(), /** @scrutinizer ignore-type */ $this->module->metrics);
Loading history...
79
        $this->headline('YII-2 Code Statistic', 'green');
80
81
        if($groupName !==null){
82
            if(!isset($statistic[$groupName])){
83
                $this->stderr('Undefined group '.$groupName);
84
                return ExitCode::DATAERR;
85
            }
86
            $this->headline($groupName, 'lightYellow');
87
            $this->climate->table($statistic[$groupName]);
88
            return ExitCode::OK;
89
        }
90
91
        foreach ($statistic as $group =>$data){
92
            $this->headline($group, 'lightYellow');
93
            //$this->climate->table($data);
94
            Output::arrayList($data);
95
            if(!$this->confirm('Show next group?')){
96
                break;
97
            }
98
        }
99
        return ExitCode::OK;
100
    }
101
102
    /**
103
     * Return  phploc statistic for all files
104
     * @return int
105
     */
106
    public function actionCommon():int
107
    {
108
        $service = $this->module->statService;
109
        $statistic = $service->makeCommonStatistic($this->module->prepareFiles(), $this->module->metrics);
0 ignored issues
show
Bug introduced by
It seems like $this->module->metrics can also be of type null and object; however, parameter $metrics of insolita\codestat\lib\co...::makeCommonStatistic() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

109
        $statistic = $service->makeCommonStatistic($this->module->prepareFiles(), /** @scrutinizer ignore-type */ $this->module->metrics);
Loading history...
110
        $this->headline('YII-2 Code Statistic', 'green');
111
        //$this->climate->table($statistic);
112
        Output::arrayList($statistic);
113
        return ExitCode::OK;
114
    }
115
116
    /**
117
     * Return  phploc statistic for concrete directory
118
     * @param string $dir - Path or path alias
119
     * @return int
120
     */
121
    public function actionDirectory(string $dir):int
122
    {
123
        $service = $this->module->statService;
124
        $dir = \Yii::getAlias($dir);
125
        if(!is_dir($dir)){
126
            $this->stderr('Directory not found by path '.$dir);
127
            return ExitCode::UNSPECIFIED_ERROR;
128
        }
129
        $statistic = $service->makeCommonStatistic(FileHelper::findFiles($dir, [
130
            'only' => ['*.php'],
131
            'caseSensitive' => false,
132
            'recursive' => true,
133
        ]), $this->module->metrics);
0 ignored issues
show
Bug introduced by
It seems like $this->module->metrics can also be of type null and object; however, parameter $metrics of insolita\codestat\lib\co...::makeCommonStatistic() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

133
        ]), /** @scrutinizer ignore-type */ $this->module->metrics);
Loading history...
134
        $this->headline('YII-2 Code Statistic', 'green');
135
        //$this->climate->table($statistic);
136
        Output::arrayList($statistic);
137
        return ExitCode::OK;
138
    }
139
140
    /**
141
     * Return  phploc statistic for concrete file
142
     * @param string $filePath - Path or path alias
143
     * @return int
144
     */
145
    public function actionFile(string $filePath):int
146
    {
147
        $service = $this->module->statService;
148
        $filePath = \Yii::getAlias($filePath);
149
        if(!file_exists($filePath)){
150
            $this->stderr('File not found by path '.$filePath);
151
            return ExitCode::UNSPECIFIED_ERROR;
152
        }
153
        $statistic = $service->makeCommonStatistic([$filePath], $this->module->metrics);
0 ignored issues
show
Bug introduced by
It seems like $this->module->metrics can also be of type null and object; however, parameter $metrics of insolita\codestat\lib\co...::makeCommonStatistic() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

153
        $statistic = $service->makeCommonStatistic([$filePath], /** @scrutinizer ignore-type */ $this->module->metrics);
Loading history...
154
        $this->headline('YII-2 Code Statistic', 'green');
155
        //$this->climate->table($statistic);
156
        Output::arrayList($statistic);
157
        return ExitCode::OK;
158
    }
159
160
    /**
161
     * Show files that will be processed accordingly module configuration
162
     */
163
    public function actionListFiles()
164
    {
165
        Output::info('The following files will be processed accordingly module configuration');
166
        $files = $this->module->prepareFiles();
167
        Output::arrayList($files);
168
        Output::separator();
169
        Output::info('Total: ' . count($files));
170
    }
171
172
    /**
173
     * Show available metrics
174
     */
175
    public function actionListMetrics()
176
    {
177
        Output::arrayList(CodestatService::$metricNames);
178
        Output::separator();
179
    }
180
    
181
    protected function colorize(array $summary)
182
    {
183
        $colorized = [];
184
        foreach ($summary as $i => $row) {
185
            foreach ($row as $key => $value) {
186
                if ($key === 'Group') {
187
                    $value = $this->wrap($value, 'yellow');
188
                }
189
                if ($i == count($summary) - 1) {
190
                    $value = $this->wrap($value, 'light_cyan');
191
                }
192
                $key = $this->wrap($key, 'green');
193
                $colorized[$i][$key] = (string)$value;
194
            }
195
        }
196
        return $colorized;
197
    }
198
    
199
    protected function wrap($string, $color)
200
    {
201
        return "<bold><$color>$string</$color></bold>";
202
    }
203
    
204
    protected function headline($string, $color)
205
    {
206
        $this->climate->green()->border('=', 110)->$color()->tab(4)->out($string);
207
    }
208
}
209