Passed
Push — main ( 5e6da4...b963fe )
by mikhail
02:59
created

AnalyzeCommentCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 37
ccs 0
cts 31
cp 0
rs 9.488
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Commands;
6
7
use SavinMikhail\CommentsDensity\CDS;
8
use SavinMikhail\CommentsDensity\CommentDensity;
9
use SavinMikhail\CommentsDensity\Comments\CommentFactory;
10
use SavinMikhail\CommentsDensity\ComToLoc;
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\FileAnalyzer;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\FileAnalyzer 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\MissingDocBlockAnalyzer;
14
use SavinMikhail\CommentsDensity\Reporters\ConsoleReporter;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...porters\ConsoleReporter 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\Reporters\HtmlReporter;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...\Reporters\HtmlReporter 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\Reporters\ReporterInterface;
17
use SavinMikhail\CommentsDensity\StatisticCalculator;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\StatisticCalculator 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...
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Yaml\Parser;
22
23
class AnalyzeCommentCommand extends Command
24
{
25
    protected function configure(): void
26
    {
27
        $this->setName('analyze:comments')
28
            ->setDescription('Analyzes the comment density in files within a directory.')
29
            ->setHelp('This command allows you to analyze the comments in PHP files within a specified directory.');
30
    }
31
32
    /**
33
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output): int
36
    {
37
        $configFile = $this->getProjectRoot() . '/comments_density.yaml';
38
        $config = $this->parseConfigFile($configFile);
39
40
        $directories = $this->getDirectories($config);
41
        $exclude = $this->getExcludes($config);
42
        $thresholds = $config['thresholds'] ?? [];
43
        $outputConfig = $config['output'] ?? [];
44
45
        $configDto = new ConfigDTO(
46
            $thresholds,
47
            $exclude,
48
            $outputConfig
49
        );
50
51
        $reporter = $this->getReporter($output, $configDto);
52
        $commentFactory = new CommentFactory();
53
        $missingDocBlock = new MissingDocBlockAnalyzer();
54
        $fileAnalyzer = new FileAnalyzer(
55
            $output,
56
            $missingDocBlock,
57
            new StatisticCalculator($commentFactory),
58
            $commentFactory
59
        );
60
61
        $analyzer = new CommentDensity(
62
            $configDto,
63
            $commentFactory,
64
            $fileAnalyzer,
65
            $reporter,
66
            new CDS($configDto->thresholds),
67
            new ComToLoc($configDto->thresholds),
68
            $missingDocBlock
69
        );
70
71
        return $this->analyze($analyzer, $directories, $output);
72
    }
73
74
    private function parseConfigFile(string $configFile): array
75
    {
76
        $yamlParser = new Parser();
77
        return $yamlParser->parseFile($configFile);
78
    }
79
80
    private function getDirectories(array $config): array
81
    {
82
        return array_map(
83
            fn($dir) => $this->getProjectRoot() . '/' . $dir,
84
            $config['directories']
85
        );
86
    }
87
88
    private function getExcludes(array $config): array
89
    {
90
        return array_map(
91
            fn($dir) => $this->getProjectRoot() . '/' . $dir,
92
            $config['exclude']
93
        );
94
    }
95
96
    private function getReporter(OutputInterface $output, ConfigDTO $configDto): ReporterInterface
97
    {
98
        if (!empty($configDto->outputConfig) && $configDto->outputConfig['type'] === 'html') {
99
            return new HtmlReporter(__DIR__ . '/../../../' . $configDto->outputConfig['file']);
100
        }
101
        return new ConsoleReporter($output);
102
    }
103
104
    private function analyze(CommentDensity $analyzer, array $directories, OutputInterface $output): int
105
    {
106
        $limitExceeded = $analyzer->analyzeDirectories($directories);
107
108
        if ($limitExceeded) {
109
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
110
            return Command::FAILURE;
111
        }
112
113
        $output->writeln('<info>Comment thresholds are passed!</info>');
114
        return Command::SUCCESS;
115
    }
116
117
    private function getProjectRoot(): string
118
    {
119
        return dirname(__DIR__, 2);
120
    }
121
}
122