Passed
Push — master ( 58f621...c5eda0 )
by Insolita
01:22
created

CodeStatModule::defaultRules()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 22
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by solly [18.10.17 4:33]
4
 */
5
6
namespace insolita\codestat;
7
8
use insolita\codestat\lib\classdetect\RegexpDetector;
9
use insolita\codestat\lib\CodestatService;
10
use insolita\codestat\lib\collection\GroupCollection;
11
use insolita\codestat\lib\contracts\ClassDetectorInterface;
12
use insolita\codestat\lib\contracts\CodestatServiceInterface;
13
use Yii;
14
use yii\base\Action;
15
use yii\base\Behavior;
16
use yii\base\Component;
17
use yii\base\Event;
18
use yii\base\InvalidConfigException;
19
use yii\base\Model;
20
use yii\base\Module;
21
use yii\base\Object;
22
use yii\base\Widget;
23
use yii\console\Controller as ConsoleController;
24
use yii\db\ActiveQuery;
25
use yii\db\BaseActiveRecord;
26
use yii\di\Instance;
27
use yii\rest\Controller as RestController;
28
use yii\web\AssetBundle;
29
use yii\web\Controller as WebController;
30
31
/**
32
 *
33
 */
34
class CodeStatModule extends Module
35
{
36
    /**
37
     * array, list of directories that will be scanned
38
     *
39
     * @var array
40
     */
41
    public $scanTargets
42
        = [
43
            //
44
        ];
45
    
46
    /**
47
     * array, list of patterns excluding from the results matching file or directory paths
48
     *
49
     * @see \yii\helpers\FileHelper::findFiles() 'except' doc
50
     * @var array
51
     */
52
    public $exceptTargets
53
        = [
54
            'config*',
55
            'vendor*',
56
            '*web/*',
57
            '*runtime/*',
58
            '*views/*',
59
        ];
60
    
61
    /**
62
     * Custom group analyse function, with input Group, should return array with metric names and values
63
     *
64
     * @see CodestatServiceInterface::analyse()
65
     * @example
66
     * 'analyseCallback = function(Group $group){
67
     *       $metrics=$customAnalyzer->analyze($group->getFiles());
68
     *       return ['totalFiles'=>count($group->getFiles()), 'metric1'=>$metrics[some], ...etc];
69
     * }
70
     * @var null|callable
71
     */
72
    public $analyseCallback;
73
    
74
    /**
75
     * @var string|CodestatServiceInterface
76
     */
77
    public $statService = CodestatService::class;
78
    /**
79
     * @var string|ClassDetectorInterface
80
     */
81
    public $classDetector = RegexpDetector::class;
82
    /**
83
     * @var array|GroupCollection
84
     * Leave empty for use defaults
85
     */
86
    public $groupRules;
87
    
88
    /**
89
     *
90
     */
91
    public function init()
92
    {
93
        $this->checkConfig();
94
        $this->prepareScanTargets();
95
        $this->prepareRules();
96
        $this->prepareService();
97
        parent::init();
98
    }
99
    
100
    protected function checkConfig()
101
    {
102
        if (empty($this->scanTargets)) {
103
            throw new InvalidConfigException('scanTargets can`t be empty');
104
        }
105
        if (empty($this->groupRules)) {
106
            $this->groupRules = self::defaultRules();
107
        }
108
        if (!is_array($this->scanTargets) || !is_array($this->exceptTargets)) {
109
            throw new InvalidConfigException('scanTargets and exceptTargets must be array');
110
        }
111
        if (!(is_array($this->groupRules) || $this->groupRules instanceof GroupCollection)) {
112
            throw new InvalidConfigException('groupRules must be array or instance of GroupCollection');
113
        }
114
    }
115
    
116
    public static function defaultRules()
117
    {
118
        return [
119
            'Actions' => Action::class,
120
            'ActiveQuery'=>ActiveQuery::class,
121
            'ActiveRecords' => BaseActiveRecord::class,
122
            'AssetBundles' => AssetBundle::class,
123
            'Behaviors' => Behavior::class,
124
            'ConsoleControllers' => ConsoleController::class,
125
            'RestControllers' => RestController::class,
126
            'WebControllers' => WebController::class,
127
            'Events' => Event::class,
128
            'Models' => Model::class,
129
            'Modules' => Module::class,
130
            'Widgets' => Widget::class,
131
            'Components' => Component::class,
132
            'Objects' => Object::class,
133
            'PureClass' => function(\ReflectionClass $reflection) {
134
                return (
135
                    !$reflection->getParentClass()
136
                    && !$reflection->isInterface()
137
                    && !$reflection->isAbstract()
138
                );
139
            }
140
        ];
141
    }
142
    
143
    protected function prepareScanTargets()
144
    {
145
        $this->scanTargets = array_map(function($path) {
146
            return \Yii::getAlias($path);
147
        }, $this->scanTargets);
148
    }
149
    
150
    protected function prepareRules()
151
    {
152
        if (is_array($this->groupRules)) {
153
            $this->groupRules = new GroupCollection($this->groupRules);
154
        }
155
    }
156
    
157
    protected function prepareService()
158
    {
159
        $this->classDetector = Instance::ensure($this->classDetector, ClassDetectorInterface::class);
160
        $this->statService = Yii::createObject($this->statService, [$this->classDetector, $this->groupRules]);
161
    }
162
}
163