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