Passed
Push — main ( 311549...7ac0b4 )
by mikhail
03:56
created

Analyzer::createOutputDTO()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

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