| Conditions | 34 |
| Paths | 112 |
| Total Lines | 137 |
| 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 |
||
| 29 | public function handle() |
||
| 30 | { |
||
| 31 | $modelName = $this->option('model'); |
||
| 32 | if (empty($modelName) || !class_exists($modelName)) { |
||
| 33 | $this->error('Model does not exists !'); |
||
| 34 | return false; |
||
| 35 | } |
||
| 36 | |||
| 37 | // use doctrine/dbal |
||
| 38 | $model = $this->laravel->make($modelName); |
||
|
|
|||
| 39 | $table = $model->getConnection()->getTablePrefix() . $model->getTable(); |
||
| 40 | $schema = $model->getConnection()->getDoctrineSchemaManager($table); |
||
| 41 | |||
| 42 | if (!method_exists($schema, 'getDatabasePlatform')) { |
||
| 43 | $this->error('You need to require doctrine/dbal: ~2.3 in your own composer.json to get database columns. '); |
||
| 44 | $this->info('Using install command: composer require doctrine/dbal'); |
||
| 45 | return false; |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | // custom mapping the types that doctrine/dbal does not support |
||
| 50 | $databasePlatform = $schema->getDatabasePlatform(); |
||
| 51 | $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); |
||
| 52 | $databasePlatform->registerDoctrineTypeMapping('geometry', 'string'); |
||
| 53 | $databasePlatform->registerDoctrineTypeMapping('geometrycollection', 'string'); |
||
| 54 | $databasePlatform->registerDoctrineTypeMapping('linestring', 'string'); |
||
| 55 | $databasePlatform->registerDoctrineTypeMapping('multilinestring', 'string'); |
||
| 56 | $databasePlatform->registerDoctrineTypeMapping('multipoint', 'string'); |
||
| 57 | $databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string'); |
||
| 58 | $databasePlatform->registerDoctrineTypeMapping('point', 'string'); |
||
| 59 | $databasePlatform->registerDoctrineTypeMapping('polygon', 'string'); |
||
| 60 | $databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string'); |
||
| 61 | $databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string'); |
||
| 62 | |||
| 63 | $database = null; |
||
| 64 | if (strpos($table, '.')) { |
||
| 65 | list($database, $table) = explode('.', $table); |
||
| 66 | } |
||
| 67 | $columns = $schema->listTableColumns($table, $database); |
||
| 68 | |||
| 69 | $adminForm = ''; |
||
| 70 | if ($columns) { |
||
| 71 | foreach ($columns as $column) { |
||
| 72 | $name = $column->getName(); |
||
| 73 | if (in_array($name, ['id', 'created_at', 'deleted_at'])) { |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | $type = $column->getType()->getName(); |
||
| 77 | $comment = $column->getComment(); |
||
| 78 | $default = $column->getDefault(); |
||
| 79 | |||
| 80 | // set column fieldType and defaultValue |
||
| 81 | switch ($type) { |
||
| 82 | case 'boolean': |
||
| 83 | case 'bool': |
||
| 84 | $fieldType = 'switch'; |
||
| 85 | $defaultValue = $default; |
||
| 86 | break; |
||
| 87 | case 'json': |
||
| 88 | case 'array': |
||
| 89 | case 'object': |
||
| 90 | $fieldType = 'text'; |
||
| 91 | $defaultValue = $default; |
||
| 92 | break; |
||
| 93 | case 'string': |
||
| 94 | switch ($name) { |
||
| 95 | case $this->checkColumn($name, ['email']): |
||
| 96 | $fieldType = 'email'; |
||
| 97 | break; |
||
| 98 | case $this->checkColumn($name, ['password', 'pwd']): |
||
| 99 | $fieldType = 'password'; |
||
| 100 | break; |
||
| 101 | case $this->checkColumn($name, ['url', 'link', 'src', 'href']): |
||
| 102 | $fieldType = 'url'; |
||
| 103 | break; |
||
| 104 | case $this->checkColumn($name, ['ip']): |
||
| 105 | $fieldType = 'ip'; |
||
| 106 | break; |
||
| 107 | case $this->checkColumn($name, ['mobile', 'phone']): |
||
| 108 | $fieldType = 'mobile'; |
||
| 109 | break; |
||
| 110 | case $this->checkColumn($name, ['color', 'rgb']): |
||
| 111 | $fieldType = 'color'; |
||
| 112 | break; |
||
| 113 | case $this->checkColumn($name, ['image', 'img', 'avatar']) : |
||
| 114 | $fieldType = 'image'; |
||
| 115 | break; |
||
| 116 | case $this->checkColumn($name, ['file', 'attachment']) : |
||
| 117 | $fieldType = 'file'; |
||
| 118 | break; |
||
| 119 | default: |
||
| 120 | $fieldType = 'text'; |
||
| 121 | } |
||
| 122 | $defaultValue = "'{$default}'"; |
||
| 123 | break; |
||
| 124 | case 'integer': |
||
| 125 | case 'bigint': |
||
| 126 | case 'smallint': |
||
| 127 | case 'timestamp': |
||
| 128 | $fieldType = 'number'; |
||
| 129 | $defaultValue = $default; |
||
| 130 | break; |
||
| 131 | case 'decimal': |
||
| 132 | case 'float': |
||
| 133 | case 'real': |
||
| 134 | $fieldType = 'decimal'; |
||
| 135 | $defaultValue = $default; |
||
| 136 | break; |
||
| 137 | case 'datetime': |
||
| 138 | $fieldType = 'datetime'; |
||
| 139 | $defaultValue = "date('Y-m-d H:i:s')"; |
||
| 140 | break; |
||
| 141 | case 'date': |
||
| 142 | $fieldType = 'date'; |
||
| 143 | $defaultValue = "date('Y-m-d')"; |
||
| 144 | break; |
||
| 145 | case 'text': |
||
| 146 | case 'blob': |
||
| 147 | $fieldType = 'textarea'; |
||
| 148 | $defaultValue = 'NULL'; |
||
| 149 | break; |
||
| 150 | default: |
||
| 151 | $fieldType = 'text'; |
||
| 152 | $defaultValue = "''"; |
||
| 153 | } |
||
| 154 | |||
| 155 | // set column comment |
||
| 156 | $comment = $comment ? $comment : $name; |
||
| 157 | |||
| 158 | |||
| 159 | $adminForm .= "\$form->{$fieldType}('{$name}', '{$comment}')->default({$defaultValue});\n"; |
||
| 160 | } |
||
| 161 | $this->alert("laravel-admin form filed generator for {$modelName}:"); |
||
| 162 | $this->info($adminForm); |
||
| 163 | } |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 198 |
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.