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) |
||
| 46 | { |
||
| 47 | return new InvalidArgumentException( |
||
| 48 | get_called_class() . "->$field must $message" |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function setField($field, $value, bool $strict = true) |
||
| 53 | { |
||
| 54 | try { |
||
| 55 | $this->setFieldStrict("$field", $value); |
||
| 56 | } catch (InvalidArgumentException $e) { |
||
| 57 | if ($strict) { |
||
| 58 | throw $e; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | protected function setFieldStrict(string $field, $value) |
||
| 64 | { |
||
| 65 | if ($field == '@context') { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | $type = static::fieldType($field); |
||
| 70 | if (!$type) { |
||
| 71 | throw new InvalidArgumentException( |
||
| 72 | get_called_class() . "->$field does not exist" |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | if (is_null($value)) { |
||
| 77 | $value = null; |
||
| 78 | } elseif (is_array($type)) { # Set or Listing |
||
| 79 | if ($type[0] == 'Set') { |
||
| 80 | if (!($value instanceof Set)) { |
||
| 81 | if (is_array($value)) { |
||
| 82 | $class = 'JSKOS\\' . $type[1]; |
||
| 83 | $value = new Set( |
||
| 84 | array_map(function ($m) use ($class) { |
||
| 85 | if (is_null($m)) { |
||
| 86 | return null; |
||
| 87 | } |
||
| 88 | if ($m instanceof $class) { |
||
| 89 | return $m; |
||
| 90 | } |
||
| 91 | return new $class($m); |
||
| 92 | }, $value) |
||
| 93 | ); |
||
| 94 | } else { |
||
| 95 | throw $this->fieldException($field, "be a Set"); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | # TODO: check member types |
||
| 99 | } else { # Listing |
||
| 100 | if (!($value instanceof Listing)) { |
||
| 101 | if (is_array($value)) { |
||
| 102 | $value = new Listing($value); |
||
| 103 | } else { |
||
| 104 | throw $this->fieldException($field, "be a Listing"); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | # TODO: check member types |
||
| 108 | } |
||
| 109 | } elseif (in_array($type, ['LanguageMapOfStrings', 'LanguageMapOfLists', 'ConceptScheme', 'Item'])) { |
||
| 110 | $type = "JSKOS\\$type"; |
||
| 111 | if (!($value instanceof $type)) { |
||
| 112 | $value = new $type($value); |
||
| 113 | } |
||
| 114 | } elseif ($type != '*' && !DataType::hasType($value, $type)) { |
||
| 115 | throw $this->fieldException($field, "match JSKOS\DataType::is$type"); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->$field = $value; |
||
| 119 | } |
||
| 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 |
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
dieintroduces 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 withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.