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