Passed
Pull Request — master (#35)
by Anton
03:28
created

ModuleCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 33.02 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 90.24%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 35
loc 106
ccs 37
cts 41
cp 0.9024
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 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
    }
37
38
    /**
39
     * @param InputInterface $input
40
     * @param OutputInterface $output
41
     * @return void
42
     */
43 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...
44
    {
45 1
        $this->write('Running <info>generate:module</info> command');
46
        try {
47
            // validate
48 1
            $this->validateModuleArgument();
49
50
            // create main folder and subfolders
51 1
            $this->generate($input, $output);
52
53
            // verify it
54 1
            $this->verify($input, $output);
55
        } catch (\Exception $e) {
56
            $this->error("ERROR: {$e->getMessage()}");
57
        }
58 1
    }
59
60
    /**
61
     * @param InputInterface $input
62
     * @param OutputInterface $output
63
     * @return void
64
     */
65 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...
66
    {
67 1
        $path = $this->getApplication()->getModulePath($input->getArgument('module'));
68 1
        $this->createSubFolders(
69 1
            $path,
70
            [
71 1
                'controllers',
72
                'views'
73
            ]
74
        );
75 1
    }
76
77
    /**
78
     * @param string $path
79
     * @param string[] $subFolders
80
     */
81 1
    protected function createSubFolders($path, array $subFolders = [])
82
    {
83 1
        if (!$this->getFs()->exists($path)) {
84 1
            $this->getFs()->mkdir($path);
85
        }
86
87 1
        foreach ($subFolders as $subFolderName) {
88 1
            $subFolderPath = $path . DIRECTORY_SEPARATOR . $subFolderName;
89 1
            if ($this->getFs()->exists($subFolderPath)) {
90
                $this->comment(" |> Directory <info>$subFolderPath</info> already exists");
91
            } else {
92 1
                $this->getFs()->mkdir($subFolderPath, 0755);
93 1
                $this->getFs()->touch([$subFolderPath . DIRECTORY_SEPARATOR . '.keep']);
94
            }
95
        }
96 1
    }
97
98
    /**
99
     * @param InputInterface $input
100
     * @param OutputInterface $output
101
     * @return void
102
     * @throws GeneratorException
103
     */
104 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...
105
    {
106 1
        $module = $input->getArgument('module');
107 1
        $modulePath = $this->getApplication()->getModulePath($module);
108
109
        $paths = [
110 1
            $modulePath,
111 1
            $modulePath . DS . 'controllers',
112 1
            $modulePath . DS . 'views'
113
        ];
114
115 1
        foreach ($paths as $path) {
116 1
            if (!$this->getFs()->exists($path)) {
117
                throw new GeneratorException("Directory `$path` is not exists");
118
            }
119
        }
120
121 1
        $this->write(" |> Module <info>$module</info> has been successfully created.");
122 1
    }
123
}
124