| Conditions | 20 |
| Paths | 780 |
| Total Lines | 82 |
| Code Lines | 51 |
| 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 |
||
| 60 | public function convert($database = null) |
||
| 61 | { |
||
| 62 | if (!is_null($database)) { |
||
| 63 | $this->database = $database; |
||
| 64 | $this->customDb = true; |
||
| 65 | } |
||
| 66 | |||
| 67 | $tables = $this->getTables(); |
||
| 68 | |||
| 69 | // Loop over the tables |
||
| 70 | foreach ($tables as $key => $value) { |
||
| 71 | // Do not export the ignored tables |
||
| 72 | if (in_array($value['table_name'], self::$ignore)) { |
||
| 73 | continue; |
||
| 74 | } |
||
| 75 | |||
| 76 | $down = "Schema::drop('{$value['table_name']}');"; |
||
| 77 | $up = "Schema::create('{$value['table_name']}', function(Blueprint $" . "table) {\n"; |
||
| 78 | |||
| 79 | |||
| 80 | $tableDescribes = $this->getTableDescribes($value['table_name']); |
||
| 81 | // Loop over the tables fields |
||
| 82 | foreach ($tableDescribes as $values) { |
||
| 83 | $para = strpos($values->Type, '('); |
||
| 84 | $type = $para > -1 ? substr($values->Type, 0, $para) : $values->Type; |
||
| 85 | $numbers = ""; |
||
| 86 | $nullable = $values->Null == "NO" ? "" : "->nullable()"; |
||
| 87 | $default = empty($values->Default) ? "" : "->default(\"{$values->Default}\")"; |
||
| 88 | $unsigned = strpos($values->Type, "unsigned") === false ? '': '->unsigned()'; |
||
| 89 | |||
| 90 | if (in_array($type, ['var', 'varchar', 'enum', 'decimal', 'float'])) { |
||
| 91 | $para = strpos($values->Type, '('); |
||
| 92 | $opt = ", " . substr($values->Type, $para + 1, -1); |
||
| 93 | $numbers = $type== 'enum' ? ', array(' . $opt . ')' : $opt; |
||
| 94 | } |
||
| 95 | $method = $this->columnType($type); |
||
| 96 | if ($values->Key == 'PRI') { |
||
| 97 | $method = 'increments'; |
||
| 98 | } |
||
| 99 | |||
| 100 | $up .= " $" . "table->{$method}('{$values->Field}'{$numbers}){$nullable}{$default}{$unsigned};\n"; |
||
| 101 | } |
||
| 102 | |||
| 103 | $tableIndexes = $this->getTableIndexes($value['table_name']); |
||
| 104 | if (!is_null($tableIndexes) && count($tableIndexes)){ |
||
| 105 | foreach ($tableIndexes as $index) { |
||
| 106 | if(Str::endsWith($index['Key_name'], '_index')) { |
||
| 107 | $up .= ' $' . "table->index('" . $index['Key_name'] . "');\n"; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | $up .= " });\n\n"; |
||
| 113 | $Constraint = $ConstraintDown = ""; |
||
| 114 | /** |
||
| 115 | * @var array $tableConstraints |
||
| 116 | */ |
||
| 117 | $tableConstraints = $this->getTableConstraints($value['table_name']); |
||
| 118 | if (!is_null($tableConstraints) && count($tableConstraints)) { |
||
| 119 | $Constraint = $ConstraintDown = " |
||
| 120 | Schema::table('{$value['table_name']}', function(Blueprint $" . "table) {\n"; |
||
| 121 | $tables = []; |
||
| 122 | foreach ($tableConstraints as $foreign) { |
||
| 123 | if (!in_array($foreign->Field, $tables)) { |
||
| 124 | $ConstraintDown .= ' $' . "table->dropForeign('" . $foreign->Field . "');\n"; |
||
| 125 | $Constraint .= ' $' . "table->foreign('" . $foreign->Field . "')->references('" . $foreign->References . "')->on('" . $foreign->ON . "')->onDelete('" . $foreign->onDelete . "');\n"; |
||
| 126 | $tables[$foreign->Field] = $foreign->Field; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | $Constraint .= " });\n\n"; |
||
| 130 | $ConstraintDown .= " });\n\n"; |
||
| 131 | } |
||
| 132 | $this->schema[$value['table_name']] = array( |
||
| 133 | 'up' => $up, |
||
| 134 | 'constraint' => $Constraint, |
||
| 135 | 'constraint_down' => $ConstraintDown, |
||
| 136 | 'down' => $down |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 201 |