Passed
Push — main ( 8b63c1...8805f7 )
by mikhail
03:12
created

AnalyzeFileCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 74
ccs 44
cts 52
cp 0.8462
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A analyzeFile() 0 11 2
A configure() 0 6 1
A parseConfigFile() 0 4 1
A getProjectRoot() 0 3 1
A execute() 0 37 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\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...
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Yaml\Parser;
22
23
use function dirname;
24
25
class AnalyzeFileCommand extends Command
26
{
27 2
    protected function configure(): void
28
    {
29 2
        $this->setName('analyze:file')
30 2
            ->setDescription('Analyzes the comment density in a single PHP file.')
31 2
            ->addArgument('file', InputArgument::REQUIRED, 'The PHP file to analyze')
32 2
            ->setHelp('This command allows you to analyze the comments in a single PHP file.');
33
    }
34
35
    /**
36
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
37
     */
38 2
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40 2
        $configFile = $this->getProjectRoot() . '/comments_density.yaml';
41 2
        $config = $this->parseConfigFile($configFile);
42
43 2
        $thresholds = $config['thresholds'];
44 2
        $outputConfig = $config['output'] ?? [];
45 2
        $file = $input->getArgument('file');
46 2
        $missingDocBlock = new MissingDocBlockAnalyzer();
47 2
        $configDto = new ConfigDTO(
48 2
            $thresholds,
49 2
            [],
50 2
            $outputConfig
51 2
        );
52 2
        $commentFactory = new CommentFactory();
53 2
        $cds = new CDS($configDto->thresholds, $commentFactory);
54 2
        $fileAnalyzer = new FileAnalyzer(
55 2
            $output,
56 2
            $missingDocBlock,
57 2
            $cds,
58 2
            $commentFactory
59 2
        );
60 2
        $metrics = new Metrics(
61 2
            $cds,
62 2
            new ComToLoc($configDto->thresholds),
63 2
            new PerformanceMonitor(),
64 2
        );
65 2
        $analyzer = new CommentDensity(
66 2
            $configDto,
67 2
            $commentFactory,
68 2
            $fileAnalyzer,
69 2
            new ConsoleReporter($output),
70 2
            $missingDocBlock,
71 2
            $metrics
72 2
        );
73
74 2
        return $this->analyzeFile($analyzer, $file, $output);
75
    }
76
77 2
    protected function analyzeFile(CommentDensity $analyzer, string $file, OutputInterface $output): int
78
    {
79 2
        $limitExceeded = $analyzer->analyzeFile($file);
80
81 2
        if ($limitExceeded) {
82
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
83
            $output->writeln('<info>To skip commit checks, add -n or --no-verify flag to commit command</info>');
84
            return Command::FAILURE;
85
        }
86 2
        $output->writeln('<info>Comment thresholds are passed!</info>');
87 2
        return Command::SUCCESS;
88
    }
89
90
    protected function getProjectRoot(): string
91
    {
92
        return dirname(__DIR__, 4);
93
    }
94
95
    protected function parseConfigFile(string $configFile): array
96
    {
97
        $yamlParser = new Parser();
98
        return $yamlParser->parseFile($configFile);
99
    }
100
}
101