| Conditions | 28 |
| Paths | 4237 |
| Total Lines | 73 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 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 namespace Xethron\MigrationsGenerator\Generators; |
||
| 87 | protected function getFields($columns, IndexGenerator $indexGenerator) |
||
| 88 | { |
||
| 89 | $fields = array(); |
||
| 90 | foreach ($columns as $column) { |
||
| 91 | $name = $column->getName(); |
||
| 92 | $type = $column->getType()->getName(); |
||
| 93 | $length = $column->getLength(); |
||
| 94 | $default = $column->getDefault(); |
||
| 95 | if (is_bool($default)) |
||
| 96 | $default = $default === true ? 1 : 0; |
||
| 97 | $nullable = (!$column->getNotNull()); |
||
| 98 | $index = $indexGenerator->getIndex($name); |
||
| 99 | $comment = $column->getComment(); |
||
| 100 | $decorators = null; |
||
| 101 | $args = null; |
||
| 102 | |||
| 103 | if (isset($this->fieldTypeMap[$type])) { |
||
| 104 | $type = $this->fieldTypeMap[$type]; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Different rules for different type groups |
||
| 108 | if (in_array($type, ['tinyInteger', 'smallInteger', 'integer', 'bigInteger'])) { |
||
| 109 | // Integer |
||
| 110 | if ($type == 'integer' and $column->getUnsigned() and $column->getAutoincrement()) { |
||
| 111 | $type = 'increments'; |
||
| 112 | $index = null; |
||
| 113 | } else { |
||
| 114 | if ($column->getUnsigned()) { |
||
| 115 | $decorators[] = 'unsigned'; |
||
| 116 | } |
||
| 117 | if ($column->getAutoincrement()) { |
||
| 118 | $args = 'true'; |
||
| 119 | $index = null; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } elseif ($type == 'dateTime') { |
||
| 123 | if ($name == 'deleted_at' and $nullable) { |
||
| 124 | $nullable = false; |
||
| 125 | $type = 'softDeletes'; |
||
| 126 | $name = ''; |
||
| 127 | } elseif ($name == 'created_at' and isset($fields['updated_at'])) { |
||
| 128 | $fields['updated_at'] = ['field' => '', 'type' => 'timestamps']; |
||
| 129 | continue; |
||
| 130 | } elseif ($name == 'updated_at' and isset($fields['created_at'])) { |
||
| 131 | $fields['created_at'] = ['field' => '', 'type' => 'timestamps']; |
||
| 132 | continue; |
||
| 133 | } |
||
| 134 | } elseif (in_array($type, ['decimal', 'float', 'double'])) { |
||
| 135 | // Precision based numbers |
||
| 136 | $args = $this->getPrecision($column->getPrecision(), $column->getScale()); |
||
| 137 | if ($column->getUnsigned()) { |
||
| 138 | $decorators[] = 'unsigned'; |
||
| 139 | } |
||
| 140 | } else { |
||
| 141 | // Probably not a number (string/char) |
||
| 142 | if ($type === 'string' && $column->getFixed()) { |
||
| 143 | $type = 'char'; |
||
| 144 | } |
||
| 145 | $args = $this->getLength($length); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($nullable) $decorators[] = 'nullable'; |
||
| 149 | if ($default !== null) $decorators[] = $this->getDefault($default, $type); |
||
| 150 | if ($index) $decorators[] = $this->decorate($index->type, $index->name); |
||
| 151 | if ($comment) $decorators[] = "comment('" . addcslashes($comment, "\\'") . "')"; |
||
|
|
|||
| 152 | |||
| 153 | $field = ['field' => $name, 'type' => $type]; |
||
| 154 | if ($decorators) $field['decorators'] = $decorators; |
||
| 155 | if ($args) $field['args'] = $args; |
||
| 156 | $fields[$name] = $field; |
||
| 157 | } |
||
| 158 | return $fields; |
||
| 159 | } |
||
| 160 | |||
| 259 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: