| Conditions | 12 |
| Paths | 105 |
| Total Lines | 106 |
| Code Lines | 64 |
| 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 |
||
| 93 | protected function schemaToMigration(Schema $schema) { |
||
| 94 | $content = <<<'EOT' |
||
| 95 | /** @var Schema $schema */ |
||
| 96 | $schema = $schemaClosure(); |
||
| 97 | |||
| 98 | EOT; |
||
| 99 | |||
| 100 | foreach ($schema->getTables() as $table) { |
||
| 101 | $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
||
| 102 | |||
| 103 | if (!$schema->hasTable('{{table-name}}')) { |
||
| 104 | $table = $schema->createTable('{{table-name}}'); |
||
| 105 | |||
| 106 | EOT |
||
| 107 | ); |
||
| 108 | |||
| 109 | foreach ($table->getColumns() as $column) { |
||
| 110 | $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
||
| 111 | $table->addColumn('{{name}}', '{{type}}', [ |
||
| 112 | |||
| 113 | EOT |
||
| 114 | ); |
||
| 115 | if ($column->getAutoincrement()) { |
||
| 116 | $content .= <<<'EOT' |
||
| 117 | 'autoincrement' => true, |
||
| 118 | |||
| 119 | EOT; |
||
| 120 | } |
||
| 121 | $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
||
| 122 | 'notnull' => {{notnull}}, |
||
| 123 | |||
| 124 | EOT |
||
| 125 | ); |
||
| 126 | if ($column->getLength() !== null) { |
||
| 127 | $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
||
| 128 | 'length' => {{length}}, |
||
| 129 | |||
| 130 | EOT |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | $default = $column->getDefault(); |
||
| 134 | if ($default !== null) { |
||
| 135 | $default = is_numeric($default) ? $default : "'$default'"; |
||
| 136 | $content .= str_replace('{{default}}', $default, <<<'EOT' |
||
| 137 | 'default' => {{default}}, |
||
| 138 | |||
| 139 | EOT |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | $content .= <<<'EOT' |
||
| 143 | ]); |
||
| 144 | |||
| 145 | EOT; |
||
| 146 | } |
||
| 147 | |||
| 148 | $content .= <<<'EOT' |
||
| 149 | |||
| 150 | EOT; |
||
| 151 | |||
| 152 | $primaryKey = $table->getPrimaryKey(); |
||
| 153 | if ($primaryKey !== null) { |
||
| 154 | $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
||
| 155 | $table->setPrimaryKey(['{{columns}}']); |
||
| 156 | |||
| 157 | EOT |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | |||
| 161 | foreach ($table->getIndexes() as $index) { |
||
| 162 | if ($index->isPrimary()) { |
||
| 163 | continue; |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($index->isUnique()) { |
||
| 167 | $content .= str_replace( |
||
| 168 | ['{{columns}}', '{{name}}'], |
||
| 169 | [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
||
| 170 | <<<'EOT' |
||
| 171 | $table->addUniqueIndex(['{{columns}}'], '{{name}}'); |
||
| 172 | |||
| 173 | EOT |
||
| 174 | ); |
||
| 175 | } else { |
||
| 176 | $content .= str_replace( |
||
| 177 | ['{{columns}}', '{{name}}'], |
||
| 178 | [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
||
| 179 | <<<'EOT' |
||
| 180 | $table->addIndex(['{{columns}}'], '{{name}}'); |
||
| 181 | |||
| 182 | EOT |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $content .= <<<'EOT' |
||
| 188 | } |
||
| 189 | |||
| 190 | EOT; |
||
| 191 | } |
||
| 192 | |||
| 193 | $content .= <<<'EOT' |
||
| 194 | return $schema; |
||
| 195 | EOT; |
||
| 196 | |||
| 197 | return $content; |
||
| 198 | } |
||
| 199 | } |
||
| 200 |