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

CrudCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 33.87 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 71.15%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 7
dl 42
loc 124
ccs 37
cts 52
cp 0.7115
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
A execute() 17 17 2
B generate() 0 38 3
B verify() 25 25 4

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\Input\InputArgument;
10
use Bluzman\Input\InputException;
11
use Bluzman\Generator;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * ModelCommand
17
 *
18
 * @package  Bluzman\Command\Generate
19
 */
20
class CrudCommand extends AbstractGenerateCommand
21
{
22
    /**
23
     * Command configuration
24
     */
25 14
    protected function configure()
26
    {
27
        $this
28
            // the name of the command (the part after "bin/bluzman")
29 14
            ->setName('generate:crud')
30
            // the short description shown while running "php bin/bluzman list"
31 14
            ->setDescription('Generate a CRUD for model')
32
            // the full command description shown when running the command with
33
            // the "--help" option
34 14
            ->setHelp('This command allows you to generate CRUD files')
35
        ;
36
37 14
        $this->addModelArgument();
38 14
        $this->addModuleArgument(InputArgument::OPTIONAL);
39 14
        $this->addForceOption();
40 14
    }
41
42
    /**
43
     * @param InputInterface  $input
44
     * @param OutputInterface $output
45
     *
46
     * @return void
47
     * @throws \Bluzman\Generator\GeneratorException
48
     */
49 3 View Code Duplication
    protected function execute(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...
50
    {
51 3
        $this->write('Running <info>generate:crud</info> command');
52
        try {
53
            // validate
54 3
            $this->validateModelArgument();
55 1
            $this->validateModuleArgument();
56
57
            // generate directories and files
58 1
            $this->generate($input, $output);
59
60
            // verify it
61 1
            $this->verify($input, $output);
62 2
        } catch (InputException $e) {
63 2
            $this->error("ERROR: {$e->getMessage()}");
64
        }
65 3
    }
66
67
    /**
68
     * @param InputInterface $input
69
     * @param OutputInterface $output
70
     * @return void
71
     * @throws InputException
72
     */
73 1
    protected function generate(InputInterface $input, OutputInterface $output) : void
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...
74
    {
75 1
        $model = ucfirst($input->getArgument('model'));
76 1
        $module = $input->getArgument('module');
77
78
        // template data
79
        $data = [
80 1
            'model' => $model,
81 1
            'module' => $module
82
        ];
83
84
        // generate CRUD class
85 1
        $this->write(" |> Generate CRUD class <info>$model\\Crud</info>");
86 1
        $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Crud.php';
87 1
        $this->generateFile('CrudTemplate', $crudFile, $data);
88
89 1
        if ($module) {
90
            if (!$this->getApplication()->isModelExists($model)) {
91
                throw new InputException(
92
                    "Model $model is not exist, " .
93
                    "run command <question>bluzman generate:model $model</question> before"
94
                );
95
            }
96
97
            $this->write(" |> Generate CRUD controller <info>$module/controllers/crud.php</info>");
98
99
            $controllerFile = $this->getControllerPath($module, 'crud');
100
            $this->generateFile('CrudControllerTemplate', $controllerFile, $data);
101
102
            $this->write(" |> Generate CRUD view <info>$module/views/crud.phtml</info>");
103
104
            $tableInstance = $this->getTableInstance($model);
105
            $data['columns'] = $tableInstance::getMeta();
106
107
            $viewFile = $this->getViewPath($module, 'crud');
108
            $this->generateFile('CrudViewTemplate', $viewFile, $data);
109
        }
110 1
    }
111
112
    /**
113
     * @param InputInterface $input
114
     * @param OutputInterface $output
115
     * @return void
116
     * @throws \Bluzman\Generator\GeneratorException
117
     */
118 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...
119
    {
120 1
        $model = $input->getArgument('model');
121 1
        $module = $input->getArgument('module');
122
123 1
        $modelPath = $this->getApplication()->getModelPath($model);
124
125
        $paths = [
126 1
            $modelPath . DS . 'Crud.php',
127
        ];
128
129 1
        foreach ($paths as $path) {
130 1
            if (!$this->getFs()->exists($path)) {
131
                throw new Generator\GeneratorException("File `$path` is not exists");
132
            }
133
        }
134
135 1
        $this->write(" |> CRUD for <info>{$model}</info> has been successfully created.");
136 1
        if ($module) {
137
            $this->write(
138
                " |> <options=bold>Open page <info>/acl</info> in your browser " .
139
                "and set permission <info>Management</info> for <info>{$module}</info> module</>"
140
            );
141
        }
142 1
    }
143
}
144