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) |
||
| 35 | { |
||
| 36 | $this->database = $database; |
||
| 37 | $columns = $schema->listTableColumns( $table ); |
||
| 38 | if ( empty( $columns ) ) return false; |
||
| 39 | |||
| 40 | $indexGenerator = new IndexGenerator($table, $schema, $ignoreIndexNames); |
||
| 41 | $fields = $this->setEnum($this->getFields($columns, $indexGenerator), $table); |
||
| 42 | $indexes = $this->getMultiFieldIndexes($indexGenerator); |
||
| 43 | return array_merge($fields, $indexes); |
||
| 44 | } |
||
| 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) |
||
| 74 | { |
||
| 75 | foreach ($this->getEnum($table) as $column) { |
||
| 76 | $fields[$column->column_name]['type'] = 'enum'; |
||
| 77 | $fields[$column->column_name]['args'] = str_replace('enum(', 'array(', $column->column_type); |
||
| 78 | } |
||
| 79 | return $fields; |
||
| 80 | } |
||
| 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) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param int $precision |
||
| 191 | * @param int $scale |
||
| 192 | * @return string|void |
||
| 193 | */ |
||
| 194 | protected function getPrecision($precision, $scale) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string|array $args |
||
| 207 | * @param string $quotes |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | protected function argsToString($args, $quotes = '\'') |
||
| 211 | { |
||
| 212 | if ( is_array( $args ) ) { |
||
| 213 | $separator = $quotes .', '. $quotes; |
||
| 214 | $args = implode($separator, str_replace($quotes, '\\'.$quotes, $args)); |
||
| 215 | } else { |
||
| 216 | $args = str_replace($quotes, '\\'.$quotes, $args); |
||
| 217 | } |
||
| 218 | |||
| 219 | return $quotes . $args . $quotes; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get Decorator |
||
| 224 | * @param string $function |
||
| 225 | * @param string|array $args |
||
| 226 | * @param string $quotes |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | protected function decorate($function, $args, $quotes = '\'') |
||
| 230 | { |
||
| 231 | if ( ! is_null( $args ) ) { |
||
| 232 | $args = $this->argsToString($args, $quotes); |
||
| 233 | return $function . '(' . $args . ')'; |
||
| 234 | } else { |
||
| 235 | return $function; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param IndexGenerator $indexGenerator |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | protected function getMultiFieldIndexes(IndexGenerator $indexGenerator) |
||
| 258 | } |
||
| 259 |
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: