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, bool $strict=false) |
||
50 | |||
51 | private function fieldException($field, $message) |
||
57 | |||
58 | protected function setField(string $field, $value, bool $strict=true) |
||
117 | |||
118 | public function __set($field, $value) |
||
122 | |||
123 | public function &__get($field) |
||
140 | |||
141 | public function __isset($field): bool |
||
145 | |||
146 | /** |
||
147 | * Check whether a given value looks like an URI. |
||
148 | */ |
||
149 | public static function isURI($uri): bool |
||
153 | |||
154 | /** |
||
155 | * Check whether a given value looks like an http/https URL. |
||
156 | */ |
||
157 | public static function isURL($url): bool |
||
163 | |||
164 | /** |
||
165 | * Check whether a given value looks like a date. |
||
166 | */ |
||
167 | public static function isDate($date): bool |
||
171 | |||
172 | /** |
||
173 | * Check whether a given value looks like a language tag. |
||
174 | */ |
||
175 | public static function isLanguage($language): bool |
||
179 | |||
180 | /** |
||
181 | * Check whether a given value looks like a language range. |
||
182 | */ |
||
183 | public static function isLanguageRange($range): bool |
||
187 | |||
188 | /** |
||
189 | * Check whether a given value looks like a language or language range. |
||
190 | */ |
||
191 | public static function isLanguageOrRange($language): bool |
||
197 | |||
198 | /** |
||
199 | * Check whether a given value is a string. |
||
200 | */ |
||
201 | public static function isString($string): bool |
||
205 | |||
206 | /** |
||
207 | * Check whether a given value is a concept scheme |
||
208 | */ |
||
209 | public static function isConceptScheme($scheme): bool |
||
213 | |||
214 | /** |
||
215 | * Check whether a given value is of given type. |
||
216 | */ |
||
217 | public static function hasType($value, string $type): bool |
||
222 | } |
||
223 |
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.