Passed
Push — main ( d846f8...8b63c1 )
by mikhail
04:31
created

AnalyzeCommentCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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