Passed
Push — main ( 0f6316...2e020c )
by mikhail
03:25
created

Analyzer::checkThresholdsExceeded()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 5
nop 0
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
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\Config;
13
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException;
14
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\MetricsFacade;
15
use SavinMikhail\CommentsDensity\Baseline\Storage\BaselineStorageInterface;
16
use SplFileInfo;
17
use Symfony\Contracts\Cache\CacheInterface;
18
19
final readonly class Analyzer
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 19 at column 6
Loading history...
20
{
21
    public function __construct(
22
        private Config $configDTO,
23
        private CommentTypeFactory $commentFactory,
24
        private MetricsFacade $metrics,
25
        private BaselineStorageInterface $baselineStorage,
26
        private CacheInterface $cache,
27
        private CommentStatisticsAggregator $statisticsAggregator,
28
    ) {}
29
30
    /**
31
     * @param SplFileInfo[] $files
32
     * @throws CommentsDensityException|InvalidArgumentException
33
     */
34
    public function analyze(iterable $files): Report
35
    {
36
        $this->metrics->startPerformanceMonitoring();
37
        $comments = [];
38
        $filesAnalyzed = 0;
39
        $totalLinesOfCode = 0;
40
41
        foreach ($files as $file) {
42
            $contentExtractor = new FileContentExtractor($file, $this->configDTO);
43
            if ($contentExtractor->shouldSkipFile()) {
44
                continue;
45
            }
46
            $commentFinder = new CommentFinder(
47
                $this->commentFactory,
48
                $this->configDTO,
49
            );
50
51
            $fileComments = $this->cache->get(
52
                $this->getCacheKey($file),
53
                static fn(): array => $commentFinder($contentExtractor->getContent(), $file->getRealPath()),
54
            );
55
56
            $lines = (new FileTotalLinesCounter($file))();
57
58
            $comments = [...$comments, ...$fileComments];
59
            $totalLinesOfCode += $lines;
60
            ++$filesAnalyzed;
61
        }
62
63
        if ($this->configDTO->useBaseline) {
64
            $comments = $this->baselineStorage->filterComments($comments);
65
        }
66
67
        $commentStatistics = $this->statisticsAggregator->calculateCommentStatistics($comments);
68
69
        return $this->createReport($comments, $commentStatistics, $filesAnalyzed, $totalLinesOfCode);
70
    }
71
72
    private function getCacheKey(SplFileInfo $file): string
73
    {
74
        $filePath = $file->getRealPath();
75
        $lastModified = filemtime($filePath);
76
77
        return md5($filePath . $lastModified);
78
    }
79
80
    private function checkThresholdsExceeded(): bool
81
    {
82
        if ($this->metrics->hasExceededThreshold()) {
83
            return true;
84
        }
85
        foreach ($this->commentFactory->getCommentTypes() as $commentType) {
86
            if ($commentType->hasExceededThreshold()) {
87
                return true;
88
            }
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * @param CommentDTO[] $comments
96
     * @param CommentStatisticsDTO[] $preparedStatistics
97
     */
98
    private function createReport(
99
        array $comments,
100
        array $preparedStatistics,
101
        int $filesAnalyzed,
102
        int $totalLinesOfCode,
103
    ): Report {
104
        $comToLoc = $this->metrics->prepareComToLoc($preparedStatistics, $totalLinesOfCode);
105
        $cds = $this->metrics->prepareCDS($this->metrics->calculateCDS($preparedStatistics));
106
        $exceedThreshold = $this->checkThresholdsExceeded();
107
        $this->metrics->stopPerformanceMonitoring();
108
        $performanceMetrics = $this->metrics->getPerformanceMetrics();
109
110
        return new Report(
111
            $filesAnalyzed,
112
            $preparedStatistics,
113
            $comments,
114
            $performanceMetrics,
115
            $comToLoc,
116
            $cds,
117
            $exceedThreshold,
118
        );
119
    }
120
}
121