Passed
Push — main ( fcc13f...628042 )
by mikhail
02:55
created

AnalyzeCommentCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 78.95%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 46
c 2
b 0
f 0
dl 0
loc 89
ccs 45
cts 57
cp 0.7895
rs 10

7 Methods

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