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