Passed
Push — main ( a82980...5a08b8 )
by mikhail
14:17
created

Analyzer::shouldAnalyzeMissingDocBlocks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\Cache\Cache 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\Comments\CommentFactory;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...Comments\CommentFactory 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\DTO\Input\ConfigDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\DTO\Input\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...
12
use SavinMikhail\CommentsDensity\DTO\Output\CommentDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\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...
13
use SavinMikhail\CommentsDensity\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...
14
use SavinMikhail\CommentsDensity\DTO\Output\OutputDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\DTO\Output\OutputDTO 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\Metrics\MetricsFacade;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\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...
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,
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...
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