Passed
Push — master ( 44594f...8d043c )
by Insolita
01:42
created

DefaultController::actionDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

44
        /** @scrutinizer ignore-call */ 
45
        $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...
45
        foreach ($summary as $name => &$row) {
46
            $row = ['Group' => $name] + $row;
47
        }
48
        $total = $service->summaryStatistic($summary);
49
        $total = ['Group' => 'Total'] + $total;
50
        $summary = $summary + [$total];
51
        if ($this->color) {
52
            $summary = $this->colorize(array_values($summary));
53
        }
54
        $this->headline('YII-2 Code Statistic', 'lightYellow');
55
        $this->climate->table($summary);
56
57
        if($showErrors !== true){
58
           return ExitCode::OK;
59
        }
60
        $this->headline('Failed for resolve', 'lightYellow');
61
        if(!count($service->errorList())){
62
            $this->climate->info('Errors not found');
63
        }else{
64
            $this->climate->table($service->errorList());
65
        }
66
        return ExitCode::OK;
67
    }
68
69
    /**
70
     *  Return full phploc statistic per each defined group
71
     * @param string|null $groupName
72
     * @return int
73
     */
74
    public function actionAdvanced(?string $groupName = null):int
75
    {
76
        $service = $this->module->statService;
77
        $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

77
        $statistic = $service->makeAdvancedStatistic($this->module->prepareFiles(), /** @scrutinizer ignore-type */ $this->module->metrics);
Loading history...
78
        $this->headline('YII-2 Code Statistic', 'green');
79
80
        if($groupName !==null){
81
            if(!isset($statistic[$groupName])){
82
                $this->stderr('Undefined group '.$groupName);
83
                return ExitCode::DATAERR;
84
            }
85
            $this->headline($groupName, 'lightYellow');
86
            $this->climate->table($statistic[$groupName]);
87
            return ExitCode::OK;
88
        }
89
90
        foreach ($statistic as $group =>$data){
91
            $this->headline($group, 'lightYellow');
92
            $this->climate->table($data);
93
            if(!$this->confirm('Show next group?')){
94
                break;
95
            }
96
        }
97
        return ExitCode::OK;
98
    }
99
100
    /**
101
     * Return  phploc statistic for all files
102
     * @return int
103
     */
104
    public function actionCommon():int
105
    {
106
        $service = $this->module->statService;
107
        $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

107
        $statistic = $service->makeCommonStatistic($this->module->prepareFiles(), /** @scrutinizer ignore-type */ $this->module->metrics);
Loading history...
108
        $this->headline('YII-2 Code Statistic', 'green');
109
        $this->climate->table($statistic);
110
        return ExitCode::OK;
111
    }
112
113
    /**
114
     * Return  phploc statistic for concrete directory
115
     * @param string $dir - Path or path alias
116
     * @return int
117
     */
118
    public function actionDirectory(string $dir):int
119
    {
120
        $service = $this->module->statService;
121
        $dir = \Yii::getAlias($dir);
122
        if(!is_dir($dir)){
123
            $this->stderr('Directory not found by path '.$dir);
124
            return ExitCode::UNSPECIFIED_ERROR;
125
        }
126
        $statistic = $service->makeCommonStatistic(FileHelper::findFiles($dir, [
127
            'only' => ['*.php'],
128
            'caseSensitive' => false,
129
            'recursive' => true,
130
        ]), $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

130
        ]), /** @scrutinizer ignore-type */ $this->module->metrics);
Loading history...
131
        $this->headline('YII-2 Code Statistic', 'green');
132
        $this->climate->table($statistic);
133
        return ExitCode::OK;
134
    }
135
136
    /**
137
     * Return  phploc statistic for concrete file
138
     * @param string $filePath - Path or path alias
139
     * @return int
140
     */
141
    public function actionFile(string $filePath):int
142
    {
143
        $service = $this->module->statService;
144
        $filePath = \Yii::getAlias($filePath);
145
        if(!file_exists($filePath)){
146
            $this->stderr('File not found by path '.$filePath);
147
            return ExitCode::UNSPECIFIED_ERROR;
148
        }
149
        $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

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