| Conditions | 38 |
| Paths | 13329 |
| Total Lines | 112 |
| 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 |
||
| 146 | private function loadField($table, $xml) { |
||
| 147 | $options = ['notnull' => false]; |
||
| 148 | $primary = null; |
||
| 149 | foreach ($xml->children() as $child) { |
||
| 150 | /** |
||
| 151 | * @var \SimpleXMLElement $child |
||
| 152 | */ |
||
| 153 | switch ($child->getName()) { |
||
| 154 | case 'name': |
||
| 155 | $name = (string)$child; |
||
| 156 | $name = $this->platform->quoteIdentifier($name); |
||
| 157 | break; |
||
| 158 | case 'type': |
||
| 159 | $type = (string)$child; |
||
| 160 | switch ($type) { |
||
| 161 | case 'text': |
||
| 162 | $type = 'string'; |
||
| 163 | break; |
||
| 164 | case 'clob': |
||
| 165 | $type = 'text'; |
||
| 166 | break; |
||
| 167 | case 'timestamp': |
||
| 168 | $type = 'datetime'; |
||
| 169 | break; |
||
| 170 | case 'numeric': |
||
| 171 | $type = 'decimal'; |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | break; |
||
| 175 | case 'length': |
||
| 176 | $length = (string)$child; |
||
| 177 | $options['length'] = $length; |
||
| 178 | break; |
||
| 179 | case 'unsigned': |
||
| 180 | $unsigned = $this->asBool($child); |
||
| 181 | $options['unsigned'] = $unsigned; |
||
| 182 | break; |
||
| 183 | case 'notnull': |
||
| 184 | $notnull = $this->asBool($child); |
||
| 185 | $options['notnull'] = $notnull; |
||
| 186 | break; |
||
| 187 | case 'autoincrement': |
||
| 188 | $autoincrement = $this->asBool($child); |
||
| 189 | $options['autoincrement'] = $autoincrement; |
||
| 190 | break; |
||
| 191 | case 'default': |
||
| 192 | $default = (string)$child; |
||
| 193 | $options['default'] = $default; |
||
| 194 | break; |
||
| 195 | case 'comments': |
||
| 196 | $comment = (string)$child; |
||
| 197 | $options['comment'] = $comment; |
||
| 198 | break; |
||
| 199 | case 'primary': |
||
| 200 | $primary = $this->asBool($child); |
||
| 201 | break; |
||
| 202 | case 'precision': |
||
| 203 | $precision = (string)$child; |
||
| 204 | $options['precision'] = $precision; |
||
| 205 | break; |
||
| 206 | case 'scale': |
||
| 207 | $scale = (string)$child; |
||
| 208 | $options['scale'] = $scale; |
||
| 209 | break; |
||
| 210 | default: |
||
| 211 | throw new \DomainException('Unknown element: ' . $child->getName()); |
||
| 212 | |||
| 213 | } |
||
| 214 | } |
||
| 215 | if (isset($name, $type)) { |
||
| 216 | if (isset($options['default']) && empty($options['default'])) { |
||
| 217 | if (empty($options['notnull']) || !$options['notnull']) { |
||
| 218 | unset($options['default']); |
||
| 219 | $options['notnull'] = false; |
||
| 220 | } else { |
||
| 221 | $options['default'] = ''; |
||
| 222 | } |
||
| 223 | if ($type == 'integer' || $type == 'decimal') { |
||
| 224 | $options['default'] = 0; |
||
| 225 | } elseif ($type == 'boolean') { |
||
| 226 | $options['default'] = false; |
||
| 227 | } |
||
| 228 | if (!empty($options['autoincrement']) && $options['autoincrement']) { |
||
| 229 | unset($options['default']); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | if ($type === 'integer' && isset($options['default'])) { |
||
| 233 | $options['default'] = (int)$options['default']; |
||
| 234 | } |
||
| 235 | if ($type === 'integer' && isset($options['length'])) { |
||
| 236 | $length = $options['length']; |
||
| 237 | if ($length < 4) { |
||
| 238 | $type = 'smallint'; |
||
| 239 | } elseif ($length > 4) { |
||
| 240 | $type = 'bigint'; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | if ($type === 'boolean' && isset($options['default'])) { |
||
| 244 | $options['default'] = $this->asBool($options['default']); |
||
| 245 | } |
||
| 246 | if (!empty($options['autoincrement']) |
||
| 247 | && !empty($options['notnull']) |
||
| 248 | ) { |
||
| 249 | $primary = true; |
||
| 250 | } |
||
| 251 | |||
| 252 | $table->addColumn($name, $type, $options); |
||
| 253 | if ($primary) { |
||
| 254 | $table->setPrimaryKey([$name]); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 337 |