Passed
Push — master ( c5eda0...8a7fed )
by Insolita
02:55
created

DefaultController::actionListFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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;
0 ignored issues
show
Bug introduced by
The type League\CLImate\CLImate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use function str_repeat;
12
use Yii;
13
use yii\base\Module;
14
use yii\console\Controller;
15
use yii\helpers\Console;
16
use yii\helpers\FileHelper;
17
use function mb_strwidth;
18
19
class DefaultController extends Controller
20
{
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
    public function actionIndex()
37
    {
38
        $service = $this->module->statService;
39
        $summary = $service->makeStatistic($this->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

39
        /** @scrutinizer ignore-call */ 
40
        $summary = $service->makeStatistic($this->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...
40
        foreach ($summary as $name => &$row) {
41
            $row = ['Group' => $name] + $row;
42
        }
43
        $total = $service->summaryStatistic($summary);
44
        $total = ['Group' => 'Total'] + $total;
45
        $summary = $summary + [$total];
46
        if ($this->color) {
47
            $summary = $this->colorize(array_values($summary));
48
        }
49
        $this->headline('YII-2 Code Statistic', 'lightYellow');
50
        $this->climate->table($summary);
51
    }
52
    
53
    public function actionListFiles()
54
    {
55
        Output::info('The following files will be processed accordingly module configuration');
56
        $files = $this->prepareFiles();
57
        Output::arrayList($files);
58
        Output::separator();
59
        Output::info('Total: ' . count($files));
60
    }
61
    
62
    protected function colorize(array $summary)
63
    {
64
        $colorized = [];
65
        foreach ($summary as $i => $row) {
66
            foreach ($row as $key => $value) {
67
                if ($key === 'Group') {
68
                    $value = $this->wrap($value, 'yellow');
69
                }
70
                if ($i == count($summary) - 1) {
71
                    $value = $this->wrap($value, 'light_cyan');
72
                }
73
                $key = $this->wrap($key, 'green');
74
                $colorized[$i][$key] = (string)$value;
75
            }
76
        }
77
        return $colorized;
78
    }
79
    
80
    protected function wrap($string, $color)
81
    {
82
        return "<bold><$color>$string</$color></bold>";
83
    }
84
    
85
    protected function headline($string, $color)
86
    {
87
        $this->climate->green()->border('=', 110)->$color()->tab(4)->out($string);
88
    }
89
    
90
    /**
91
     * @return array
92
     */
93
    protected function prepareFiles()
94
    {
95
        $files = [];
96
        foreach ($this->module->scanTargets as $dir) {
97
            $files = array_merge(FileHelper::findFiles($dir, [
98
                'only' => ['*.php'],
99
                'except' => $this->module->exceptTargets,
100
                'caseSensitive' => false,
101
                'recursive' => true,
102
            ]), $files);
103
        }
104
        return $files;
105
    }
106
}
107