Passed
Push — main ( b963fe...fcc13f )
by mikhail
03:11
created

AnalyzeCommentCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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