GenerateModelCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 25
loc 100
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A generatePhpDoc() 9 9 2
A generateFillable() 9 9 2
B execute() 7 47 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
            return;
52
        }
53
54
        $columns = [];
55 View Code Duplication
        foreach ($table as $columnName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
            $columnType = Capsule::schema()->getColumnType($tableName, $columnName);
57
            $columns[] = [
58
                'name' => $columnName,
59
                'type' => $columnType !== 'datetime' ? $columnType : '\Carbon\Carbon',
60
            ];
61
        }
62
63
        $modelName = substr($tableName, 0, -1);
64
        $className = Helper::underscoreToCamelCase($modelName, true);
65
        $baseName  = $className.'.php';
66
        $path      = $this->getPath($baseName, MODELS_PATH);
67
68
        $placeHolders = [
69
            '<class>',
70
            '<tableName>',
71
            '<phpdoc>',
72
            '<fillable>',
73
        ];
74
        $replacements = [
75
            $className,
76
            strtolower($tableName),
77
            $this->generatePhpDoc($columns),
78
            $this->generateFillable($columns),
79
        ];
80
81
        $this->generateCode($placeHolders, $replacements, 'ModelTemplate.tpl', $path);
82
83
        $output->writeln(sprintf('Generated new model class to "<info>%s</info>"', realpath($path)));
84
85
        return;
86
    }
87
88
    /**
89
     * @param array $columns
90
     * @return string
91
     */
92 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...
93
    {
94
        $phpdoc = [];
95
        foreach ($columns as $column) {
96
            $phpdoc[] = sprintf(" * @property %s $%s", $column['type'], $column['name']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal * @property %s $%s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
97
        };
98
99
        return implode("\n", $phpdoc);
100
    }
101
102
    /**
103
     * @param array $columns
104
     * @return string
105
     */
106 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...
107
    {
108
        $fillable = [];
109
        foreach ($columns as $column) {
110
            $fillable[] = sprintf("        '%s',", $column['name']);
111
        };
112
113
        return implode("\n", $fillable);
114
    }
115
}
116