Completed
Push — master ( 4aee41...86ed77 )
by Insolita
02:15
created

DefaultController::actionAdvanced()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 13
rs 9.9666
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
    /**
19
     * @var CodeStatModule|Module
20
     **/
21
    public $module;
22
    
23
    public $color = true;
24
    
25
    protected $climate;
26
    
27
    public function __construct($id, Module $module, CLImate $CLImate, array $config = [])
28
    {
29
        $this->climate = $CLImate;
30
        parent::__construct($id, $module, $config);
31
    }
32
33
    public function actionIndex(bool $showErrors = false)
34
    {
35
        $service = $this->module->statService;
36
        $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

36
        /** @scrutinizer ignore-call */ 
37
        $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...
37
        foreach ($summary as $name => &$row) {
38
            $row = ['Group' => $name] + $row;
39
        }
40
        $total = $service->summaryStatistic($summary);
41
        $total = ['Group' => 'Total'] + $total;
42
        $summary = $summary + [$total];
43
        if ($this->color) {
44
            $summary = $this->colorize(array_values($summary));
45
        }
46
        $this->headline('YII-2 Code Statistic', 'lightYellow');
47
        $this->climate->table($summary);
48
49
        if($showErrors !== true){
50
           return ExitCode::OK;
51
        }
52
        $this->headline('Failed for resolve', 'lightYellow');
53
        if(!count($service->errorList())){
0 ignored issues
show
Bug introduced by
The method errorList() does not exist on insolita\codestat\lib\co...odestatServiceInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to insolita\codestat\lib\co...odestatServiceInterface. ( Ignorable by Annotation )

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

53
        if(!count($service->/** @scrutinizer ignore-call */ errorList())){
Loading history...
54
            $this->climate->info('Errors not found');
55
        }else{
56
            $this->climate->table($service->errorList());
57
        }
58
        return ExitCode::OK;
59
    }
60
61
    public function actionAdvanced()
62
    {
63
        $service = $this->module->statService;
64
        $statistic = $service->makeAdvansedStatistic($this->module->prepareFiles());
65
        $this->headline('YII-2 Code Statistic', 'lightYellow');
66
        foreach ($statistic as $groupName =>$data){
67
            $this->headline($groupName, 'yellow');
68
            $this->climate->table($data);
69
            if(!$this->confirm('Show next group?')){
70
                break;
71
            }
72
        }
73
        return ExitCode::OK;
74
    }
75
    
76
    public function actionListFiles()
77
    {
78
        Output::info('The following files will be processed accordingly module configuration');
79
        $files = $this->module->prepareFiles();
80
        Output::arrayList($files);
81
        Output::separator();
82
        Output::info('Total: ' . count($files));
83
    }
84
    
85
    protected function colorize(array $summary)
86
    {
87
        $colorized = [];
88
        foreach ($summary as $i => $row) {
89
            foreach ($row as $key => $value) {
90
                if ($key === 'Group') {
91
                    $value = $this->wrap($value, 'yellow');
92
                }
93
                if ($i == count($summary) - 1) {
94
                    $value = $this->wrap($value, 'light_cyan');
95
                }
96
                $key = $this->wrap($key, 'green');
97
                $colorized[$i][$key] = (string)$value;
98
            }
99
        }
100
        return $colorized;
101
    }
102
    
103
    protected function wrap($string, $color)
104
    {
105
        return "<bold><$color>$string</$color></bold>";
106
    }
107
    
108
    protected function headline($string, $color)
109
    {
110
        $this->climate->green()->border('=', 110)->$color()->tab(4)->out($string);
111
    }
112
}
113