Passed
Push — main ( 6122b5...b860fb )
by mikhail
02:53
created

AnalyzeCommentCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 57
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 40 2
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\CommentDensity;
8
use SavinMikhail\CommentsDensity\Comments\CommentFactory;
9
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...
10
use SavinMikhail\CommentsDensity\MissingDocBlockAnalyzer;
11
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...
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Yaml\Parser;
16
17
class AnalyzeCommentCommand extends Command
18
{
19
    protected function configure(): void
20
    {
21
        $this->setName('analyze:comments')
22
            ->setDescription('Analyzes the comment density in files within a directory.')
23
            ->setHelp('This command allows you to analyze the comments in PHP files within a specified directory.');
24
    }
25
26
    /**
27
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
28
     */
29
    protected function execute(InputInterface $input, OutputInterface $output): int
30
    {
31
        $configFile = $this->getProjectRoot() . '/comments_density.yaml';
32
33
        $yamlParser = new Parser();
34
        $config = $yamlParser->parseFile($configFile);
35
36
        $directories = array_map(
37
            fn($dir) => $this->getProjectRoot() . '/' . $dir,
38
            $config['directories'] ?? [$this->getProjectRoot() . '/' . $config['directory']]
39
        );
40
        $exclude = array_map(
41
            fn($dir) => $this->getProjectRoot() . '/' . $dir,
42
            $config['exclude'] ?? [$this->getProjectRoot() . '/' . $config['exclude']]
43
        );
44
        $thresholds = $config['thresholds'];
45
        $outputConfig = $config['output'] ?? [];
46
47
        $commentFactory = new CommentFactory();
48
        $analyzer = new CommentDensity(
49
            $output,
50
            $thresholds,
51
            $exclude,
52
            $outputConfig,
53
            $commentFactory,
54
            new FileAnalyzer(
55
                $output,
56
                new MissingDocBlockAnalyzer(),
57
                new StatisticCalculator($commentFactory),
58
                $commentFactory
59
            )
60
        );
61
        $limitExceeded = $analyzer->analyzeDirectories($directories);
62
63
        if ($limitExceeded) {
64
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
65
            return Command::FAILURE;
66
        }
67
        $output->writeln('<info>Comment thresholds are passed!</info>');
68
        return Command::SUCCESS;
69
    }
70
71
    private function getProjectRoot(): string
72
    {
73
        return dirname(__DIR__, 4);
74
    }
75
}
76