Test Failed
Push — main ( bd747d...5d33d5 )
by mikhail
03:54
created

Analyzer::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
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;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...r\DTO\Output\CommentDTO was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentStatisticsDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...ut\CommentStatisticsDTO was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\Report;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...lyzer\DTO\Output\Report was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...ents\CommentTypeFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConfigDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...ts\Config\DTO\ConfigDTO was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException;
14
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\MetricsFacade;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...s\Metrics\MetricsFacade was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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,
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...entStatisticsAggregator was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...er\FileContentExtractor was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
            if ($contentExtractor->shouldSkipFile()) {
49
                continue;
50
            }
51
            $task = new CommentFinder(
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...\Analyzer\CommentFinder was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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