1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Churn\Configuration; |
4
|
|
|
|
5
|
|
|
use function array_key_exists; |
6
|
|
|
use Webmozart\Assert\Assert; |
7
|
|
|
|
8
|
|
|
class Config |
9
|
|
|
{ |
10
|
|
|
const DIRECTORIES_TO_SCAN = []; |
11
|
|
|
const FILES_TO_SHOW = 10; |
12
|
|
|
const MINIMUM_SCORE_TO_SHOW = 0.1; |
13
|
|
|
const AMOUNT_OF_PARALLEL_JOBS = 10; |
14
|
|
|
const SHOW_COMMITS_SINCE = '10 years ago'; |
15
|
|
|
const FILES_TO_IGNORE = []; |
16
|
|
|
const FILE_EXTENSIONS_TO_PARSE = ['php']; |
17
|
|
|
const VCS = 'git'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a config with given configuration |
26
|
|
|
* @param array $configuration The array containing the configuration values. |
27
|
|
|
* @return Config |
28
|
|
|
*/ |
29
|
|
|
public static function create(array $configuration): Config |
30
|
|
|
{ |
31
|
|
|
return new self($configuration); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a config with default configuration |
36
|
|
|
* @return Config |
37
|
|
|
*/ |
38
|
|
|
public static function createFromDefaultValues(): Config |
39
|
|
|
{ |
40
|
|
|
return new self(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Config constructor. |
45
|
|
|
* @param array $configuration Raw config data. |
46
|
|
|
* @throws \InvalidArgumentException If parameters is badly defined. |
47
|
|
|
*/ |
48
|
|
|
private function __construct(array $configuration = []) |
49
|
|
|
{ |
50
|
|
|
if (!empty($configuration)) { |
51
|
|
|
$this->validateConfigurationValues($configuration); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->configuration = $configuration; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the names of directories to scan |
59
|
|
|
* |
60
|
|
|
* @return string[] |
61
|
|
|
*/ |
62
|
|
|
public function getDirectoriesToScan(): array |
63
|
|
|
{ |
64
|
|
|
return $this->configuration['directoriesToScan'] ?? self::DIRECTORIES_TO_SCAN; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the number of files to display in the results table. |
69
|
|
|
* @return integer |
70
|
|
|
*/ |
71
|
|
|
public function getFilesToShow(): int |
72
|
|
|
{ |
73
|
|
|
return $this->configuration['filesToShow'] ?? self::FILES_TO_SHOW; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the minimum score a file need to display. |
78
|
|
|
* @return float |
79
|
|
|
*/ |
80
|
|
|
public function getMinScoreToShow(): float |
81
|
|
|
{ |
82
|
|
|
return $this->configuration['minScoreToShow'] ?? self::MINIMUM_SCORE_TO_SHOW; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the number of parallel jobs to use to process the files. |
87
|
|
|
* @return integer |
88
|
|
|
*/ |
89
|
|
|
public function getParallelJobs(): int |
90
|
|
|
{ |
91
|
|
|
return $this->configuration['parallelJobs'] ?? self::AMOUNT_OF_PARALLEL_JOBS; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get how far back in the git history to go to count commits. |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getCommitsSince(): string |
99
|
|
|
{ |
100
|
|
|
return $this->configuration['commitsSince'] ?? self::SHOW_COMMITS_SINCE; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the paths to files to ignore when processing. |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getFilesToIgnore(): array |
108
|
|
|
{ |
109
|
|
|
return $this->configuration['filesToIgnore'] ?? self::FILES_TO_IGNORE; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the file extensions to use when processing. |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getFileExtensions(): array |
117
|
|
|
{ |
118
|
|
|
return $this->configuration['fileExtensions'] ?? self::FILE_EXTENSIONS_TO_PARSE; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the version control system. |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getVCS(): string |
126
|
|
|
{ |
127
|
|
|
return $this->configuration['vcs'] ?? self::VCS; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $configuration The array containing the configuration values. |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
private function validateConfigurationValues(array $configuration): void |
135
|
|
|
{ |
136
|
|
|
$this->validateDirectoriesToScan($configuration); |
137
|
|
|
$this->validateFilesToShow($configuration); |
138
|
|
|
$this->validateMinScoreToShow($configuration); |
139
|
|
|
$this->validateParallelJobs($configuration); |
140
|
|
|
$this->validateCommitsSince($configuration); |
141
|
|
|
$this->validateFilesToIgnore($configuration); |
142
|
|
|
$this->validateFileExtensions($configuration); |
143
|
|
|
$this->validateVCS($configuration); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array $configuration The array containing the configuration values. |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
private function validateDirectoriesToScan(array $configuration): void |
151
|
|
|
{ |
152
|
|
|
if (array_key_exists('directoriesToScan', $configuration)) { |
153
|
|
|
Assert::allString($configuration['directoriesToScan'], 'Directories to scan should be an array of strings'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $configuration The array containing the configuration values. |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
private function validateFilesToShow(array $configuration): void |
162
|
|
|
{ |
163
|
|
|
if (array_key_exists('filesToShow', $configuration)) { |
164
|
|
|
Assert::integer($configuration['filesToShow'], 'Files to show should be an integer'); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param array $configuration The array containing the configuration values. |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
private function validateMinScoreToShow(array $configuration): void |
173
|
|
|
{ |
174
|
|
|
if (array_key_exists('minScoreToShow', $configuration)) { |
175
|
|
|
Assert::numeric($configuration['minScoreToShow'], 'Minimum score to show should be a number'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $configuration The array containing the configuration values. |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
private function validateParallelJobs(array $configuration): void |
184
|
|
|
{ |
185
|
|
|
if (array_key_exists('parallelJobs', $configuration)) { |
186
|
|
|
Assert::integer($configuration['parallelJobs'], 'Amount of parallel jobs should be an integer'); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param array $configuration The array containing the configuration values. |
192
|
|
|
* @return void |
193
|
|
|
* @throws \InvalidArgumentException If date is in a bad format. |
194
|
|
|
*/ |
195
|
|
|
private function validateCommitsSince(array $configuration): void |
196
|
|
|
{ |
197
|
|
|
if (array_key_exists('commitsSince', $configuration)) { |
198
|
|
|
Assert::string($configuration['commitsSince'], 'Commits since should be a string'); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param array $configuration The array containing the configuration values. |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
private function validateFilesToIgnore(array $configuration): void |
207
|
|
|
{ |
208
|
|
|
if (array_key_exists('filesToIgnore', $configuration)) { |
209
|
|
|
Assert::isArray($configuration['filesToIgnore'], 'Files to ignore should be an array of strings'); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param array $configuration The array containing the configuration values. |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
|
|
private function validateFileExtensions(array $configuration): void |
218
|
|
|
{ |
219
|
|
|
if (array_key_exists('fileExtensions', $configuration)) { |
220
|
|
|
Assert::isArray($configuration['fileExtensions'], 'File extensions should be an array of strings'); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param array $configuration The array containing the configuration values. |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
|
private function validateVCS(array $configuration): void |
229
|
|
|
{ |
230
|
|
|
if (array_key_exists('vcs', $configuration)) { |
231
|
|
|
Assert::string($configuration['vcs'], 'VCS should be a string'); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|