MakeFileCommand::getExpectedFilenames()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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