Test Failed
Push — main ( 27d416...4ff5eb )
by mikhail
03:21
created

AnalyzeCommentCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A getFilesFromDirectories() 0 6 3
A execute() 0 28 2
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\Reporters\ReporterFactory;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...porters\ReporterFactory 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\Baseline\Storage\TreePhpBaselineStorage;
15
use SplFileInfo;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
final class AnalyzeCommentCommand extends Command
21
{
22
    protected function configure(): void
23
    {
24
        $this->setName('analyze:comments')
25
            ->setDescription('Analyzes the comment density in files within a directory.')
26
            ->setHelp('This command allows you to analyze the comments in PHP files within a specified directory.');
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output): int
30
    {
31
        $configLoader = new ConfigLoader();
32
        $path = $configLoader->getProjectRoot() . '/baseline.php';
33
34
        $storage = new TreePhpBaselineStorage();
35
        $storage->init($path);
36
37
        $configDto = $configLoader->getConfigDto();
38
        $files = $this->getFilesFromDirectories($configDto->directories);
39
40
        $reporter = (new ReporterFactory())->createReporter($output, $configDto);
41
        $analyzerFactory = new AnalyzerFactory();
42
43
        $analyzer = $analyzerFactory->getAnalyzer($configDto, $output, $storage);
44
45
        $outputDTO = $analyzer->analyze($files);
46
47
        $reporter->report($outputDTO);
48
49
        if ($outputDTO->exceedThreshold) {
50
            $output->writeln('<error>Comment thresholds were exceeded!</error>');
51
52
            return Command::FAILURE;
53
        }
54
        $output->writeln('<info>Comment thresholds are passed!</info>');
55
56
        return Command::SUCCESS;
57
    }
58
59
    /**
60
     * @param string[] $directories
61
     * @return SplFileInfo[]
62
     */
63
    protected function getFilesFromDirectories(array $directories): iterable
64
    {
65
        foreach ($directories as $directory) {
66
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
67
            foreach ($iterator as $file) {
68
                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...
69
            }
70
        }
71
    }
72
}
73