Completed
Pull Request — master (#38)
by Anton
12:38
created

ModuleCommand::verify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 11
cts 11
cp 1
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Generate;
8
9
use Bluzman\Generator\GeneratorException;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Generate Module Structure
15
 *
16
 * @package  Bluzman\Command\Generate
17
 */
18
class ModuleCommand extends AbstractGenerateCommand
19
{
20
    /**
21
     * Command configuration
22
     */
23 14
    protected function configure()
24
    {
25
        $this
26
            // the name of the command (the part after "bin/bluzman")
27 14
            ->setName('generate:module')
28
            // the short description shown while running "php bin/bluzman list"
29 14
            ->setDescription('Generate a new module')
30
            // the full command description shown when running the command with
31
            // the "--help" option
32 14
            ->setHelp('This command allows you to generate a module structure')
33
        ;
34
35 14
        $this->addModuleArgument();
36 14
        $this->addForceOption();
37 14
    }
38
39
    /**
40
     * @param InputInterface $input
41
     * @param OutputInterface $output
42
     * @return void
43
     */
44 1 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46 1
        $this->write('Running <info>generate:module</info> command');
47
        try {
48
            // validate
49 1
            $this->validateModuleArgument();
50
51
            // create main folder and subfolders
52 1
            $this->generate($input, $output);
53
54
            // verify it
55 1
            $this->verify($input, $output);
56
        } catch (\Exception $e) {
57
            $this->error("ERROR: {$e->getMessage()}");
58
        }
59 1
    }
60
61
    /**
62
     * @param InputInterface $input
63
     * @param OutputInterface $output
64
     * @return void
65
     */
66 1
    protected function generate(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68 1
        $path = $this->getApplication()->getModulePath($input->getArgument('module'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('module') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Bluzman\Application\Application::getModulePath() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
69 1
        $this->createSubFolders(
70 1
            $path,
71
            [
72 1
                'controllers',
73
                'views'
74
            ]
75
        );
76 1
    }
77
78
    /**
79
     * @param string $path
80
     * @param string[] $subFolders
81
     */
82 1
    protected function createSubFolders($path, array $subFolders = [])
83
    {
84 1
        if (!$this->getFs()->exists($path)) {
85 1
            $this->getFs()->mkdir($path);
86
        }
87
88 1
        foreach ($subFolders as $subFolderName) {
89 1
            $subFolderPath = $path . DIRECTORY_SEPARATOR . $subFolderName;
90 1
            if ($this->getFs()->exists($subFolderPath)) {
91
                $this->comment(" |> Directory <info>$subFolderPath</info> already exists");
92
            } else {
93 1
                $this->getFs()->mkdir($subFolderPath, 0755);
94 1
                $this->getFs()->touch([$subFolderPath . DIRECTORY_SEPARATOR . '.keep']);
0 ignored issues
show
Documentation introduced by
array($subFolderPath . D...RY_SEPARATOR . '.keep') is of type array<integer,string,{"0":"string"}>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
            }
96
        }
97 1
    }
98
99
    /**
100
     * @param InputInterface $input
101
     * @param OutputInterface $output
102
     * @return void
103
     * @throws GeneratorException
104
     */
105 1 View Code Duplication
    public function verify(InputInterface $input, OutputInterface $output) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107 1
        $module = $input->getArgument('module');
108 1
        $modulePath = $this->getApplication()->getModulePath($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by $input->getArgument('module') on line 107 can also be of type array<integer,string> or null; however, Bluzman\Application\Application::getModulePath() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109
110
        $paths = [
111 1
            $modulePath,
112 1
            $modulePath . DS . 'controllers',
113 1
            $modulePath . DS . 'views'
114
        ];
115
116 1
        foreach ($paths as $path) {
117 1
            if (!$this->getFs()->exists($path)) {
118 1
                throw new GeneratorException("Directory `$path` is not exists");
119
            }
120
        }
121
122 1
        $this->write(" |> Module <info>$module</info> has been successfully created.");
123 1
    }
124
}
125