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