-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCognitiveMetricsCollector.php
More file actions
203 lines (172 loc) · 6.08 KB
/
Copy pathCognitiveMetricsCollector.php
File metadata and controls
203 lines (172 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Events\FileProcessed;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Events\ParserFailed;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Events\SourceFilesFound;
use Phauthentic\CognitiveCodeAnalysis\Business\DirectoryScanner;
use Phauthentic\CognitiveCodeAnalysis\CognitiveAnalysisException;
use Phauthentic\CognitiveCodeAnalysis\Config\CognitiveConfig;
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService;
use SplFileInfo;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Throwable;
/**
* CognitiveMetricsCollector class that collects cognitive metrics from source files
*/
class CognitiveMetricsCollector
{
/**
* @var array<string, array<string, string>>|null Cached ignored items from the last parsing operation
*/
private ?array $ignoredItems = null;
public function __construct(
protected readonly Parser $parser,
protected readonly DirectoryScanner $directoryScanner,
protected readonly ConfigService $configService,
protected readonly MessageBusInterface $messageBus,
) {
}
/**
* Collect cognitive metrics from the given path
*
* @param string $path
* @param CognitiveConfig $config
* @return CognitiveMetricsCollection
* @throws CognitiveAnalysisException|ExceptionInterface
*/
public function collect(string $path, CognitiveConfig $config): CognitiveMetricsCollection
{
$files = $this->findSourceFiles($path, $config->excludeFilePatterns);
/** @var SplFileInfo[] $clonedFiles */
$clonedFiles = [];
foreach ($files as $file) {
$clonedFiles[] = $file;
}
$this->messageBus->dispatch(new SourceFilesFound($clonedFiles));
return $this->findMetrics($clonedFiles);
}
/**
* @throws CognitiveAnalysisException
*/
private function getCodeFromFile(SplFileInfo $file): string
{
$code = file_get_contents($file->getRealPath());
if ($code === false) {
throw new CognitiveAnalysisException("Could not read file: {$file->getRealPath()}");
}
return $code;
}
/**
* Collect metrics from the found source files
*
* @param iterable<SplFileInfo> $files
* @return CognitiveMetricsCollection
* @throws ExceptionInterface
*/
private function findMetrics(iterable $files): CognitiveMetricsCollection
{
$metricsCollection = new CognitiveMetricsCollection();
foreach ($files as $file) {
try {
$metrics = $this->parser->parse(
$this->getCodeFromFile($file)
);
// Store ignored items from the parser
$this->ignoredItems = $this->parser->getIgnored();
} catch (Throwable $exception) {
$this->messageBus->dispatch(new ParserFailed(
$file,
$exception
));
continue;
}
$metricsCollection = $this->processMethodMetrics(
$metrics,
$metricsCollection,
$file->getRealPath()
);
$this->messageBus->dispatch(new FileProcessed(
$file,
));
}
return $metricsCollection;
}
/**
* @param array<string, mixed> $methodMetrics
* @param CognitiveMetricsCollection $metricsCollection
* @param string $file
* @return CognitiveMetricsCollection
*/
private function processMethodMetrics(
array $methodMetrics,
CognitiveMetricsCollection $metricsCollection,
string $file
): CognitiveMetricsCollection {
foreach ($methodMetrics as $classAndMethod => $metrics) {
if ($this->isExcluded($classAndMethod)) {
continue;
}
[$class, $method] = explode('::', $classAndMethod);
$metricsArray = array_merge($metrics, [
'class' => $class,
'method' => $method,
'file' => $file
]);
$metric = new CognitiveMetrics($metricsArray);
if (!$metricsCollection->contains($metric)) {
$metricsCollection->add($metric);
}
}
return $metricsCollection;
}
private function isExcluded(string $classAndMethod): bool
{
$regexes = $this->configService->getConfig()->excludePatterns;
foreach ($regexes as $regex) {
if (preg_match('/' . $regex . '/', $classAndMethod)) {
return true;
}
}
return false;
}
/**
* Find source files using DirectoryScanner
*
* @param string $path Path to the directory or file to scan
* @param array<int, string> $exclude List of regx to exclude
* @return iterable<mixed, SplFileInfo> An iterable of SplFileInfo objects
*/
private function findSourceFiles(string $path, array $exclude = []): iterable
{
return $this->directoryScanner->scan([$path], ['^(?!.*\.php$).+'] + $exclude);
}
/**
* Get all ignored classes and methods from the last parsing operation.
*
* @return array<string, array<string, string>> Array with 'classes' and 'methods' keys
*/
public function getIgnored(): array
{
return $this->ignoredItems ?? ['classes' => [], 'methods' => []];
}
/**
* Get ignored classes from the last parsing operation.
*
* @return array<string, string> Array of ignored class FQCNs
*/
public function getIgnoredClasses(): array
{
return $this->ignoredItems['classes'] ?? [];
}
/**
* Get ignored methods from the last parsing operation.
*
* @return array<string, string> Array of ignored method keys (ClassName::methodName)
*/
public function getIgnoredMethods(): array
{
return $this->ignoredItems['methods'] ?? [];
}
}