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(string $field, string $message) |
||
57 | |||
58 | protected function setField(string $field, $value, bool $strict = true) |
||
113 | |||
114 | public function __set($field, $value) |
||
118 | |||
119 | public function &__get($field) |
||
136 | |||
137 | public function __isset($field): bool |
||
141 | |||
142 | /** |
||
143 | * Check whether a given value looks like an URI. |
||
144 | */ |
||
145 | public static function isURI($uri): bool |
||
149 | |||
150 | /** |
||
151 | * Check whether a given value looks like an http/https URL. |
||
152 | */ |
||
153 | public static function isURL($url): bool |
||
159 | |||
160 | /** |
||
161 | * Check whether a given value looks like a date. |
||
162 | */ |
||
163 | public static function isDate($date): bool |
||
167 | |||
168 | /** |
||
169 | * Check whether a given value looks like a language tag. |
||
170 | */ |
||
171 | public static function isLanguage($language): bool |
||
175 | |||
176 | /** |
||
177 | * Check whether a given value looks like a language range. |
||
178 | */ |
||
179 | public static function isLanguageRange($range): bool |
||
183 | |||
184 | /** |
||
185 | * Check whether a given value looks like a language or language range. |
||
186 | */ |
||
187 | public static function isLanguageOrRange($language): bool |
||
193 | |||
194 | /** |
||
195 | * Check whether a given value is a string. |
||
196 | */ |
||
197 | public static function isString($string): bool |
||
201 | |||
202 | /** |
||
203 | * Check whether a given value is a non-negative integer |
||
204 | */ |
||
205 | public static function isNonNegativeInteger($value): bool |
||
209 | |||
210 | /** |
||
211 | * Check whether a given value is a percentage |
||
212 | */ |
||
213 | public static function isPercentage($value): bool |
||
217 | |||
218 | /** |
||
219 | * Check whether a given value is a concept scheme |
||
220 | */ |
||
221 | public static function isConceptScheme($scheme): bool |
||
225 | |||
226 | /** |
||
227 | * Check whether a given value is of given type. |
||
228 | */ |
||
229 | public static function hasType($value, string $type): bool |
||
234 | } |
||
235 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.