Complex classes like FieldGenerator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FieldGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Xethron\MigrationsGenerator\Generators; |
||
| 5 | class FieldGenerator { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Convert dbal types to Laravel Migration Types |
||
| 9 | * @var array |
||
| 10 | */ |
||
| 11 | protected $fieldTypeMap = [ |
||
| 12 | 'tinyint' => 'tinyInteger', |
||
| 13 | 'smallint' => 'smallInteger', |
||
| 14 | 'bigint' => 'bigInteger', |
||
| 15 | 'datetime' => 'dateTime', |
||
| 16 | 'blob' => 'binary', |
||
| 17 | ]; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $database; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create array of all the fields for a table |
||
| 26 | * |
||
| 27 | * @param string $table Table Name |
||
| 28 | * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema |
||
| 29 | * @param string $database |
||
| 30 | * @param bool $ignoreIndexNames |
||
| 31 | * |
||
| 32 | * @return array|bool |
||
| 33 | */ |
||
| 34 | public function generate($table, $schema, $database, $ignoreIndexNames) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Return all enum columns for a given table |
||
| 48 | * @param string $table |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | protected function getEnum($table) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param array $fields |
||
| 70 | * @param string $table |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | protected function setEnum(array $fields, $table) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param \Doctrine\DBAL\Schema\Column[] $columns |
||
| 84 | * @param IndexGenerator $indexGenerator |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | protected function getFields($columns, IndexGenerator $indexGenerator) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param int $length |
||
| 163 | * @return int|void |
||
| 164 | */ |
||
| 165 | protected function getLength($length) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $default |
||
| 174 | * @param string $type |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | protected function getDefault($default, &$type) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param int $precision |
||
| 192 | * @param int $scale |
||
| 193 | * @return string|void |
||
| 194 | */ |
||
| 195 | protected function getPrecision($precision, $scale) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string|array $args |
||
| 208 | * @param string $quotes |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | protected function argsToString($args, $quotes = '\'') |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get Decorator |
||
| 225 | * @param string $function |
||
| 226 | * @param string|array $args |
||
| 227 | * @param string $quotes |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | protected function decorate($function, $args, $quotes = '\'') |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param IndexGenerator $indexGenerator |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | protected function getMultiFieldIndexes(IndexGenerator $indexGenerator) |
||
| 259 | } |
||
| 260 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: