Complex classes like DataType 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 DataType, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types = 1); |
||
25 | abstract class DataType extends PrettyJsonSerializable |
||
26 | { |
||
27 | use Constructor; |
||
28 | |||
29 | const FIELDS = []; |
||
30 | |||
31 | /** |
||
32 | * Get field definition from FIELDS, including parent classes. |
||
33 | */ |
||
34 | protected static function fieldType(string $field) |
||
44 | |||
45 | private function fieldException(string $field, string $message) |
||
51 | |||
52 | protected function setField($field, $value, bool $strict = true) |
||
62 | |||
63 | protected function setFieldStrict(string $field, $value) |
||
120 | |||
121 | public function __set($field, $value) |
||
125 | |||
126 | public function &__get($field) |
||
143 | |||
144 | public function __isset($field): bool |
||
148 | |||
149 | /** |
||
150 | * Check whether a given value looks like an URI. |
||
151 | */ |
||
152 | public static function isURI($uri): bool |
||
156 | |||
157 | /** |
||
158 | * Check whether a given value looks like an http/https URL. |
||
159 | */ |
||
160 | public static function isURL($url): bool |
||
166 | |||
167 | /** |
||
168 | * Check whether a given value looks like a date. |
||
169 | */ |
||
170 | public static function isDate($date): bool |
||
174 | |||
175 | /** |
||
176 | * Check whether a given value looks like a language tag. |
||
177 | */ |
||
178 | public static function isLanguage($language): bool |
||
182 | |||
183 | /** |
||
184 | * Check whether a given value looks like a language range. |
||
185 | */ |
||
186 | public static function isLanguageRange($range): bool |
||
190 | |||
191 | /** |
||
192 | * Check whether a given value looks like a language or language range. |
||
193 | */ |
||
194 | public static function isLanguageOrRange($language): bool |
||
200 | |||
201 | /** |
||
202 | * Check whether a given value is a string. |
||
203 | */ |
||
204 | public static function isString($string): bool |
||
208 | |||
209 | /** |
||
210 | * Check whether a given value is a non-negative integer |
||
211 | */ |
||
212 | public static function isNonNegativeInteger($value): bool |
||
216 | |||
217 | /** |
||
218 | * Check whether a given value is a percentage |
||
219 | */ |
||
220 | public static function isPercentage($value): bool |
||
224 | |||
225 | /** |
||
226 | * Check whether a given value is a concept scheme |
||
227 | */ |
||
228 | public static function isConceptScheme($scheme): bool |
||
232 | |||
233 | /** |
||
234 | * Check whether a given value is of given type. |
||
235 | */ |
||
236 | public static function hasType($value, string $type): bool |
||
241 | } |
||
242 |