1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\Analyzer; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use SavinMikhail\CommentsDensity\Baseline\Storage\BaselineStorageInterface; |
9
|
|
|
use SavinMikhail\CommentsDensity\Cache\Cache; |
|
|
|
|
10
|
|
|
use SavinMikhail\CommentsDensity\Comments\CommentFactory; |
|
|
|
|
11
|
|
|
use SavinMikhail\CommentsDensity\DTO\Input\ConfigDTO; |
|
|
|
|
12
|
|
|
use SavinMikhail\CommentsDensity\DTO\Output\CommentDTO; |
|
|
|
|
13
|
|
|
use SavinMikhail\CommentsDensity\DTO\Output\CommentStatisticsDTO; |
|
|
|
|
14
|
|
|
use SavinMikhail\CommentsDensity\DTO\Output\OutputDTO; |
|
|
|
|
15
|
|
|
use SavinMikhail\CommentsDensity\Metrics\MetricsFacade; |
|
|
|
|
16
|
|
|
use SavinMikhail\CommentsDensity\MissingDocblock\MissingDocBlockAnalyzer; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
use function array_push; |
20
|
|
|
|
21
|
|
|
final class Analyzer |
22
|
|
|
{ |
23
|
|
|
private int $totalLinesOfCode = 0; |
24
|
|
|
|
25
|
6 |
|
public function __construct( |
26
|
|
|
private readonly ConfigDTO $configDTO, |
27
|
|
|
private readonly CommentFactory $commentFactory, |
28
|
|
|
private readonly MissingDocBlockAnalyzer $missingDocBlock, |
29
|
|
|
private readonly MetricsFacade $metrics, |
30
|
|
|
private readonly OutputInterface $output, |
31
|
|
|
private readonly MissingDocBlockAnalyzer $docBlockAnalyzer, |
32
|
|
|
private readonly BaselineStorageInterface $baselineStorage, |
33
|
|
|
private readonly Cache $cache, |
34
|
|
|
private readonly CommentStatisticsAggregator $statisticsAggregator, |
|
|
|
|
35
|
6 |
|
) {} |
36
|
|
|
|
37
|
5 |
|
public function analyze(Generator $files): OutputDTO |
38
|
|
|
{ |
39
|
5 |
|
$this->metrics->startPerformanceMonitoring(); |
40
|
5 |
|
$comments = []; |
41
|
5 |
|
$filesAnalyzed = 0; |
42
|
|
|
|
43
|
5 |
|
foreach ($files as $file) { |
44
|
5 |
|
$task = new AnalyzeFileTask( |
|
|
|
|
45
|
5 |
|
$this->cache, |
46
|
5 |
|
$this->docBlockAnalyzer, |
47
|
5 |
|
$this->missingDocBlock, |
48
|
5 |
|
$this->commentFactory, |
49
|
5 |
|
$this->configDTO, |
50
|
5 |
|
$this->output, |
51
|
5 |
|
); |
52
|
|
|
|
53
|
5 |
|
$response = $task->run($file); |
54
|
|
|
|
55
|
5 |
|
$fileComments = $response['comments']; |
56
|
5 |
|
$lines = $response['lines']; |
57
|
|
|
|
58
|
5 |
|
array_push($comments, ...$fileComments); |
59
|
5 |
|
$this->totalLinesOfCode += $lines; |
60
|
5 |
|
++$filesAnalyzed; |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
if ($this->configDTO->useBaseline) { |
64
|
|
|
$comments = $this->baselineStorage->filterComments($comments); |
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
$commentStatistics = $this->statisticsAggregator->calculateCommentStatistics($comments); |
68
|
|
|
|
69
|
5 |
|
return $this->createOutputDTO($comments, $commentStatistics, $filesAnalyzed); |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
private function checkThresholdsExceeded(): bool |
73
|
|
|
{ |
74
|
5 |
|
if ($this->metrics->hasExceededThreshold()) { |
75
|
2 |
|
return true; |
76
|
|
|
} |
77
|
3 |
|
if ($this->missingDocBlock->hasExceededThreshold()) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
3 |
|
foreach ($this->commentFactory->getCommentTypes() as $commentType) { |
81
|
3 |
|
if ($commentType->hasExceededThreshold()) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param CommentDTO[] $comments |
91
|
|
|
* @param CommentStatisticsDTO[] $preparedStatistics |
92
|
|
|
*/ |
93
|
5 |
|
private function createOutputDTO( |
94
|
|
|
array $comments, |
95
|
|
|
array $preparedStatistics, |
96
|
|
|
int $filesAnalyzed, |
97
|
|
|
): OutputDTO { |
98
|
5 |
|
$comToLoc = $this->metrics->prepareComToLoc($preparedStatistics, $this->totalLinesOfCode); |
99
|
5 |
|
$cds = $this->metrics->prepareCDS($this->metrics->calculateCDS($preparedStatistics)); |
100
|
5 |
|
$exceedThreshold = $this->checkThresholdsExceeded(); |
101
|
5 |
|
$this->metrics->stopPerformanceMonitoring(); |
102
|
5 |
|
$performanceMetrics = $this->metrics->getPerformanceMetrics(); |
103
|
|
|
|
104
|
5 |
|
return new OutputDTO( |
105
|
5 |
|
$filesAnalyzed, |
106
|
5 |
|
$preparedStatistics, |
107
|
5 |
|
$comments, |
108
|
5 |
|
$performanceMetrics, |
109
|
5 |
|
$comToLoc, |
110
|
5 |
|
$cds, |
111
|
5 |
|
$exceedThreshold, |
112
|
5 |
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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