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