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