| Conditions | 32 |
| Paths | 2568 |
| Total Lines | 119 |
| Code Lines | 94 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 16 | ||
| Bugs | 1 | Features | 10 |
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 Nwidart\DbExporter; |
||
| 68 | public function convert($database = null) |
||
| 69 | { |
||
| 70 | if (!is_null($database)) { |
||
| 71 | $this->database = $database; |
||
| 72 | $this->customDb = true; |
||
| 73 | } |
||
| 74 | |||
| 75 | $tables = $this->getTables(); |
||
| 76 | |||
| 77 | // Loop over the tables |
||
| 78 | foreach ($tables as $key => $value) { |
||
| 79 | // Do not export the ignored tables |
||
| 80 | if (in_array($value['table_name'], self::$ignore)) { |
||
| 81 | continue; |
||
| 82 | } |
||
| 83 | |||
| 84 | $down = "Schema::drop('{$value['table_name']}');"; |
||
| 85 | $up = "Schema::create('{$value['table_name']}', function(Blueprint $" . "table) {\n"; |
||
| 86 | |||
| 87 | $tableDescribes = $this->getTableDescribes($value['table_name']); |
||
| 88 | // Loop over the tables fields |
||
| 89 | foreach ($tableDescribes as $values) { |
||
| 90 | $method = ""; |
||
| 91 | $para = strpos($values->Type, '('); |
||
| 92 | $type = $para > -1 ? substr($values->Type, 0, $para) : $values->Type; |
||
| 93 | $numbers = ""; |
||
| 94 | $nullable = $values->Null == "NO" ? "" : "->nullable()"; |
||
| 95 | $default = empty($values->Default) ? "" : "->default('".$values->Default."')"; |
||
| 96 | $unsigned = strpos($values->Type, "unsigned") === false ? '' : '->unsigned()'; |
||
| 97 | |||
| 98 | switch ($type) { |
||
| 99 | case 'int' : |
||
| 100 | $method = 'integer'; |
||
| 101 | break; |
||
| 102 | case 'smallint' : |
||
| 103 | $method = 'smallInteger'; |
||
| 104 | break; |
||
| 105 | case 'bigint' : |
||
| 106 | $method = 'bigInteger'; |
||
| 107 | break; |
||
| 108 | case 'char' : |
||
| 109 | case 'varchar' : |
||
| 110 | $para = strpos($values->Type, '('); |
||
| 111 | $numbers = ", " . substr($values->Type, $para + 1, -1); |
||
| 112 | $method = 'string'; |
||
| 113 | break; |
||
| 114 | case 'float' : |
||
| 115 | $method = 'float'; |
||
| 116 | break; |
||
| 117 | case 'double' : |
||
| 118 | $para = strpos($values->Type, '('); # 6 |
||
| 119 | $numbers = ", " . substr($values->Type, $para + 1, -1); |
||
| 120 | $method = 'double'; |
||
| 121 | break; |
||
| 122 | case 'decimal' : |
||
| 123 | $para = strpos($values->Type, '('); |
||
| 124 | $numbers = ", " . substr($values->Type, $para + 1, -1); |
||
| 125 | $method = 'decimal'; |
||
| 126 | break; |
||
| 127 | case 'tinyint' : |
||
| 128 | $method = 'boolean'; |
||
| 129 | break; |
||
| 130 | case 'date' : |
||
| 131 | $method = 'date'; |
||
| 132 | break; |
||
| 133 | case 'timestamp' : |
||
| 134 | $method = 'timestamp'; |
||
| 135 | break; |
||
| 136 | case 'datetime' : |
||
| 137 | $method = 'dateTime'; |
||
| 138 | break; |
||
| 139 | case 'time' : |
||
| 140 | $method = 'time'; |
||
| 141 | break; |
||
| 142 | case 'longtext' : |
||
| 143 | $method = 'longText'; |
||
| 144 | break; |
||
| 145 | case 'mediumtext' : |
||
| 146 | $method = 'mediumText'; |
||
| 147 | break; |
||
| 148 | case 'text' : |
||
| 149 | $method = 'text'; |
||
| 150 | break; |
||
| 151 | case 'longblob': |
||
| 152 | case 'blob' : |
||
| 153 | $method = 'binary'; |
||
| 154 | break; |
||
| 155 | case 'enum' : |
||
| 156 | $method = 'enum'; |
||
| 157 | $para = strpos($values->Type, '('); # 4 |
||
| 158 | $options = substr($values->Type, $para + 1, -1); |
||
| 159 | $numbers = ', array(' . $options . ')'; |
||
| 160 | break; |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($values->Key == 'PRI') { |
||
| 164 | $method = 'increments'; |
||
| 165 | } |
||
| 166 | |||
| 167 | $up .= " $" . "table->{$method}('{$values->Field}'{$numbers}){$nullable}{$default}{$unsigned};\n"; |
||
| 168 | } |
||
| 169 | |||
| 170 | $tableIndexes = $this->getTableIndexes($value['table_name']); |
||
| 171 | if (!is_null($tableIndexes) && count($tableIndexes)){ |
||
| 172 | foreach ($tableIndexes as $index) { |
||
| 173 | $up .= ' $' . "table->index('" . $index['Key_name'] . "');\n"; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | $up .= " });\n\n"; |
||
| 178 | |||
| 179 | $this->schema[$value['table_name']] = array( |
||
| 180 | 'up' => $up, |
||
| 181 | 'down' => $down |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 230 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.