Completed
Push — master ( a44a96...6cab0f )
by Insolita
01:37
created

DefaultController::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 League\CLImate\CLImate;
11
use yii\base\Module;
12
use yii\console\Controller;
13
14
class DefaultController extends Controller
15
{
16
    /**
17
     * @var CodeStatModule|Module
18
     **/
19
    public $module;
20
    
21
    public $color = true;
22
23
    /**
24
     * If true will output files with failed reads by Reflection class
25
    */
26
    public $showErrors = false;
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
    public function options($actionID)
37
    {
38
        return array_merge(parent::options($actionID) , ['showErrors']);
39
    }
40
41
    public function actionIndex()
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($this->showErrors === true){
58
            $this->headline('Failed for resolve', 'lightYellow');
59
            $this->climate->table($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

59
            $this->climate->table($service->/** @scrutinizer ignore-call */ errorList());
Loading history...
60
        }
61
    }
62
    
63
    public function actionListFiles()
64
    {
65
        Output::info('The following files will be processed accordingly module configuration');
66
        $files = $this->module->prepareFiles();
67
        Output::arrayList($files);
68
        Output::separator();
69
        Output::info('Total: ' . count($files));
70
    }
71
    
72
    protected function colorize(array $summary)
73
    {
74
        $colorized = [];
75
        foreach ($summary as $i => $row) {
76
            foreach ($row as $key => $value) {
77
                if ($key === 'Group') {
78
                    $value = $this->wrap($value, 'yellow');
79
                }
80
                if ($i == count($summary) - 1) {
81
                    $value = $this->wrap($value, 'light_cyan');
82
                }
83
                $key = $this->wrap($key, 'green');
84
                $colorized[$i][$key] = (string)$value;
85
            }
86
        }
87
        return $colorized;
88
    }
89
    
90
    protected function wrap($string, $color)
91
    {
92
        return "<bold><$color>$string</$color></bold>";
93
    }
94
    
95
    protected function headline($string, $color)
96
    {
97
        $this->climate->green()->border('=', 110)->$color()->tab(4)->out($string);
98
    }
99
}
100