| Conditions | 26 |
| Paths | 170 |
| Total Lines | 89 |
| 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 |
||
| 79 | public function generateForm() |
||
| 80 | { |
||
| 81 | $reservedColumns = $this->getReservedColumns(); |
||
| 82 | |||
| 83 | $output = ''; |
||
| 84 | |||
| 85 | foreach ($this->getTableColumns() as $column) { |
||
| 86 | $name = $column->getName(); |
||
| 87 | if (in_array($name, $reservedColumns)) { |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | $type = $column->getType()->getName(); |
||
| 91 | $default = $column->getDefault(); |
||
| 92 | |||
| 93 | $defaultValue = ''; |
||
| 94 | |||
| 95 | // set column fieldType and defaultValue |
||
| 96 | switch ($type) { |
||
| 97 | case 'boolean': |
||
| 98 | case 'bool': |
||
| 99 | $fieldType = 'switch'; |
||
| 100 | break; |
||
| 101 | case 'json': |
||
| 102 | case 'array': |
||
| 103 | case 'object': |
||
| 104 | $fieldType = 'text'; |
||
| 105 | break; |
||
| 106 | case 'string': |
||
| 107 | $fieldType = 'text'; |
||
| 108 | foreach ($this->fieldTypeMapping as $type => $regex) { |
||
| 109 | if (preg_match("/^($regex)$/i", $name) !== 0) { |
||
| 110 | $fieldType = $type; |
||
| 111 | break; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | $defaultValue = "'{$default}'"; |
||
| 115 | break; |
||
| 116 | case 'integer': |
||
| 117 | case 'bigint': |
||
| 118 | case 'smallint': |
||
| 119 | case 'timestamp': |
||
| 120 | $fieldType = 'number'; |
||
| 121 | break; |
||
| 122 | case 'decimal': |
||
| 123 | case 'float': |
||
| 124 | case 'real': |
||
| 125 | $fieldType = 'decimal'; |
||
| 126 | break; |
||
| 127 | case 'datetime': |
||
| 128 | $fieldType = 'datetime'; |
||
| 129 | $defaultValue = "date('Y-m-d H:i:s')"; |
||
| 130 | break; |
||
| 131 | case 'date': |
||
| 132 | $fieldType = 'date'; |
||
| 133 | $defaultValue = "date('Y-m-d')"; |
||
| 134 | break; |
||
| 135 | case 'time': |
||
| 136 | $fieldType = 'time'; |
||
| 137 | $defaultValue = "date('H:i:s')"; |
||
| 138 | break; |
||
| 139 | case 'text': |
||
| 140 | case 'blob': |
||
| 141 | $fieldType = 'textarea'; |
||
| 142 | break; |
||
| 143 | default: |
||
| 144 | $fieldType = 'text'; |
||
| 145 | $defaultValue = "'{$default}'"; |
||
| 146 | } |
||
| 147 | |||
| 148 | $defaultValue = $defaultValue ?: $default; |
||
| 149 | |||
| 150 | $comment = $column->getComment(); |
||
| 151 | $comment = explode(' ', $comment)[0]; |
||
| 152 | if (!$comment) { |
||
| 153 | $comment = $name; |
||
| 154 | } |
||
| 155 | $label = $this->formatLabel($comment); |
||
| 156 | |||
| 157 | $output .= sprintf($this->formats['form_field'], $fieldType, $name, $label); |
||
| 158 | |||
| 159 | if (trim($defaultValue, "'\"")) { |
||
| 160 | $output .= "->default({$defaultValue})"; |
||
| 161 | } |
||
| 162 | |||
| 163 | $output .= ";\r\n"; |
||
| 164 | } |
||
| 165 | |||
| 166 | return $output; |
||
| 167 | } |
||
| 168 | |||
| 270 |