|
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
|
|
|
) { |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
5 |
|
public function analyze(Generator $files): OutputDTO |
|
39
|
|
|
{ |
|
40
|
5 |
|
$this->metrics->startPerformanceMonitoring(); |
|
41
|
5 |
|
$comments = []; |
|
42
|
5 |
|
$filesAnalyzed = 0; |
|
43
|
|
|
|
|
44
|
5 |
|
foreach ($files as $file) { |
|
45
|
5 |
|
$task = new AnalyzeFileTask( |
|
46
|
5 |
|
$this->cache, |
|
47
|
5 |
|
$this->docBlockAnalyzer, |
|
48
|
5 |
|
$this->missingDocBlock, |
|
49
|
5 |
|
$this->commentFactory, |
|
50
|
5 |
|
$this->configDTO, |
|
51
|
5 |
|
$this->output |
|
52
|
5 |
|
); |
|
53
|
|
|
|
|
54
|
5 |
|
$response = $task->run($file); |
|
55
|
|
|
|
|
56
|
5 |
|
$fileComments = $response['comments']; |
|
57
|
5 |
|
$lines = $response['lines']; |
|
58
|
|
|
|
|
59
|
5 |
|
array_push($comments, ...$fileComments); |
|
60
|
5 |
|
$this->totalLinesOfCode += $lines; |
|
61
|
5 |
|
$filesAnalyzed++; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
if ($this->configDTO->useBaseline) { |
|
65
|
|
|
$comments = $this->baselineStorage->filterComments($comments); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
$commentStatistics = $this->statisticsAggregator->calculateCommentStatistics($comments); |
|
69
|
|
|
|
|
70
|
5 |
|
return $this->createOutputDTO($comments, $commentStatistics, $filesAnalyzed); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
private function checkThresholdsExceeded(): bool |
|
74
|
|
|
{ |
|
75
|
5 |
|
if ($this->metrics->hasExceededThreshold()) { |
|
76
|
2 |
|
return true; |
|
77
|
|
|
} |
|
78
|
3 |
|
if ($this->missingDocBlock->hasExceededThreshold()) { |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
3 |
|
foreach ($this->commentFactory->getCommentTypes() as $commentType) { |
|
82
|
3 |
|
if ($commentType->hasExceededThreshold()) { |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
3 |
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param CommentDTO[] $comments |
|
91
|
|
|
* @param CommentStatisticsDTO[] $preparedStatistics |
|
92
|
|
|
* @param int $filesAnalyzed |
|
93
|
|
|
* @return OutputDTO |
|
94
|
|
|
*/ |
|
95
|
5 |
|
private function createOutputDTO( |
|
96
|
|
|
array $comments, |
|
97
|
|
|
array $preparedStatistics, |
|
98
|
|
|
int $filesAnalyzed |
|
99
|
|
|
): OutputDTO { |
|
100
|
5 |
|
$comToLoc = $this->metrics->prepareComToLoc($preparedStatistics, $this->totalLinesOfCode); |
|
101
|
5 |
|
$cds = $this->metrics->prepareCDS($this->metrics->calculateCDS($preparedStatistics)); |
|
102
|
5 |
|
$exceedThreshold = $this->checkThresholdsExceeded(); |
|
103
|
5 |
|
$this->metrics->stopPerformanceMonitoring(); |
|
104
|
5 |
|
$performanceMetrics = $this->metrics->getPerformanceMetrics(); |
|
105
|
|
|
|
|
106
|
5 |
|
return new OutputDTO( |
|
107
|
5 |
|
$filesAnalyzed, |
|
108
|
5 |
|
$preparedStatistics, |
|
109
|
5 |
|
$comments, |
|
110
|
5 |
|
$performanceMetrics, |
|
111
|
5 |
|
$comToLoc, |
|
112
|
5 |
|
$cds, |
|
113
|
5 |
|
$exceedThreshold |
|
114
|
5 |
|
); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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