| Total Complexity | 51 |
| Total Lines | 241 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Column 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 Column, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | abstract class Column implements ColumnInterface |
||
| 40 | { |
||
| 41 | private bool|null $allowNull = null; |
||
| 42 | private bool $autoIncrement = false; |
||
| 43 | private string|ExpressionInterface|null $check = null; |
||
|
|
|||
| 44 | private string|null $comment = null; |
||
| 45 | private bool $computed = false; |
||
| 46 | private string|null $dbType = null; |
||
| 47 | private mixed $defaultValue = null; |
||
| 48 | private string|null $extra = null; |
||
| 49 | private bool $primaryKey = false; |
||
| 50 | private ForeignKeyConstraint|null $reference = null; |
||
| 51 | private int|null $scale = null; |
||
| 52 | private int|null $size = null; |
||
| 53 | private bool $unique = false; |
||
| 54 | private bool $unsigned = false; |
||
| 55 | private array $values = []; |
||
| 56 | |||
| 57 | public function __construct( |
||
| 58 | private string|null $type = null, |
||
| 59 | private string|null $phpType = null, |
||
| 60 | ) { |
||
| 61 | } |
||
| 62 | |||
| 63 | public function allowNull(bool|null $value = true): static |
||
| 64 | { |
||
| 65 | $this->allowNull = $value; |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function autoIncrement(bool $value = true): static |
||
| 70 | { |
||
| 71 | $this->autoIncrement = $value; |
||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function comment(string $value = null): static |
||
| 76 | { |
||
| 77 | $this->comment = $value; |
||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function computed(bool $value = true): static |
||
| 82 | { |
||
| 83 | $this->computed = $value; |
||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function dbType(string $value = null): static |
||
| 88 | { |
||
| 89 | $this->dbType = $value; |
||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function dbTypecast(mixed $value): mixed |
||
| 94 | { |
||
| 95 | return $value; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function defaultValue(mixed $value = null): static |
||
| 99 | { |
||
| 100 | $this->defaultValue = $value; |
||
| 101 | return $this; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function extra(string $value = null): static |
||
| 105 | { |
||
| 106 | $this->extra = $value; |
||
| 107 | return $this; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getComment(): string|null |
||
| 111 | { |
||
| 112 | return $this->comment; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function getDbType(): string|null |
||
| 116 | { |
||
| 117 | return $this->dbType; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function getDefaultValue(): mixed |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getExtra(): string|null |
||
| 126 | { |
||
| 127 | return $this->extra; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getFullDbType(): string|null |
||
| 131 | { |
||
| 132 | if ($this->dbType === null) { |
||
| 133 | return null; |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($this->size === null) { |
||
| 137 | return $this->dbType; |
||
| 138 | } |
||
| 139 | |||
| 140 | return "$this->dbType($this->size)"; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function getPhpType(): string|null |
||
| 144 | { |
||
| 145 | return $this->phpType; |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getScale(): int|null |
||
| 149 | { |
||
| 150 | return $this->scale; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function getSize(): int|null |
||
| 154 | { |
||
| 155 | return $this->size; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getType(): string |
||
| 161 | } |
||
| 162 | |||
| 163 | public function getValues(): array |
||
| 164 | { |
||
| 166 | } |
||
| 167 | |||
| 168 | public function isAllowNull(): bool|null |
||
| 169 | { |
||
| 170 | return $this->allowNull; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function isAutoIncrement(): bool |
||
| 174 | { |
||
| 175 | return $this->autoIncrement; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function isComputed(): bool |
||
| 179 | { |
||
| 180 | return $this->computed; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function isPrimaryKey(): bool |
||
| 184 | { |
||
| 185 | return $this->primaryKey; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function isUnsigned(): bool |
||
| 189 | { |
||
| 190 | return $this->unsigned; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function load(array $info): static |
||
| 194 | { |
||
| 195 | foreach ($info as $key => $value) { |
||
| 196 | match ($key) { |
||
| 197 | 'allow_null' => $this->allowNull($value !== null ? (bool) $value : null), |
||
| 198 | 'auto_increment' => $this->autoIncrement((bool) $value), |
||
| 199 | 'comment' => $this->comment($value !== null ? (string) $value : null), |
||
| 200 | 'computed' => $this->computed((bool) $value), |
||
| 201 | 'db_type' => $this->dbType($value !== null ? (string) $value : null), |
||
| 202 | 'default_value' => $this->defaultValue($value), |
||
| 203 | 'extra' => $this->extra($value !== null ? (string) $value : null), |
||
| 204 | 'primary_key' => $this->primaryKey((bool) $value), |
||
| 205 | 'php_type' => $this->phpType($value !== null ? (string) $value : null), |
||
| 206 | 'scale' => $this->scale($value !== null ? (int) $value : null), |
||
| 207 | 'size' => $this->size($value !== null ? (int) $value : null), |
||
| 208 | 'type' => $this->type($value !== null ? (string) $value : null), |
||
| 209 | 'unsigned' => $this->unsigned((bool) $value), |
||
| 210 | 'values' => $this->values(is_array($value) ? $value : null), |
||
| 211 | default => null, |
||
| 212 | }; |
||
| 213 | } |
||
| 214 | |||
| 215 | if (array_key_exists('default_value_raw', $info)) { |
||
| 216 | $this->defaultValue($this->normalizeDefaultValue($info['default_value_raw'])); |
||
| 217 | } |
||
| 218 | |||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function normalizeDefaultValue(string|null $value): mixed |
||
| 223 | { |
||
| 224 | if ($value === null || $this->computed || preg_match("/^\(?NULL\b/i", $value) === 1) { |
||
| 225 | return null; |
||
| 226 | } |
||
| 227 | |||
| 228 | if (preg_match("/^'(.*)'|^\(([^()]*)\)/s", $value, $matches) === 1) { |
||
| 229 | return $this->phpTypecast($matches[2] ?? str_replace("''", "'", $matches[1])); |
||
| 230 | } |
||
| 231 | |||
| 232 | return new Expression($value); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function phpType(string $value = null): static |
||
| 236 | { |
||
| 237 | $this->phpType = $value; |
||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | public function phpTypecast(mixed $value): mixed |
||
| 242 | { |
||
| 243 | return $value; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function primaryKey(bool $value = true): static |
||
| 247 | { |
||
| 248 | $this->primaryKey = $value; |
||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | public function scale(int $value = null): static |
||
| 253 | { |
||
| 254 | $this->scale = $value; |
||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function size(int $value = null): static |
||
| 259 | { |
||
| 260 | $this->size = $value; |
||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function type(string $value = null): static |
||
| 265 | { |
||
| 266 | $this->type = $value; |
||
| 267 | return $this; |
||
| 268 | } |
||
| 269 | |||
| 270 | public function unsigned(bool $value = true): static |
||
| 274 | } |
||
| 275 | |||
| 276 | public function values(array $value = []): static |
||
| 277 | { |
||
| 278 | $this->values = $value; |
||
| 279 | return $this; |
||
| 280 | } |
||
| 281 | } |
||
| 282 |