MakeFileCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 23
c 0
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 26 2
A configure() 0 8 1
A getExpectedFilenames() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Infrastructure\Command;
6
7
use Gacela\Console\ConsoleFacade;
8
use Gacela\Console\Domain\FilenameSanitizer\FilenameSanitizer;
9
use Gacela\Framework\DocBlockResolverAwareTrait;
10
use Override;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
use function sprintf;
18
19
/**
20
 * @method ConsoleFacade getFacade()
21
 */
22
final class MakeFileCommand extends Command
23
{
24
    use DocBlockResolverAwareTrait;
25
26
    #[Override]
27
    protected function configure(): void
28
    {
29
        $this->setName('make:file')
30
            ->setDescription('Generate a ' . $this->getExpectedFilenames())
31
            ->addArgument('path', InputArgument::REQUIRED, 'The file path. For example "App/TestModule/TestSubModule"')
32
            ->addArgument('filenames', InputArgument::REQUIRED | InputArgument::IS_ARRAY, $this->getExpectedFilenames())
33
            ->addOption('short-name', 's', InputOption::VALUE_NONE, 'Remove module prefix to the class name');
34
    }
35
36
    #[Override]
37
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39
        /** @var list<string> $inputFileNames */
40
        $inputFileNames = $input->getArgument('filenames');
41
42
        $filenames = array_map(
43
            fn (string $raw): string => $this->getFacade()->sanitizeFilename($raw),
44
            $inputFileNames,
0 ignored issues
show
Bug introduced by
$inputFileNames of type Gacela\Console\Infrastructure\Command\list is incompatible with the type array expected by parameter $array of array_map(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            /** @scrutinizer ignore-type */ $inputFileNames,
Loading history...
45
        );
46
47
        /** @var string $path */
48
        $path = $input->getArgument('path');
49
        $commandArguments = $this->getFacade()->parseArguments($path);
50
        $shortName = (bool)$input->getOption('short-name');
51
52
        foreach ($filenames as $filename) {
53
            $absolutePath = $this->getFacade()->generateFileContent(
54
                $commandArguments,
55
                $filename,
56
                $shortName,
57
            );
58
            $output->writeln(sprintf("> Path '%s' created successfully", $absolutePath));
59
        }
60
61
        return self::SUCCESS;
62
    }
63
64
    private function getExpectedFilenames(): string
65
    {
66
        return implode(', ', FilenameSanitizer::EXPECTED_FILENAMES);
67
    }
68
}
69