Total Complexity | 50 |
Total Lines | 259 |
Duplicated Lines | 0 % |
Changes | 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 | public function __construct(string $name) |
||
45 | { |
||
46 | $this->name = $name; |
||
47 | } |
||
48 | |||
49 | private bool $allowNull = false; |
||
50 | private bool $autoIncrement = false; |
||
51 | private string|null $comment = null; |
||
52 | private bool $computed = false; |
||
53 | private string|null $dbType = null; |
||
54 | private mixed $defaultValue = null; |
||
55 | private array|null $enumValues = null; |
||
56 | private string|null $extra = null; |
||
57 | private bool $isPrimaryKey = false; |
||
58 | private string $name; |
||
59 | private string|null $phpType = null; |
||
60 | private int|null $precision = null; |
||
61 | private int|null $scale = null; |
||
62 | private int|null $size = null; |
||
63 | private string $type = ''; |
||
64 | private bool $unsigned = false; |
||
65 | |||
66 | public function allowNull(bool $value): void |
||
67 | { |
||
68 | $this->allowNull = $value; |
||
69 | } |
||
70 | |||
71 | public function autoIncrement(bool $value): void |
||
72 | { |
||
73 | $this->autoIncrement = $value; |
||
74 | } |
||
75 | |||
76 | public function comment(string|null $value): void |
||
77 | { |
||
78 | $this->comment = $value; |
||
79 | } |
||
80 | |||
81 | public function computed(bool $value): void |
||
82 | { |
||
83 | $this->computed = $value; |
||
84 | } |
||
85 | |||
86 | public function dbType(string|null $value): void |
||
87 | { |
||
88 | $this->dbType = $value; |
||
89 | } |
||
90 | |||
91 | public function dbTypecast(mixed $value): mixed |
||
92 | { |
||
93 | /** |
||
94 | * The default implementation does the same as casting for PHP, but it should be possible to override this with |
||
95 | * annotation of an explicit PDO type. |
||
96 | */ |
||
97 | return $this->typecast($value); |
||
98 | } |
||
99 | |||
100 | public function defaultValue(mixed $value): void |
||
101 | { |
||
102 | $this->defaultValue = $value; |
||
103 | } |
||
104 | |||
105 | public function enumValues(array|null $value): void |
||
106 | { |
||
107 | $this->enumValues = $value; |
||
108 | } |
||
109 | |||
110 | public function extra(string|null $value): void |
||
111 | { |
||
112 | $this->extra = $value; |
||
113 | } |
||
114 | |||
115 | public function getComment(): string|null |
||
116 | { |
||
117 | return $this->comment; |
||
118 | } |
||
119 | |||
120 | public function getDbType(): string|null |
||
121 | { |
||
122 | return $this->dbType; |
||
123 | } |
||
124 | |||
125 | public function getDefaultValue(): mixed |
||
126 | { |
||
127 | return $this->defaultValue; |
||
128 | } |
||
129 | |||
130 | public function getEnumValues(): array|null |
||
131 | { |
||
132 | return $this->enumValues; |
||
133 | } |
||
134 | |||
135 | public function getExtra(): string|null |
||
136 | { |
||
137 | return $this->extra; |
||
138 | } |
||
139 | |||
140 | public function getName(): string |
||
141 | { |
||
142 | return $this->name; |
||
143 | } |
||
144 | |||
145 | public function getPrecision(): int|null |
||
146 | { |
||
147 | return $this->precision; |
||
148 | } |
||
149 | |||
150 | public function getPhpType(): string|null |
||
151 | { |
||
152 | return $this->phpType; |
||
153 | } |
||
154 | |||
155 | public function getScale(): int|null |
||
158 | } |
||
159 | |||
160 | public function getSize(): int|null |
||
161 | { |
||
162 | return $this->size; |
||
163 | } |
||
164 | |||
165 | public function getType(): string |
||
166 | { |
||
167 | return $this->type; |
||
168 | } |
||
169 | |||
170 | public function isAllowNull(): bool |
||
173 | } |
||
174 | |||
175 | public function isAutoIncrement(): bool |
||
176 | { |
||
177 | return $this->autoIncrement; |
||
178 | } |
||
179 | |||
180 | public function isComputed(): bool |
||
181 | { |
||
182 | return $this->computed; |
||
183 | } |
||
184 | |||
185 | public function isPrimaryKey(): bool |
||
186 | { |
||
187 | return $this->isPrimaryKey; |
||
188 | } |
||
189 | |||
190 | public function isUnsigned(): bool |
||
191 | { |
||
192 | return $this->unsigned; |
||
193 | } |
||
194 | |||
195 | public function phpType(string|null $value): void |
||
196 | { |
||
197 | $this->phpType = $value; |
||
198 | } |
||
199 | |||
200 | public function phpTypecast(mixed $value): mixed |
||
201 | { |
||
202 | return $this->typecast($value); |
||
203 | } |
||
204 | |||
205 | public function precision(int|null $value): void |
||
206 | { |
||
207 | $this->precision = $value; |
||
208 | } |
||
209 | |||
210 | public function primaryKey(bool $value): void |
||
211 | { |
||
212 | $this->isPrimaryKey = $value; |
||
213 | } |
||
214 | |||
215 | public function scale(int|null $value): void |
||
216 | { |
||
217 | $this->scale = $value; |
||
218 | } |
||
219 | |||
220 | public function size(int|null $value): void |
||
221 | { |
||
222 | $this->size = $value; |
||
223 | } |
||
224 | |||
225 | public function type(string $value): void |
||
226 | { |
||
227 | $this->type = $value; |
||
228 | } |
||
229 | |||
230 | public function unsigned(bool $value): void |
||
231 | { |
||
232 | $this->unsigned = $value; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Converts the input value according to {@see phpType} after retrieval from the database. |
||
237 | * |
||
238 | * If the value is null or an {@see Expression}, it won't be converted. |
||
239 | * |
||
240 | * @param mixed $value The value to be converted. |
||
241 | * |
||
242 | * @return mixed The converted value. |
||
243 | */ |
||
244 | protected function typecast(mixed $value): mixed |
||
301 | } |
||
302 | } |
||
303 |