1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer; |
6
|
|
|
|
7
|
|
|
use Psr\Cache\InvalidArgumentException; |
8
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO; |
|
|
|
|
9
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentStatisticsDTO; |
|
|
|
|
10
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\Report; |
|
|
|
|
11
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory; |
|
|
|
|
12
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConfigDTO; |
|
|
|
|
13
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException; |
14
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\MetricsFacade; |
|
|
|
|
15
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer; |
16
|
|
|
use SavinMikhail\CommentsDensity\Baseline\Storage\BaselineStorageInterface; |
17
|
|
|
use SplFileInfo; |
18
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
19
|
|
|
|
20
|
|
|
use function array_push; |
21
|
|
|
|
22
|
|
|
final class Analyzer |
23
|
|
|
{ |
24
|
|
|
private int $totalLinesOfCode = 0; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
private readonly ConfigDTO $configDTO, |
28
|
|
|
private readonly CommentTypeFactory $commentFactory, |
29
|
|
|
private readonly MetricsFacade $metrics, |
30
|
|
|
private readonly MissingDocBlockAnalyzer $missingDocBlockAnalyzer, |
31
|
|
|
private readonly BaselineStorageInterface $baselineStorage, |
32
|
|
|
private readonly CacheInterface $cache, |
33
|
|
|
private readonly CommentStatisticsAggregator $statisticsAggregator, |
|
|
|
|
34
|
|
|
) {} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param SplFileInfo[] $files |
38
|
|
|
* @throws CommentsDensityException|InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
public function analyze(iterable $files): Report |
41
|
|
|
{ |
42
|
|
|
$this->metrics->startPerformanceMonitoring(); |
43
|
|
|
$comments = []; |
44
|
|
|
$filesAnalyzed = 0; |
45
|
|
|
|
46
|
|
|
foreach ($files as $file) { |
47
|
|
|
$contentExtractor = new FileContentExtractor($file, $this->configDTO); |
|
|
|
|
48
|
|
|
if ($contentExtractor->shouldSkipFile()) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
$task = new CommentFinder( |
|
|
|
|
52
|
|
|
$this->commentFactory, |
53
|
|
|
$this->configDTO, |
54
|
|
|
$this->missingDocBlockAnalyzer, |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$fileComments = $this->cache->get( |
58
|
|
|
$this->getCacheKey($file), |
59
|
|
|
fn(): array => $task->run($contentExtractor->getContent(), $file->getRealPath()), |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$lines = (new FileTotalLinesCounter())->run($file); |
63
|
|
|
|
64
|
|
|
array_push($comments, ...$fileComments); |
65
|
|
|
$this->totalLinesOfCode += $lines; |
66
|
|
|
++$filesAnalyzed; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($this->configDTO->useBaseline) { |
70
|
|
|
$comments = $this->baselineStorage->filterComments($comments); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$commentStatistics = $this->statisticsAggregator->calculateCommentStatistics($comments); |
74
|
|
|
|
75
|
|
|
return $this->createOutputDTO($comments, $commentStatistics, $filesAnalyzed); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getCacheKey(SplFileInfo $file): string |
79
|
|
|
{ |
80
|
|
|
$filePath = $file->getRealPath(); |
81
|
|
|
$lastModified = filemtime($filePath); |
82
|
|
|
return md5($filePath . $lastModified); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function checkThresholdsExceeded(): bool |
86
|
|
|
{ |
87
|
|
|
if ($this->metrics->hasExceededThreshold()) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
if ($this->missingDocBlockAnalyzer->hasExceededThreshold()) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
foreach ($this->commentFactory->getCommentTypes() as $commentType) { |
94
|
|
|
if ($commentType->hasExceededThreshold()) { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param CommentDTO[] $comments |
104
|
|
|
* @param CommentStatisticsDTO[] $preparedStatistics |
105
|
|
|
*/ |
106
|
|
|
private function createOutputDTO( |
107
|
|
|
array $comments, |
108
|
|
|
array $preparedStatistics, |
109
|
|
|
int $filesAnalyzed, |
110
|
|
|
): Report { |
111
|
|
|
$comToLoc = $this->metrics->prepareComToLoc($preparedStatistics, $this->totalLinesOfCode); |
112
|
|
|
$cds = $this->metrics->prepareCDS($this->metrics->calculateCDS($preparedStatistics)); |
113
|
|
|
$exceedThreshold = $this->checkThresholdsExceeded(); |
114
|
|
|
$this->metrics->stopPerformanceMonitoring(); |
115
|
|
|
$performanceMetrics = $this->metrics->getPerformanceMetrics(); |
116
|
|
|
|
117
|
|
|
return new Report( |
118
|
|
|
$filesAnalyzed, |
119
|
|
|
$preparedStatistics, |
120
|
|
|
$comments, |
121
|
|
|
$performanceMetrics, |
122
|
|
|
$comToLoc, |
123
|
|
|
$cds, |
124
|
|
|
$exceedThreshold, |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths