Passed
Pull Request — master (#1068)
by butschster
23:53 queued 14:16
created

InfoCommand::perform()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 62
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 6.0004

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 62
ccs 42
cts 43
cp 0.9767
rs 8.7057
cc 6
nc 6
nop 3
crap 6.0004

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Scaffolder\Command;
6
7
use Spiral\Boot\DirectoriesInterface;
8
use Spiral\Console\Attribute\Argument;
9
use Spiral\Console\Attribute\AsCommand;
10
use Spiral\Console\Command;
11
use Spiral\Console\Console;
12
use Spiral\Scaffolder\Config\ScaffolderConfig;
13
use Symfony\Component\Console\Helper\TableSeparator;
14
15
#[AsCommand(name: 'scaffolder:info', description: 'Show information about available scaffolder commands.')]
16
final class InfoCommand extends Command
17
{
18
    #[Argument(description: 'Class name')]
19
    private string $name = 'Example';
20
21 1
    public function perform(ScaffolderConfig $config, DirectoriesInterface $dirs, Console $console): int
22
    {
23 1
        $this->output->title('Scaffolder commands');
0 ignored issues
show
Bug introduced by
The method title() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Style\OutputStyle. ( Ignorable by Annotation )

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

23
        $this->output->/** @scrutinizer ignore-call */ 
24
                       title('Scaffolder commands');
Loading history...
Bug introduced by
The method title() does not exist on null. ( Ignorable by Annotation )

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

23
        $this->output->/** @scrutinizer ignore-call */ 
24
                       title('Scaffolder commands');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24 1
        $this->writeln(
25 1
            'Scaffolder enables developers to quickly and easily generate application code for various classes, using a set of console commands',
26 1
        );
27
28 1
        $this->newLine();
29
30 1
        $table = $this->table(['Command', 'Target']);
31 1
        $rootDir = $dirs->get('root');
32 1
        $available = $config->getDeclarations();
33
34 1
        $i = 0;
35 1
        foreach ($available as $name) {
36 1
            $command = 'create:' . $name;
37
38 1
            if (!$console->getApplication()->has($command)) {
39
                continue;
40
            }
41
42 1
            $command = $console->getApplication()->get($command);
43
44 1
            if ($i > 0) {
45 1
                $table->addRow(new TableSeparator());
46
            }
47 1
            $declaration = $config->getDeclaration($name);
48
49 1
            $options = [];
50 1
            foreach ($declaration['options'] ?? [] as $key => $value) {
51 1
                $options[] = $key . ': <fg=yellow>' . \json_encode(\str_replace($rootDir, '', $value)) . '</>';
52
            }
53
54 1
            $file = \str_replace($rootDir, '', $config->classFilename($name, $this->name));
55 1
            $namespace = $config->classNamespace($name, $this->name);
56 1
            $table->addRow([
57 1
                $command->getName() . "\n<fg=gray>{$command->getDescription()}</>",
58 1
                <<<TARGET
59 1
path: <fg=green>/$file</>
60 1
namespace: <fg=yellow>$namespace</>
61 1
TARGET
62 1
                .
63 1
                ($options !== [] ? "\n" . \implode("\n", $options) : ''),
64 1
            ]);
65
66 1
            $i++;
67
        }
68
69 1
        $randomName = $available[\array_rand($available)];
70 1
        $this->writeln(
71 1
            "<info>Use `<fg=yellow>php app.php create:{$randomName} {$this->name}</>` command to generate desired class. Below is a list of available commands:</info>",
72 1
        );
73
74 1
        $table->render();
75
76 1
        $this->writeln(
77 1
            '<info>Use `<fg=yellow>php app.php create:*** --help</>` command to see available options.</info>',
78 1
        );
79
80 1
        $this->writeln('Read more about scaffolder in <fg=yellow>https://spiral.dev/docs/basics-scaffolding</> documentation section.');
81
82 1
        return self::SUCCESS;
83
    }
84
}
85