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

AnalyzeFileCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
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\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\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Yaml\Parser;
17
18
class AnalyzeFileCommand extends Command
19
{
20
    protected function configure(): void
21
    {
22
        $this->setName('analyze:file')
23
            ->setDescription('Analyzes the comment density in a single PHP file.')
24
            ->addArgument('file', InputArgument::REQUIRED, 'The PHP file to analyze')
25
            ->setHelp('This command allows you to analyze the comments in a single PHP file.');
26
    }
27
28
    /**
29
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output): int
32
    {
33
        $configFile = $this->getProjectRoot() . '/comments_density.yaml';
34
35
        $yamlParser = new Parser();
36
        $config = $yamlParser->parseFile($configFile);
37
38
        $thresholds = $config['thresholds'];
39
        $outputConfig = $config['output'] ?? [];
40
        $file = $input->getArgument('file');
41
42
        $commentFactory = new CommentFactory();
43
        $analyzer = new CommentDensity(
44
            $output,
45
            $thresholds,
46
            [],
47
            $outputConfig,
48
            $commentFactory,
49
            new FileAnalyzer(
50
                $output,
51
                new MissingDocBlockAnalyzer(),
52
                new StatisticCalculator($commentFactory),
53
                $commentFactory
54
            )
55
        );
56
57
        $limitExceeded = $analyzer->analyzeFile($file);
58
59
        if ($limitExceeded) {
60
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
61
            $output->writeln('<info>To skip commit checks, add -n or --no-verify flag to commit command</info>');
62
            return Command::FAILURE;
63
        }
64
        $output->writeln('<info>Comment thresholds are passed!</info>');
65
        return Command::SUCCESS;
66
    }
67
68
    private function getProjectRoot(): string
69
    {
70
        return dirname(__DIR__, 4);
71
    }
72
}
73