| Conditions | 16 |
| Paths | 1360 |
| Total Lines | 83 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 |
||
| 153 | protected function migrateSimple( |
||
| 154 | bool $includeInserts, |
||
| 155 | string $tableOld, |
||
| 156 | string $tableNew, |
||
| 157 | array $fieldNamesOld, |
||
| 158 | array $fieldNamesNew, |
||
| 159 | array $leftJoin = [], |
||
| 160 | string $where = '' |
||
| 161 | ) { |
||
| 162 | if (! $this->tableExists($tableOld)) { |
||
| 163 | $this->flushNow("{$tableOld} (old table) does not exist", 'error'); |
||
| 164 | } |
||
| 165 | |||
| 166 | if (! $this->tableExists($tableNew)) { |
||
| 167 | $this->flushNow("{$tableNew} (new table) does not exist", 'error'); |
||
| 168 | } |
||
| 169 | |||
| 170 | try { |
||
| 171 | $this->flushNow('getting new table IDs.'); |
||
| 172 | $newEntryIDs = $this->getListOfIDs($tableNew); |
||
| 173 | |||
| 174 | $this->flushNow('getting old IDs.'); |
||
| 175 | $oldEntries = $this->getListAsIterableQuery($tableOld, $leftJoin, $where); |
||
| 176 | $oldEntryIDs = []; |
||
| 177 | |||
| 178 | //add a new line using the ID as identifier |
||
| 179 | foreach ($oldEntries as $oldEntry) { |
||
| 180 | if ($includeInserts) { |
||
| 181 | if (! in_array($oldEntry['ID'], $newEntryIDs, true)) { |
||
| 182 | $this->flushNow('Added row ' . $oldEntry['ID'] . ' to ' . $tableNew . '.'); |
||
| 183 | $this->runUpdateQuery('INSERT INTO "' . $tableNew . '" ("ID") VALUES (' . $oldEntry['ID'] . ');'); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $oldEntryIDs[] = $oldEntry['ID']; |
||
| 188 | } |
||
| 189 | |||
| 190 | //update fields |
||
| 191 | if (count($oldEntryIDs) > 0) { |
||
| 192 | //work out what option is shorter in terms of ID count: |
||
| 193 | $this->flushNow('working out update SQL.'); |
||
| 194 | $allIDs = $this->getListOfIDs($tableNew); |
||
| 195 | $allIDCount = count($allIDs); |
||
| 196 | $oldIDCount = count($oldEntryIDs); |
||
| 197 | if ($oldIDCount > ($allIDCount - $oldIDCount)) { |
||
| 198 | $excludeIDs = array_diff($allIDs, $oldEntryIDs); |
||
| 199 | if (0 === count($excludeIDs)) { |
||
| 200 | $excludeIDs = [0]; |
||
| 201 | } |
||
| 202 | $wherePhrase = ' NOT IN (' . implode(', ', $excludeIDs) . ')'; |
||
| 203 | } else { |
||
| 204 | if (0 === count($oldEntryIDs)) { |
||
| 205 | $oldEntryIDs = [0]; |
||
| 206 | } |
||
| 207 | $wherePhrase = ' IN (' . implode(', ', $oldEntryIDs) . ')'; |
||
| 208 | } |
||
| 209 | |||
| 210 | //update the new table with the old values |
||
| 211 | //for the rows that join with the ID and match the list of OLD ids. |
||
| 212 | if (count($fieldNamesNew) > 0) { |
||
| 213 | $updateQuery = 'UPDATE "' . $tableNew . '" AS "tablenew" '; |
||
| 214 | $updateQuery .= 'INNER JOIN "' . $tableOld . '" AS "tableold" ON "tablenew"."ID" = "tableold"."ID" '; |
||
| 215 | if ('_versions' === substr((string) $tableNew, -9)) { |
||
| 216 | $updateQuery .= ' AND "tablenew"."RecordID" = "tableold"."RecordID" '; |
||
| 217 | // also link to RecordID ... |
||
| 218 | } |
||
| 219 | $updateQuery .= 'SET '; |
||
| 220 | $fieldNamesOldCount = count($fieldNamesOld); |
||
| 221 | $fieldNamesNewCount = count($fieldNamesNew); |
||
| 222 | for ($i = 0; $i < $fieldNamesNewCount && $i < $fieldNamesOldCount; ++$i) { |
||
| 223 | if ($i > 0) { |
||
| 224 | $updateQuery .= ', '; |
||
| 225 | } |
||
| 226 | $updateQuery .= '"tablenew"."' . $fieldNamesNew[$i] . '" = "tableold"."' . $fieldNamesOld[$i] . '" '; |
||
| 227 | } |
||
| 228 | $updateQuery .= 'WHERE "tablenew"."ID" ' . $wherePhrase . ';'; |
||
| 229 | $this->flushNow(str_replace($wherePhrase, '........', $updateQuery)); |
||
| 230 | $this->runUpdateQuery($updateQuery); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } catch (Exception $exception) { |
||
| 234 | $this->flushNow("Unable to migrate {$tableOld} to {$tableNew}.", 'error'); |
||
| 235 | $this->flushNow($exception->getMessage(), 'error'); |
||
| 236 | } |
||
| 239 |