1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Churn\Configuration; |
6
|
|
|
|
7
|
|
|
class Config |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private const DIRECTORIES_TO_SCAN = []; |
11
|
|
|
private const FILES_TO_SHOW = 10; |
12
|
|
|
private const MINIMUM_SCORE_TO_SHOW = 0.1; |
13
|
|
|
private const AMOUNT_OF_PARALLEL_JOBS = 10; |
14
|
|
|
private const SHOW_COMMITS_SINCE = '10 years ago'; |
15
|
|
|
private const FILES_TO_IGNORE = []; |
16
|
|
|
private const FILE_EXTENSIONS_TO_PARSE = ['php']; |
17
|
|
|
private const VCS = 'git'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array<string, mixed> |
21
|
|
|
*/ |
22
|
|
|
private $configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array<mixed> $configuration Raw config data. |
26
|
|
|
*/ |
27
|
|
|
private function __construct(array $configuration = []) |
28
|
|
|
{ |
29
|
|
|
if (!empty($configuration)) { |
30
|
|
|
(new Validator())->validateConfigurationValues($configuration); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->configuration = $configuration; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a config with given configuration. |
38
|
|
|
* |
39
|
|
|
* @param array<mixed> $configuration The array containing the configuration values. |
40
|
|
|
*/ |
41
|
|
|
public static function create(array $configuration): Config |
42
|
|
|
{ |
43
|
|
|
return new self($configuration); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a config with default configuration. |
48
|
|
|
*/ |
49
|
|
|
public static function createFromDefaultValues(): Config |
50
|
|
|
{ |
51
|
|
|
return new self(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the paths of directories to scan. |
56
|
|
|
* |
57
|
|
|
* @return array<string> |
58
|
|
|
*/ |
59
|
|
|
public function getDirectoriesToScan(): array |
60
|
|
|
{ |
61
|
|
|
return $this->configuration['directoriesToScan'] ?? self::DIRECTORIES_TO_SCAN; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array<string> $directories Paths of directories to scan. |
66
|
|
|
*/ |
67
|
|
|
public function setDirectoriesToScan(array $directories): void |
68
|
|
|
{ |
69
|
|
|
$this->configuration['directoriesToScan'] = $directories; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the number of files to display in the results table. |
74
|
|
|
*/ |
75
|
|
|
public function getFilesToShow(): int |
76
|
|
|
{ |
77
|
|
|
return $this->configuration['filesToShow'] ?? self::FILES_TO_SHOW; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the minimum score a file need to display. |
82
|
|
|
*/ |
83
|
|
|
public function getMinScoreToShow(): float |
84
|
|
|
{ |
85
|
|
|
return $this->configuration['minScoreToShow'] ?? self::MINIMUM_SCORE_TO_SHOW; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the number of parallel jobs to use to process the files. |
90
|
|
|
*/ |
91
|
|
|
public function getParallelJobs(): int |
92
|
|
|
{ |
93
|
|
|
return $this->configuration['parallelJobs'] ?? self::AMOUNT_OF_PARALLEL_JOBS; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param integer $parallelJobs Number of parallel jobs. |
98
|
|
|
*/ |
99
|
|
|
public function setParallelJobs(int $parallelJobs): void |
100
|
|
|
{ |
101
|
|
|
$this->configuration['parallelJobs'] = $parallelJobs; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get how far back in the git history to go to count commits. |
106
|
|
|
*/ |
107
|
|
|
public function getCommitsSince(): string |
108
|
|
|
{ |
109
|
|
|
return $this->configuration['commitsSince'] ?? self::SHOW_COMMITS_SINCE; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array<string> |
114
|
|
|
*/ |
115
|
|
|
public function getFilesToIgnore(): array |
116
|
|
|
{ |
117
|
|
|
return $this->configuration['filesToIgnore'] ?? self::FILES_TO_IGNORE; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the file extensions to use when processing. |
122
|
|
|
* |
123
|
|
|
* @return array<string> |
124
|
|
|
*/ |
125
|
|
|
public function getFileExtensions(): array |
126
|
|
|
{ |
127
|
|
|
return $this->configuration['fileExtensions'] ?? self::FILE_EXTENSIONS_TO_PARSE; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the version control system. |
132
|
|
|
*/ |
133
|
|
|
public function getVCS(): string |
134
|
|
|
{ |
135
|
|
|
return $this->configuration['vcs'] ?? self::VCS; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|