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 | * @var bool |
||
| 26 | */ |
||
| 27 | private $unusedTimestamps; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param bool $unusedTimestamps |
||
| 31 | */ |
||
| 32 | public function __construct($unusedTimestamps) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Create array of all the fields for a table |
||
| 39 | * |
||
| 40 | * @param string $table Table Name |
||
| 41 | * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema |
||
| 42 | * @param string $database |
||
| 43 | * @param bool $ignoreIndexNames |
||
| 44 | * |
||
| 45 | * @return array|bool |
||
| 46 | */ |
||
| 47 | public function generate($table, $schema, $database, $ignoreIndexNames) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Return all enum columns for a given table |
||
| 61 | * @param string $table |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | protected function getEnum($table) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param array $fields |
||
| 83 | * @param string $table |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | protected function setEnum(array $fields, $table) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param \Doctrine\DBAL\Schema\Column[] $columns |
||
| 97 | * @param IndexGenerator $indexGenerator |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | protected function getFields($columns, IndexGenerator $indexGenerator) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param int $length |
||
| 176 | * @return int|void |
||
| 177 | */ |
||
| 178 | protected function getLength($length) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $default |
||
| 187 | * @param string $type |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | protected function getDefault($default, &$type) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param int $precision |
||
| 204 | * @param int $scale |
||
| 205 | * @return string|void |
||
| 206 | */ |
||
| 207 | protected function getPrecision($precision, $scale) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string|array $args |
||
| 220 | * @param string $quotes |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | protected function argsToString($args, $quotes = '\'') |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get Decorator |
||
| 237 | * @param string $function |
||
| 238 | * @param string|array $args |
||
| 239 | * @param string $quotes |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | protected function decorate($function, $args, $quotes = '\'') |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param IndexGenerator $indexGenerator |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function getMultiFieldIndexes(IndexGenerator $indexGenerator) |
||
| 271 | } |
||
| 272 |
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: