Completed
Pull Request — master (#2262)
by
unknown
17:07
created

FormCommand   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 180
rs 9.36
c 0
b 0
f 0
wmc 38
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
F handle() 0 128 34
A check_column() 0 9 3
A getOptions() 0 7 1
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class FormCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'admin:form';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'laravel-admin form filed generator';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return void
29
     */
30
    public function handle()
31
    {
32
        $modelName = $this->option('model');
33
        if (empty($modelName) || !class_exists($modelName)) {
34
            $this->error('Model does not exists !');
35
            return false;
36
        }
37
38
        // use doctrine/dbal
39
        $model = $this->laravel->make($modelName);
0 ignored issues
show
Bug introduced by
It seems like $modelName defined by $this->option('model') on line 32 can also be of type array; however, Illuminate\Contracts\Container\Container::make() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
        $table = $model->getConnection()->getTablePrefix() . $model->getTable();
41
        $schema = $model->getConnection()->getDoctrineSchemaManager($table);
42
43
        if (!method_exists($schema, 'getDatabasePlatform')) {
44
            $this->error('You need to require doctrine/dbal: ~2.3 in your own composer.json to get database columns. ');
45
            $this->info('Using install command: composer require doctrine/dbal');
46
            return false;
47
48
        }
49
50
        // custom mapping the types that doctrine/dbal does not support
51
        $databasePlatform = $schema->getDatabasePlatform();
52
        $databasePlatform->registerDoctrineTypeMapping('enum', 'string');
53
        $databasePlatform->registerDoctrineTypeMapping('geometry', 'string');
54
        $databasePlatform->registerDoctrineTypeMapping('geometrycollection', 'string');
55
        $databasePlatform->registerDoctrineTypeMapping('linestring', 'string');
56
        $databasePlatform->registerDoctrineTypeMapping('multilinestring', 'string');
57
        $databasePlatform->registerDoctrineTypeMapping('multipoint', 'string');
58
        $databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string');
59
        $databasePlatform->registerDoctrineTypeMapping('point', 'string');
60
        $databasePlatform->registerDoctrineTypeMapping('polygon', 'string');
61
        $databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string');
62
        $databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string');
63
64
        $database = null;
65
        if (strpos($table, '.')) {
66
            list($database, $table) = explode('.', $table);
67
        }
68
        $columns = $schema->listTableColumns($table, $database);
69
70
        $admin_form = '';
71
        if ($columns) {
72
            foreach ($columns as $column) {
73
                $name = $column->getName();
74
                if (in_array($name, ['id', 'created_at', 'deleted_at'])) {
75
                    continue;
76
                }
77
                $type = $column->getType()->getName();
78
                $comment = $column->getComment();
79
                $default = $column->getDefault();
80
                if ($default === '') {
81
                    $default = "''";
82
                }
83
84
                switch ($type) {
85
                    case 'boolean':
86
                    case 'bool':
87
                        $field_type = 'switch';
88
                        break;
89
                    case 'json':
90
                    case 'array':
91
                    case 'object':
92
                        $field_type = 'text';
93
                        break;
94
                    case 'string':
95
                        switch ($name) {
96
                            case $this->check_column($name, ['email']):
97
                                $field_type = 'email';
98
                                break;
99
                            case $this->check_column($name, ['password', 'pwd']):
100
                                $field_type = 'password';
101
                                break;
102
                            case $this->check_column($name, ['url', 'link', 'src', 'href']):
103
                                $field_type = 'url';
104
                                break;
105
                            case $this->check_column($name, ['ip']):
106
                                $field_type = 'ip';
107
                                break;
108
                            case $this->check_column($name, ['mobile', 'phone']):
109
                                $field_type = 'mobile';
110
                                break;
111
                            case $this->check_column($name, ['color', 'rgb']):
112
                                $field_type = 'color';
113
                                break;
114
                            case $this->check_column($name, ['image', 'img', 'avatar']) :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
115
                                $field_type = 'image';
116
                                break;
117
                            case $this->check_column($name, ['file', 'attachment']) :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
118
                                $field_type = 'file';
119
                                break;
120
                            default:
121
                                $field_type = 'text';
122
                        }
123
                        break;
124
                    case 'integer':
125
                    case 'bigint':
126
                    case 'smallint':
127
                    case 'timestamp':
128
                        $field_type = 'number';
129
                        break;
130
                    case 'decimal':
131
                    case 'float':
132
                    case 'real':
133
                        $field_type = 'decimal';
134
                        break;
135
                    case 'datetime':
136
                        $field_type = 'datetime';
137
                        $default = "date('Y-m-d H:i:s')";
138
                        break;
139
                    case 'date':
140
                        $field_type = 'date';
141
                        $default = "date('Y-m-d')";
142
                        break;
143
                    case 'text':
144
                    case 'blob':
145
                        $field_type = 'textarea';
146
                        $default = "''";
147
                        break;
148
                    default:
149
                        $field_type = 'text';
150
                }
151
                $admin_form .= "\$form->{$field_type}('{$name}', '{$comment}')->default({$default});\n";
152
            }
153
            $this->alert("laravel-admin form filed generator for {$modelName}:");
154
            $this->info($admin_form);
155
        }
156
157
    }
158
159
    /**
160
     * Check if the table column contains the specified keywords of the array
161
     * @param string $haystack
162
     * @param array $needle
163
     * @return bool
164
     */
165
    private function check_column(string $haystack, array $needle)
166
    {
167
        foreach ($needle as $value) {
168
            if (strstr($haystack, $value) !== false) {
169
                return true;
170
            }
171
        }
172
        return false;
173
    }
174
175
176
    /**
177
     * Get the console command options.
178
     *
179
     * @return array
180
     */
181
    protected function getOptions()
182
    {
183
        return [
184
            ['model', null, InputOption::VALUE_REQUIRED,
185
                'The eloquent model that should be use as controller data source.',],
186
        ];
187
    }
188
}
189