Passed
Push — feature/refactor-code-generato... ( fb1bf5 )
by Chema
04:20
created

MakeFileCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 6
cts 6
cp 1
cc 1
nc 1
nop 0
crap 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 Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * @method ConsoleFacade getFacade()
18
 */
19
final class MakeFileCommand extends Command
20
{
21
    use DocBlockResolverAwareTrait;
22
23 8
    protected function configure(): void
24
    {
25 8
        $this->setName('make:file')
26 8
            ->setDescription('Generate a ' . $this->getExpectedFilenames())
27 8
            ->addArgument('path', InputArgument::REQUIRED, 'The file path. For example "App/TestModule/TestSubModule"')
28 8
            ->addArgument('filenames', InputArgument::REQUIRED | InputArgument::IS_ARRAY, $this->getExpectedFilenames())
29 8
            ->addOption('short-name', 's', InputOption::VALUE_NONE, 'Remove module prefix to the class name');
30
    }
31
32 8
    protected function execute(InputInterface $input, OutputInterface $output): int
33
    {
34
        /** @var list<string> $inputFileNames */
35 8
        $inputFileNames = $input->getArgument('filenames');
36
37 8
        $filenames = array_map(
38 8
            fn (string $raw): string => $this->getFacade()->sanitizeFilename($raw),
39
            $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

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