Completed
Push — master ( 8a4c11...c322ac )
by Insolita
02:19
created

DefaultController::headline()   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 2
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
use yii\helpers\FileHelper;
14
15
16
class DefaultController extends Controller
17
{
18
    /**
19
     * @var CodeStatModule|Module
20
     **/
21
    public $module;
22
    
23
    public $color = true;
24
25
    /**
26
     * If true will output files with failed reads by Reflection class
27
    */
28
    public $showErrors = false;
29
    
30
    protected $climate;
31
    
32
    public function __construct($id, Module $module, CLImate $CLImate, array $config = [])
33
    {
34
        $this->climate = $CLImate;
35
        parent::__construct($id, $module, $config);
36
    }
37
    
38
    public function actionIndex()
39
    {
40
        $service = $this->module->statService;
41
        $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

41
        /** @scrutinizer ignore-call */ 
42
        $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...
42
        foreach ($summary as $name => &$row) {
43
            $row = ['Group' => $name] + $row;
44
        }
45
        $total = $service->summaryStatistic($summary);
46
        $total = ['Group' => 'Total'] + $total;
47
        $summary = $summary + [$total];
48
        if ($this->color) {
49
            $summary = $this->colorize(array_values($summary));
50
        }
51
        $this->headline('YII-2 Code Statistic', 'lightYellow');
52
        $this->climate->table($summary);
53
54
        if($this->showErrors === true){
55
            $this->headline('Failed for resolve', 'lightYellow');
56
            $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

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