Passed
Push — main ( fefea7...05b26b )
by mikhail
08:12
created

AnalyzeFilesCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 16
c 4
b 2
f 0
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 9.7333
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Commands;
6
7
use Generator;
8
use SavinMikhail\CommentsDensity\Analyzer\AnalyzerFactory;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...nalyzer\AnalyzerFactory 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...
9
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage;
10
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...
11
use SplFileInfo;
12
use Symfony\Component\Console\Command\Command as SymfonyCommand;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class AnalyzeFilesCommand extends Command
18
{
19 2
    protected function configure(): void
20
    {
21 2
        $this->setName('analyze:files')
22 2
            ->setDescription('Analyzes the comment density in multiple PHP files.')
23 2
            ->addArgument('files', InputArgument::IS_ARRAY, 'The PHP files to analyze')
24 2
            ->setHelp('This command allows you to analyze the comments in multiple PHP files.');
25
    }
26
27 2
    protected function execute(InputInterface $input, OutputInterface $output): int
28
    {
29 2
        $path = __DIR__ . '/../../baseline.php';
30
31 2
        $storage = new TreePhpBaselineStorage();
32 2
        $storage->init($path);
33
34 2
        $configDto = $this->getConfigDto();
35
36 2
        $filePaths = $input->getArgument('files');
37
38 2
        $files = $this->generateFiles($filePaths);
39
40 2
        $analyzerFactory = new AnalyzerFactory();
41 2
        $analyzer = $analyzerFactory->getAnalyzer($configDto, $output, $storage);
42 2
        $outputDTO = $analyzer->analyze($files);
43
44 2
        $reporter = new ConsoleReporter($output);
45 2
        $reporter->report($outputDTO);
46
47 2
        if ($outputDTO->exceedThreshold) {
48 1
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
49 1
            return SymfonyCommand::FAILURE;
50
        }
51
52 1
        $output->writeln('<info>Comment thresholds are passed!</info>');
53 1
        return SymfonyCommand::SUCCESS;
54
    }
55
56
    /**
57
     * @param string[] $filePaths
58
     * @return Generator
59
     */
60 2
    protected function generateFiles(array $filePaths): Generator
61
    {
62 2
        foreach ($filePaths as $filePath) {
63 2
            yield new SplFileInfo($filePath);
64
        }
65
    }
66
}
67