Conditions | 33 |
Paths | 60 |
Total Lines | 126 |
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 | switch ($type) { |
||
81 | case 'boolean': |
||
82 | case 'bool': |
||
83 | $fieldType = 'switch'; |
||
84 | break; |
||
85 | case 'json': |
||
86 | case 'array': |
||
87 | case 'object': |
||
88 | $fieldType = 'text'; |
||
89 | break; |
||
90 | case 'string': |
||
91 | switch ($name) { |
||
92 | case $this->checkColumn($name, ['email']): |
||
93 | $fieldType = 'email'; |
||
94 | break; |
||
95 | case $this->checkColumn($name, ['password', 'pwd']): |
||
96 | $fieldType = 'password'; |
||
97 | break; |
||
98 | case $this->checkColumn($name, ['url', 'link', 'src', 'href']): |
||
99 | $fieldType = 'url'; |
||
100 | break; |
||
101 | case $this->checkColumn($name, ['ip']): |
||
102 | $fieldType = 'ip'; |
||
103 | break; |
||
104 | case $this->checkColumn($name, ['mobile', 'phone']): |
||
105 | $fieldType = 'mobile'; |
||
106 | break; |
||
107 | case $this->checkColumn($name, ['color', 'rgb']): |
||
108 | $fieldType = 'color'; |
||
109 | break; |
||
110 | case $this->checkColumn($name, ['image', 'img', 'avatar']) : |
||
111 | $fieldType = 'image'; |
||
112 | break; |
||
113 | case $this->checkColumn($name, ['file', 'attachment']) : |
||
114 | $fieldType = 'file'; |
||
115 | break; |
||
116 | default: |
||
117 | $fieldType = 'text'; |
||
118 | } |
||
119 | break; |
||
120 | case 'integer': |
||
121 | case 'bigint': |
||
122 | case 'smallint': |
||
123 | case 'timestamp': |
||
124 | $fieldType = 'number'; |
||
125 | break; |
||
126 | case 'decimal': |
||
127 | case 'float': |
||
128 | case 'real': |
||
129 | $fieldType = 'decimal'; |
||
130 | break; |
||
131 | case 'datetime': |
||
132 | $fieldType = 'datetime'; |
||
133 | $default = "date('Y-m-d H:i:s')"; |
||
134 | break; |
||
135 | case 'date': |
||
136 | $fieldType = 'date'; |
||
137 | $default = "date('Y-m-d')"; |
||
138 | break; |
||
139 | case 'text': |
||
140 | case 'blob': |
||
141 | $fieldType = 'textarea'; |
||
142 | $default = "''"; |
||
143 | break; |
||
144 | default: |
||
145 | $fieldType = 'text'; |
||
146 | } |
||
147 | |||
148 | $adminForm .= "\$form->{$fieldType}('{$name}', '{$comment}')->default('{$default}');\n"; |
||
149 | } |
||
150 | $this->alert("laravel-admin form filed generator for {$modelName}:"); |
||
151 | $this->info($adminForm); |
||
152 | } |
||
153 | |||
154 | } |
||
155 | |||
187 |
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.