| Total Complexity | 52 |
| Total Lines | 260 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MigrationsAbstract often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MigrationsAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class MigrationsAbstract |
||
| 17 | { |
||
| 18 | use ContentManager, MigrationsTrait; |
||
|
|
|||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | public static int $counter = 10; |
||
| 24 | |||
| 25 | public const PATTERN_TIME = 'Y_m_d_Hi'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private array $signedIntegerMap = [ |
||
| 31 | ModelsInterface::INT_DIGITS_TINY => ModelsInterface::MIGRATION_METHOD_TINY_INTEGER, |
||
| 32 | ModelsInterface::INT_DIGITS_SMALL => ModelsInterface::MIGRATION_METHOD_SMALL_INTEGER, |
||
| 33 | ModelsInterface::INT_DIGITS_MEDIUM => ModelsInterface::MIGRATION_METHOD_MEDIUM_INTEGER, |
||
| 34 | ModelsInterface::INT_DIGITS_INT => ModelsInterface::MIGRATION_METHOD_INTEGER, |
||
| 35 | ModelsInterface::INT_DIGITS_BIGINT => ModelsInterface::MIGRATION_METHOD_BIG_INTEGER, |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private array $unsignedIntegerMap = [ |
||
| 42 | ModelsInterface::INT_DIGITS_TINY => ModelsInterface::MIGRATION_METHOD_UTINYINT, |
||
| 43 | ModelsInterface::INT_DIGITS_SMALL => ModelsInterface::MIGRATION_METHOD_USMALLINT, |
||
| 44 | ModelsInterface::INT_DIGITS_MEDIUM => ModelsInterface::MIGRATION_METHOD_UMEDIUMINT, |
||
| 45 | ModelsInterface::INT_DIGITS_INT => ModelsInterface::MIGRATION_METHOD_UINT, |
||
| 46 | ModelsInterface::INT_DIGITS_BIGINT => ModelsInterface::MIGRATION_METHOD_UBIGINT, |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Sets rows of migration with their description and options |
||
| 51 | */ |
||
| 52 | protected function setRows(): void |
||
| 53 | { |
||
| 54 | $attrs = $this->getEntityAttributes(); |
||
| 55 | foreach ($attrs as $attrKey => $attrVal) { |
||
| 56 | if (is_array($attrVal)) { |
||
| 57 | if (empty($attrVal[ApiInterface::RAML_TYPE]) === false) { |
||
| 58 | $this->setDescription($attrVal); |
||
| 59 | $type = $attrVal[ApiInterface::RAML_TYPE]; |
||
| 60 | // create migration fields depending on Types |
||
| 61 | $this->setRowContent($attrVal, $type, $attrKey); |
||
| 62 | } else {// non-standard Types aka enum |
||
| 63 | if (empty($attrVal[ApiInterface::RAML_ENUM]) === false) { |
||
| 64 | $this->setRowContent($attrVal, ApiInterface::RAML_ENUM, $attrKey); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | try { |
||
| 69 | $this->setIndex($attrVal, $attrKey); |
||
| 70 | $this->setCompositeIndex($attrVal); |
||
| 71 | } catch (AttributesException $e) { |
||
| 72 | echo $e->getTraceAsString(); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | // created_at/updated_at created for every table |
||
| 77 | $this->setRow(ModelsInterface::MIGRATION_METHOD_TIMESTAMPS, '', null, null, false); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Sets rows of migration with their description and options |
||
| 82 | * @param array $attrs |
||
| 83 | */ |
||
| 84 | protected function setAddRows(array $attrs): void |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Sets row content with opts |
||
| 110 | * @param array $attrVal |
||
| 111 | * @param string $type |
||
| 112 | * @param string $attrKey |
||
| 113 | */ |
||
| 114 | private function setRowContent(array $attrVal, string $type, string $attrKey): void |
||
| 115 | { |
||
| 116 | // create migration fields depending on Types |
||
| 117 | switch ($type) { |
||
| 118 | case ApiInterface::RAML_TYPE_STRING: |
||
| 119 | $this->setStringRow($attrKey, $attrVal); |
||
| 120 | break; |
||
| 121 | case ApiInterface::RAML_TYPE_INTEGER: |
||
| 122 | // create an auto_incremented primary key |
||
| 123 | $this->setIntegerRow($attrKey, $attrVal, $type); |
||
| 124 | break; |
||
| 125 | case ApiInterface::RAML_TYPE_BOOLEAN: |
||
| 126 | $this->setRow(ModelsInterface::MIGRATION_METHOD_UTINYINT, $attrKey); |
||
| 127 | break; |
||
| 128 | case ApiInterface::RAML_TYPE_NUMBER: |
||
| 129 | $this->setNumberRow($attrVal, $attrKey); |
||
| 130 | break; |
||
| 131 | case ApiInterface::RAML_ENUM: |
||
| 132 | $this->setRow(ModelsInterface::MIGRATION_METHOD_ENUM, $attrKey, |
||
| 133 | $this->getArrayParam($attrVal[ModelsInterface::MIGRATION_METHOD_ENUM])); |
||
| 134 | break; |
||
| 135 | case ApiInterface::RAML_DATE: |
||
| 136 | $this->setRow(ModelsInterface::MIGRATION_METHOD_DATE, $attrKey); |
||
| 137 | break; |
||
| 138 | case ApiInterface::RAML_TIME: |
||
| 139 | $this->setRow(ModelsInterface::MIGRATION_METHOD_TIME, $attrKey); |
||
| 140 | break; |
||
| 141 | case ApiInterface::RAML_DATETIME: |
||
| 142 | if ($attrKey === ModelsInterface::COLUMN_DEL_AT) { |
||
| 143 | $this->setRow(ModelsInterface::MIGRATION_METHOD_SOFT_DEL, '', null, null, false); |
||
| 144 | } else { |
||
| 145 | $this->setRow(ModelsInterface::MIGRATION_METHOD_DATETIME, $attrKey); |
||
| 146 | } |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param array $attrs |
||
| 153 | */ |
||
| 154 | public function dropRows(array $attrs): void |
||
| 155 | { |
||
| 156 | foreach ($attrs as $attrKey => $attrVal) { |
||
| 157 | $this->setRow(ModelsInterface::MIGRATION_DROP_COLUMN, $attrKey); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sets index for particular column if facets was declared |
||
| 163 | * @param array $attrVal |
||
| 164 | * @param string $attrKey |
||
| 165 | * |
||
| 166 | * @throws AttributesException |
||
| 167 | */ |
||
| 168 | public function setIndex(array $attrVal, string $attrKey): void |
||
| 169 | { |
||
| 170 | if (empty($attrVal[ApiInterface::RAML_FACETS][ApiInterface::RAML_INDEX]) === false) { |
||
| 171 | $facets = $attrVal[ApiInterface::RAML_FACETS][ApiInterface::RAML_INDEX]; |
||
| 172 | foreach ($facets as $k => $v) { |
||
| 173 | switch ($v) { |
||
| 174 | case ModelsInterface::INDEX_TYPE_INDEX: |
||
| 175 | $this->setRow(ModelsInterface::INDEX_TYPE_INDEX, $attrKey, $this->quoteParam($k)); |
||
| 176 | break; |
||
| 177 | case ModelsInterface::INDEX_TYPE_PRIMARY: |
||
| 178 | $this->setRow(ModelsInterface::INDEX_TYPE_PRIMARY, $attrKey, $this->quoteParam($k)); |
||
| 179 | break; |
||
| 180 | case ModelsInterface::INDEX_TYPE_UNIQUE: |
||
| 181 | $this->setRow(ModelsInterface::INDEX_TYPE_UNIQUE, $attrKey, $this->quoteParam($k)); |
||
| 182 | break; |
||
| 183 | case ModelsInterface::INDEX_TYPE_FOREIGN: |
||
| 184 | $this->setForeignIndex($facets, $attrKey, $k); |
||
| 185 | break; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $key |
||
| 193 | * @param int $max |
||
| 194 | * @param bool $signed |
||
| 195 | */ |
||
| 196 | private function setIntegerDigit(string $key, int $max = null, bool $signed = false): void |
||
| 197 | { |
||
| 198 | if ($signed) { |
||
| 199 | foreach ($this->signedIntegerMap as $digits => $method) { |
||
| 200 | $next = next($this->signedIntegerMap); |
||
| 201 | if ($digits >= $max && ($next === false || ($next !== false && $max < key($this->signedIntegerMap)))) { |
||
| 202 | $this->setRow($method, $key); |
||
| 203 | break; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } else { |
||
| 207 | foreach ($this->unsignedIntegerMap as $digits => $method) { |
||
| 208 | $next = next($this->unsignedIntegerMap); |
||
| 209 | if ($digits >= $max && ($next === false || ($next !== false && $max < key($this->unsignedIntegerMap)))) { |
||
| 210 | $this->setRow($method, $key); |
||
| 211 | break; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param $relationEntity |
||
| 219 | */ |
||
| 220 | protected function setPivotRows($relationEntity): void |
||
| 221 | { |
||
| 222 | $this->setRow(ModelsInterface::MIGRATION_METHOD_INCREMENTS, ApiInterface::RAML_ID); |
||
| 223 | $attrs = [ |
||
| 224 | strtolower($this->generator->objectName) . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID => $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ID]], |
||
| 225 | $relationEntity . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID => $this->generator->types[$this->generator->types[ucfirst($relationEntity)][ApiInterface::RAML_PROPS][ApiInterface::RAML_ID]], |
||
| 226 | ]; |
||
| 227 | foreach ($attrs as $attrKey => $attrVal) { |
||
| 228 | $this->setRowContent($attrVal, $attrVal[ApiInterface::RAML_TYPE], $attrKey); |
||
| 229 | } |
||
| 230 | $this->setRow(ModelsInterface::MIGRATION_METHOD_TIMESTAMPS, '', null, null, false); |
||
| 231 | } |
||
| 232 | |||
| 233 | private function getEntityAttributes() |
||
| 234 | { |
||
| 235 | $attrsArray = [ApiInterface::RAML_ID => $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ID]]] + |
||
| 236 | $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]][ApiInterface::RAML_PROPS]; |
||
| 237 | |||
| 238 | return $attrsArray; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $attrVal |
||
| 243 | * @param $attrKey |
||
| 244 | * @param $type |
||
| 245 | */ |
||
| 246 | private function setId($attrVal, $attrKey, $type): void |
||
| 247 | { |
||
| 248 | // set incremented id int |
||
| 249 | if ($type === ApiInterface::RAML_TYPE_INTEGER && empty($attrVal[ApiInterface::RAML_INTEGER_MAX]) === false) { |
||
| 250 | if ($attrVal[ApiInterface::RAML_INTEGER_MAX] > ModelsInterface::ID_MAX_INCREMENTS) { |
||
| 251 | $this->setRow(ModelsInterface::MIGRATION_METHOD_BIG_INCREMENTS, $attrKey); |
||
| 252 | |||
| 253 | return; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | $this->setRow(ModelsInterface::MIGRATION_METHOD_INCREMENTS, $attrKey); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Creates migration file with time mask |
||
| 261 | * |
||
| 262 | * @param string $migrationName |
||
| 263 | */ |
||
| 264 | protected function createMigrationFile(string $migrationName): void |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |