Conditions | 34 |
Paths | 112 |
Total Lines | 128 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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); |
||
|
|||
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']) : |
||
115 | $field_type = 'image'; |
||
116 | break; |
||
117 | case $this->check_column($name, ['file', 'attachment']) : |
||
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 | |||
189 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.