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