MakeFileCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 25
ccs 16
cts 16
cp 1
crap 2
rs 9.7998
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 3
    use ServiceResolverAwareTrait;
24
25 3
    protected function configure(): void
26 3
    {
27 3
        $this->setName('make:file')
28 3
            ->setDescription('Generate a ' . $this->getExpectedFilenames())
29 3
            ->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 9
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35 9
    {
36
        /** @var list<string> $inputFileNames */
37 9
        $inputFileNames = $input->getArgument('filenames');
38 9
39 9
        $filenames = array_map(
40 9
            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 9
44 9
        /** @var string $path */
45 8
        $path = $input->getArgument('path');
46
        $commandArguments = $this->getFacade()->parseArguments($path);
47 8
        $shortName = (bool)$input->getOption('short-name');
48 8
49 8
        foreach ($filenames as $filename) {
50 8
            $absolutePath = $this->getFacade()->generateFileContent(
51 8
                $commandArguments,
52 8
                $filename,
53 8
                $shortName,
54
            );
55
            $output->writeln(sprintf("> Path '%s' created successfully", $absolutePath));
56 8
        }
57
58
        return self::SUCCESS;
59 3
    }
60
61 3
    private function getExpectedFilenames(): string
62
    {
63
        return implode(', ', FilenameSanitizer::EXPECTED_FILENAMES);
64
    }
65
}
66