Passed
Push — master ( 6e1659...b44661 )
by Anton
01:26
created

ModuleCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 32.71 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 35
loc 107
ccs 38
cts 42
cp 0.9048
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 15 1
A execute() 16 16 2
A generate() 0 11 1
A createSubFolders() 0 16 4
A verify() 19 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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'));
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);
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
                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