Complex classes like TableColumn 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 TableColumn, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class TableColumn |
||
| 8 | { |
||
| 9 | protected $name; |
||
| 10 | protected $type; |
||
| 11 | protected $btype = 'text'; |
||
| 12 | protected $values = []; |
||
| 13 | protected $default = null; |
||
| 14 | protected $comment = null; |
||
| 15 | protected $nullable = false; |
||
| 16 | |||
| 17 | 1 | public function __construct(string $name) |
|
| 21 | |||
| 22 | 1 | public static function fromArray(string $name, array $data = []) |
|
| 23 | { |
||
| 24 | 1 | $instance = new static($name); |
|
| 25 | 1 | if (isset($data['Type'])) { |
|
| 26 | 1 | $instance->setType($data['Type']); |
|
| 27 | } |
||
| 28 | 1 | if (isset($data['type'])) { |
|
| 29 | $instance->setType($data['type']); |
||
| 30 | } |
||
| 31 | 1 | if (isset($data['Comment'])) { |
|
| 32 | 1 | $instance->setComment($data['Comment']); |
|
| 33 | } |
||
| 34 | 1 | if (isset($data['comment'])) { |
|
| 35 | $instance->setComment($data['comment']); |
||
| 36 | } |
||
| 37 | 1 | if (isset($data['Null']) && $data['Null'] === 'YES') { |
|
| 38 | $instance->setNullable(true); |
||
| 39 | } |
||
| 40 | 1 | if (isset($data['nullable']) && is_bool($data['nullable'])) { |
|
| 41 | $instance->setNullable($data['nullable']); |
||
| 42 | } |
||
| 43 | 1 | if (isset($data['Default'])) { |
|
| 44 | $instance->setDefault($data['Default']); |
||
| 45 | } |
||
| 46 | 1 | if (isset($data['default'])) { |
|
| 47 | $instance->setDefault($data['default']); |
||
| 48 | } |
||
| 49 | 1 | if ($instance->getBasicType() === 'enum' && strpos($instance->getType(), 'enum(') === 0) { |
|
| 50 | $temp = array_map(function ($v) { |
||
| 51 | return str_replace("''", "'", $v); |
||
| 52 | }, explode("','", substr($instance->getType(), 6, -2))); |
||
| 53 | $instance->setValues($temp); |
||
| 54 | } |
||
| 55 | 1 | if (isset($data['values']) && is_array($data['values'])) { |
|
| 56 | $instance->setValues($data['values']); |
||
| 57 | } |
||
| 58 | 1 | if (isset($data['DATA_TYPE'])) { |
|
| 59 | $instance->setType($data['DATA_TYPE']); |
||
| 60 | } |
||
| 61 | 1 | if (isset($data['NULLABLE']) && $data['NULLABLE'] !== 'N') { |
|
| 62 | $instance->setNullable(true); |
||
| 63 | } |
||
| 64 | 1 | if (isset($data['DATA_DEFAULT'])) { |
|
| 65 | $instance->setDefault($data['DATA_DEFAULT']); |
||
| 66 | } |
||
| 67 | 1 | return $instance; |
|
| 68 | } |
||
| 69 | |||
| 70 | public function getName() |
||
| 144 | } |