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

BaselineCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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