Total Complexity | 41 |
Total Lines | 234 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractColumnSchema 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 AbstractColumnSchema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | abstract class AbstractColumnSchema implements ColumnSchemaInterface |
||
43 | { |
||
44 | private bool $allowNull = false; |
||
45 | private bool $autoIncrement = false; |
||
46 | private string|null $comment = null; |
||
47 | private bool $computed = false; |
||
48 | private string|null $dbType = null; |
||
49 | private mixed $defaultValue = null; |
||
50 | private array|null $enumValues = null; |
||
51 | private string|null $extra = null; |
||
52 | private bool $isPrimaryKey = false; |
||
53 | private string|null $phpType = null; |
||
54 | private int|null $precision = null; |
||
55 | private int|null $scale = null; |
||
56 | private int|null $size = null; |
||
57 | private string $type = ''; |
||
58 | private bool $unsigned = false; |
||
59 | |||
60 | public function __construct(private string $name) |
||
61 | { |
||
62 | } |
||
63 | |||
64 | public function allowNull(bool $value): void |
||
65 | { |
||
66 | $this->allowNull = $value; |
||
67 | } |
||
68 | |||
69 | public function autoIncrement(bool $value): void |
||
70 | { |
||
71 | $this->autoIncrement = $value; |
||
72 | } |
||
73 | |||
74 | public function comment(string|null $value): void |
||
75 | { |
||
76 | $this->comment = $value; |
||
77 | } |
||
78 | |||
79 | public function computed(bool $value): void |
||
80 | { |
||
81 | $this->computed = $value; |
||
82 | } |
||
83 | |||
84 | public function dbType(string|null $value): void |
||
85 | { |
||
86 | $this->dbType = $value; |
||
87 | } |
||
88 | |||
89 | public function dbTypecast(mixed $value): mixed |
||
90 | { |
||
91 | /** |
||
92 | * The default implementation does the same as casting for PHP, but it should be possible to override this with |
||
93 | * annotation of an explicit PDO type. |
||
94 | */ |
||
95 | return $this->typecast($value); |
||
96 | } |
||
97 | |||
98 | public function defaultValue(mixed $value): void |
||
99 | { |
||
100 | $this->defaultValue = $value; |
||
101 | } |
||
102 | |||
103 | public function enumValues(array|null $value): void |
||
104 | { |
||
105 | $this->enumValues = $value; |
||
106 | } |
||
107 | |||
108 | public function extra(string|null $value): void |
||
109 | { |
||
110 | $this->extra = $value; |
||
111 | } |
||
112 | |||
113 | public function getComment(): string|null |
||
114 | { |
||
115 | return $this->comment; |
||
116 | } |
||
117 | |||
118 | public function getDbType(): string|null |
||
119 | { |
||
120 | return $this->dbType; |
||
121 | } |
||
122 | |||
123 | public function getDefaultValue(): mixed |
||
124 | { |
||
125 | return $this->defaultValue; |
||
126 | } |
||
127 | |||
128 | public function getEnumValues(): array|null |
||
129 | { |
||
130 | return $this->enumValues; |
||
131 | } |
||
132 | |||
133 | public function getExtra(): string|null |
||
134 | { |
||
135 | return $this->extra; |
||
136 | } |
||
137 | |||
138 | public function getName(): string |
||
139 | { |
||
140 | return $this->name; |
||
141 | } |
||
142 | |||
143 | public function getPrecision(): int|null |
||
144 | { |
||
145 | return $this->precision; |
||
146 | } |
||
147 | |||
148 | public function getPhpType(): string|null |
||
149 | { |
||
150 | return $this->phpType; |
||
151 | } |
||
152 | |||
153 | public function getScale(): int|null |
||
156 | } |
||
157 | |||
158 | public function getSize(): int|null |
||
159 | { |
||
160 | return $this->size; |
||
161 | } |
||
162 | |||
163 | public function getType(): string |
||
164 | { |
||
165 | return $this->type; |
||
166 | } |
||
167 | |||
168 | public function isAllowNull(): bool |
||
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->isPrimaryKey; |
||
186 | } |
||
187 | |||
188 | public function isUnsigned(): bool |
||
189 | { |
||
190 | return $this->unsigned; |
||
191 | } |
||
192 | |||
193 | public function phpType(string|null $value): void |
||
194 | { |
||
195 | $this->phpType = $value; |
||
196 | } |
||
197 | |||
198 | public function phpTypecast(mixed $value): mixed |
||
199 | { |
||
200 | return $this->typecast($value); |
||
201 | } |
||
202 | |||
203 | public function precision(int|null $value): void |
||
204 | { |
||
205 | $this->precision = $value; |
||
206 | } |
||
207 | |||
208 | public function primaryKey(bool $value): void |
||
209 | { |
||
210 | $this->isPrimaryKey = $value; |
||
211 | } |
||
212 | |||
213 | public function scale(int|null $value): void |
||
214 | { |
||
215 | $this->scale = $value; |
||
216 | } |
||
217 | |||
218 | public function size(int|null $value): void |
||
219 | { |
||
220 | $this->size = $value; |
||
221 | } |
||
222 | |||
223 | public function type(string $value): void |
||
226 | } |
||
227 | |||
228 | public function unsigned(bool $value): void |
||
229 | { |
||
230 | $this->unsigned = $value; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Converts the input value according to {@see phpType} after retrieval from the database. |
||
235 | * |
||
236 | * If the value is null or an {@see Expression}, it won't be converted. |
||
237 | * |
||
238 | * @param mixed $value The value to be converted. |
||
239 | * |
||
240 | * @return mixed The converted value. |
||
241 | */ |
||
242 | protected function typecast(mixed $value): mixed |
||
243 | { |
||
244 | if ( |
||
245 | $value === null |
||
246 | || $value === '' && !in_array($this->type, [ |
||
247 | SchemaInterface::TYPE_TEXT, |
||
248 | SchemaInterface::TYPE_STRING, |
||
249 | SchemaInterface::TYPE_BINARY, |
||
250 | SchemaInterface::TYPE_CHAR, |
||
251 | ], true) |
||
252 | ) { |
||
253 | return null; |
||
254 | } |
||
255 | |||
279 |