Test Failed
Push — main ( 4ff5eb...8c702d )
by mikhail
03:32
created

AnalyzeCommentCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Commands;
6
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use SavinMikhail\CommentsDensity\AnalyzeComments\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...
10
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\ConfigLoader;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...nts\Config\ConfigLoader 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\AnalyzeComments\Config\DTO\ConfigDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...ts\Config\DTO\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\AnalyzeComments\Exception\CommentsDensityException;
13
use SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\ConsoleFormatter;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...matter\ConsoleFormatter 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\AnalyzeComments\Formatter\FormatterFactory;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...matter\FormatterFactory 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 SavinMikhail\CommentsDensity\AnalyzeComments\Formatter\HtmlFormatter;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...Formatter\HtmlFormatter 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...
16
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage;
17
use SplFileInfo;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
final class AnalyzeCommentCommand extends Command
23
{
24
    public function __construct(
25
        private readonly ConfigLoader           $configLoader = new ConfigLoader(),
26
        private readonly TreePhpBaselineStorage $storage = new TreePhpBaselineStorage(),
27
        private readonly AnalyzerFactory        $analyzerFactory = new AnalyzerFactory(),
28
        ?string                                 $name = null,
29
    )
30
    {
31
        parent::__construct($name);
32
    }
33
34
    protected function configure(): void
35
    {
36
        $this->setName('analyze')
37
            ->setDescription('Analyzes the comment density in files within a directory.')
38
            ->setHelp('This command allows you to analyze the comments in PHP files within a specified directory.');
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output): int
42
    {
43
        $path = $this->configLoader->getProjectRoot() . '/baseline.php';
44
45
        $this->storage->init($path);
46
47
        $configDto = $this->configLoader->getConfigDto();
48
        $files = $this->getFilesFromDirectories($configDto->directories);
49
50
        $formatters = ['console' => new ConsoleFormatter($output)];
51
        if ($configDto->output->type === 'html') {
52
            $formatters['html'] = new HtmlFormatter($configDto->output->file);
53
        }
54
        $formatter = $formatters[$configDto->output->type] ?? $formatters['console'];
55
56
        $analyzer = $this->analyzerFactory->getAnalyzer($configDto, $this->storage);
57
58
        $report = $analyzer->analyze($files);
59
60
        $formatter->report($report);
61
62
        if ($report->exceedThreshold) {
63
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
64
65
            return Command::FAILURE;
66
        }
67
        $output->writeln('<info>Comment thresholds are passed!</info>');
68
69
        return Command::SUCCESS;
70
    }
71
72
    /**
73
     * @param string[] $directories
74
     * @return SplFileInfo[]
75
     */
76
    protected function getFilesFromDirectories(array $directories): iterable
77
    {
78
        foreach ($directories as $directory) {
79
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
80
            foreach ($iterator as $file) {
81
                yield $file;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $file returns the type Generator which is incompatible with the documented return type SplFileInfo[].
Loading history...
82
            }
83
        }
84
    }
85
}
86