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

BaselineCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Baseline\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\Baseline\Storage\TreePhpBaselineStorage;
14
use SplFileInfo;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
final class BaselineCommand extends Command
20
{
21
    protected function configure(): void
22
    {
23
        $this->setName('generate:baseline')
24
            ->setDescription('Generate a baseline of comments to ignore them in the future')
25
            ->setHelp('This command allows you to ignore old tech debt and start this quality check from this point');
26
    }
27
28
    protected function execute(InputInterface $input, OutputInterface $output): int
29
    {
30
        $configLoader = new ConfigLoader();
31
        $path = $configLoader->getProjectRoot() . '/baseline.php';
32
33
        $storage = new TreePhpBaselineStorage();
34
35
        $storage->init($path);
36
37
        $configDto = $configLoader->getConfigDto();
38
        $files = $this->getFilesFromDirectories($configDto->directories);
39
40
        $analyzerFactory = new AnalyzerFactory();
41
42
        $analyzer = $analyzerFactory->getAnalyzer($configDto, $output, $storage);
43
        $outputDTO = $analyzer->analyze($files);
44
45
        $storage->setComments($outputDTO->comments);
46
47
        $output->writeln('<info>Baseline generated successfully!</info>');
48
49
        return Command::SUCCESS;
50
    }
51
52
    /**
53
     * @param string[] $directories
54
     * @return SplFileInfo[]
55
     */
56
    protected function getFilesFromDirectories(array $directories): iterable
57
    {
58
        foreach ($directories as $directory) {
59
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
60
            foreach ($iterator as $file) {
61
                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...
62
            }
63
        }
64
    }
65
}
66