Completed
Pull Request — master (#20)
by Pavel
03:20
created

GenerateModelCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 7
dl 18
loc 99
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generatePhpDoc() 9 9 2
A generateFillable() 9 9 2
A configure() 0 7 1
B execute() 0 46 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
namespace App\Console\Commands;
4
5
use Illuminate\Database\Capsule\Manager as Capsule;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\Question;
10
use App\Common\Helper;
11
use App\Console\Traits\CodeGenerate;
12
13
/**
14
 * GenerateModelCommand
15
 */
16
class GenerateModelCommand extends Command
17
{
18
    use CodeGenerate;
19
20
    /**
21
     * Configuration of command
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('generate:model')
27
            ->setDescription('Command for generate model')
28
        ;
29
    }
30
31
    /**
32
     * Execute method of command
33
     *
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     *
37
     * @return void
38
     * @throws \Exception
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $output->writeln(['<comment>Welcome to the model generator</comment>']);
43
44
        $helper    = $this->getHelper('question');
45
        $question  = new Question('<info>Please enter table name: </info>');
46
        $tableName = $helper->ask($input, $output, $question);
47
48
        $table = Capsule::schema()->getColumnListing($tableName);
49
        if (count($table) === 0) {
50
            $output->writeln([sprintf('<comment>Not found table %s</comment>', $tableName)]);
51
        }
52
53
        $columns = [];
54
        foreach ($table as $columnName) {
55
            $columnType = Capsule::schema()->getColumnType($tableName, $columnName);
56
            $columns[] = [
57
                'name' => $columnName,
58
                'type' => $columnType !== 'datetime' ? $columnType : '\Carbon\Carbon',
59
            ];
60
        }
61
62
        $modelName = substr($tableName, 0, -1);
63
        $className = Helper::underscoreToCamelCase($modelName, true);
64
        $baseName  = $className.'.php';
65
        $path      = $this->getPath($baseName, MODELS_PATH);
66
67
        $placeHolders = [
68
            '<class>',
69
            '<tableName>',
70
            '<phpdoc>',
71
            '<fillable>',
72
        ];
73
        $replacements = [
74
            $className,
75
            strtolower($tableName),
76
            $this->generatePhpDoc($columns),
77
            $this->generateFillable($columns),
78
        ];
79
80
        $this->generateCode($placeHolders, $replacements, 'ModelTemplate.tpl', $path);
81
82
        $output->writeln(sprintf('Generated new model class to "<info>%s</info>"', realpath($path)));
83
84
        return;
85
    }
86
87
    /**
88
     * @param array $columns
89
     * @return string
90
     */
91 View Code Duplication
    private function generatePhpDoc($columns)
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...
92
    {
93
        $phpdoc = [];
94
        foreach ($columns as $column) {
95
            $phpdoc[] = sprintf(" * @property %s\t$%s", $column['type'], $column['name']);
96
        };
97
98
        return implode("\n", $phpdoc);
99
    }
100
101
    /**
102
     * @param array $columns
103
     * @return string
104
     */
105 View Code Duplication
    private function generateFillable($columns)
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
        $fillable = [];
108
        foreach ($columns as $column) {
109
            $fillable[] = sprintf("\t\t'%s',", $column['name']);
110
        };
111
112
        return implode("\n", $fillable);
113
    }
114
}
115