Test Failed
Push — main ( 27d416...4ff5eb )
by mikhail
03:21
created

Analyzer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 9
dl 0
loc 11
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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;
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...
8
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...
9
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\OutputDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...er\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...
10
use SavinMikhail\CommentsDensity\AnalyzeComments\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\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...
12
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...
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,
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
     */
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(
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...
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