Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Schema implements \JsonSerializable { |
||
| 14 | /** |
||
| 15 | * Trigger a notice when extraneous properties are encountered during validation. |
||
| 16 | */ |
||
| 17 | const VALIDATE_EXTRA_PROPERTY_NOTICE = 0x1; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Throw a ValidationException when extraneous properties are encountered during validation. |
||
| 21 | */ |
||
| 22 | const VALIDATE_EXTRA_PROPERTY_EXCEPTION = 0x2; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array All the known types. |
||
| 26 | * |
||
| 27 | * If this is ever given some sort of public access then remove the static. |
||
| 28 | */ |
||
| 29 | private static $types = [ |
||
| 30 | 'array' => ['a'], |
||
| 31 | 'object' => ['o'], |
||
| 32 | 'integer' => ['i', 'int'], |
||
| 33 | 'string' => ['s', 'str'], |
||
| 34 | 'number' => ['f', 'float'], |
||
| 35 | 'boolean' => ['b', 'bool'], |
||
| 36 | 'timestamp' => ['ts'], |
||
| 37 | 'datetime' => ['dt'], |
||
| 38 | 'null' => ['n'] |
||
| 39 | ]; |
||
| 40 | |||
| 41 | private $schema = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int A bitwise combination of the various **Schema::FLAG_*** constants. |
||
| 45 | */ |
||
| 46 | private $flags = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array An array of callbacks that will custom validate the schema. |
||
| 50 | */ |
||
| 51 | private $validators = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string|Validation The name of the class or an instance that will be cloned. |
||
| 55 | */ |
||
| 56 | private $validationClass = Validation::class; |
||
| 57 | |||
| 58 | |||
| 59 | /// Methods /// |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Initialize an instance of a new {@link Schema} class. |
||
| 63 | * |
||
| 64 | * @param array $schema The array schema to validate against. |
||
| 65 | */ |
||
| 66 | 155 | public function __construct($schema = []) { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Grab the schema's current description. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 1 | public function getDescription() { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Set the description for the schema. |
||
| 81 | * |
||
| 82 | * @param string $description The new description. |
||
| 83 | * @throws \InvalidArgumentException Throws an exception when the provided description is not a string. |
||
| 84 | * @return Schema |
||
| 85 | */ |
||
| 86 | 2 | public function setDescription($description) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Get a schema field. |
||
| 98 | * |
||
| 99 | * @param string|array $path The JSON schema path of the field with parts separated by dots. |
||
| 100 | * @param mixed $default The value to return if the field isn't found. |
||
| 101 | * @return mixed Returns the field value or `$default`. |
||
| 102 | */ |
||
| 103 | 4 | public function getField($path, $default = null) { |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Set a schema field. |
||
| 123 | * |
||
| 124 | * @param string|array $path The JSON schema path of the field with parts separated by dots. |
||
| 125 | * @param mixed $value The new value. |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | 3 | public function setField($path, $value) { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Get the ID for the schema. |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | 2 | public function getID() { |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Set the ID for the schema. |
||
| 163 | * |
||
| 164 | * @param string $ID The new ID. |
||
| 165 | * @throws \InvalidArgumentException Throws an exception when the provided ID is not a string. |
||
| 166 | * @return Schema |
||
| 167 | */ |
||
| 168 | public function setID($ID) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return the validation flags. |
||
| 180 | * |
||
| 181 | * @return int Returns a bitwise combination of flags. |
||
| 182 | */ |
||
| 183 | 1 | public function getFlags() { |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Set the validation flags. |
||
| 189 | * |
||
| 190 | * @param int $flags One or more of the **Schema::FLAG_*** constants. |
||
| 191 | * @return Schema Returns the current instance for fluent calls. |
||
| 192 | */ |
||
| 193 | 8 | public function setFlags($flags) { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Whether or not the schema has a flag (or combination of flags). |
||
| 204 | * |
||
| 205 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
| 206 | * @return bool Returns **true** if all of the flags are set or **false** otherwise. |
||
| 207 | */ |
||
| 208 | 8 | public function hasFlag($flag) { |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Set a flag. |
||
| 214 | * |
||
| 215 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
| 216 | * @param bool $value Either true or false. |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | 1 | public function setFlag($flag, $value) { |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Merge a schema with this one. |
||
| 230 | * |
||
| 231 | * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | 3 | public function merge(Schema $schema) { |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Add another schema to this one. |
||
| 241 | * |
||
| 242 | * Adding schemas together is analogous to array addition. When you add a schema it will only add missing information. |
||
| 243 | * |
||
| 244 | * @param Schema $schema The schema to add. |
||
| 245 | * @param bool $addProperties Whether to add properties that don't exist in this schema. |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | 3 | public function add(Schema $schema, $addProperties = false) { |
|
| 252 | |||
| 253 | /** |
||
| 254 | * The internal implementation of schema merging. |
||
| 255 | * |
||
| 256 | * @param array &$target The target of the merge. |
||
| 257 | * @param array $source The source of the merge. |
||
| 258 | * @param bool $overwrite Whether or not to replace values. |
||
| 259 | * @param bool $addProperties Whether or not to add object properties to the target. |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | 6 | private function mergeInternal(array &$target, array $source, $overwrite = true, $addProperties = true) { |
|
| 314 | |||
| 315 | // public function overlay(Schema $schema ) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the internal schema array. |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | * @see Schema::jsonSerialize() |
||
| 322 | */ |
||
| 323 | 15 | public function getSchemaArray() { |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Parse a short schema and return the associated schema. |
||
| 329 | * |
||
| 330 | * @param array $arr The schema array. |
||
| 331 | * @param mixed ...$args Constructor arguments for the schema instance. |
||
| 332 | * @return static Returns a new schema. |
||
| 333 | */ |
||
| 334 | 150 | public static function parse(array $arr, ...$args) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Parse a schema in short form into a full schema array. |
||
| 342 | * |
||
| 343 | * @param array $arr The array to parse into a schema. |
||
| 344 | * @return array The full schema array. |
||
| 345 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
| 346 | */ |
||
| 347 | 150 | protected function parseInternal(array $arr) { |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Parse a schema node. |
||
| 382 | * |
||
| 383 | * @param array $node The node to parse. |
||
| 384 | * @param mixed $value Additional information from the node. |
||
| 385 | * @return array Returns a JSON schema compatible node. |
||
| 386 | */ |
||
| 387 | 144 | private function parseNode($node, $value = null) { |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Parse the schema for an object's properties. |
||
| 440 | * |
||
| 441 | * @param array $arr An object property schema. |
||
| 442 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
| 443 | */ |
||
| 444 | 92 | private function parseProperties(array $arr) { |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Parse a short parameter string into a full array parameter. |
||
| 473 | * |
||
| 474 | * @param string $key The short parameter string to parse. |
||
| 475 | * @param array $value An array of other information that might help resolve ambiguity. |
||
| 476 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
| 477 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
| 478 | */ |
||
| 479 | 144 | public function parseShortParam($key, $value = []) { |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Add a custom validator to to validate the schema. |
||
| 540 | * |
||
| 541 | * @param string $fieldname The name of the field to validate, if any. |
||
| 542 | * |
||
| 543 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
| 544 | * @param callable $callback The callback to validate with. |
||
| 545 | * @return Schema Returns `$this` for fluent calls. |
||
| 546 | */ |
||
| 547 | 2 | public function addValidator($fieldname, callable $callback) { |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Require one of a given set of fields in the schema. |
||
| 554 | * |
||
| 555 | * @param array $required The field names to require. |
||
| 556 | * @param string $fieldname The name of the field to attach to. |
||
| 557 | * @param int $count The count of required items. |
||
| 558 | * @return Schema Returns `$this` for fluent calls. |
||
| 559 | */ |
||
| 560 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Validate data against the schema. |
||
| 612 | * |
||
| 613 | * @param mixed $data The data to validate. |
||
| 614 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 615 | * @return mixed Returns a cleaned version of the data. |
||
| 616 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
| 617 | */ |
||
| 618 | 120 | public function validate($data, $sparse = false) { |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Validate data against the schema and return the result. |
||
| 637 | * |
||
| 638 | * @param mixed $data The data to validate. |
||
| 639 | * @param bool $sparse Whether or not to do a sparse validation. |
||
| 640 | * @return bool Returns true if the data is valid. False otherwise. |
||
| 641 | */ |
||
| 642 | 33 | public function isValid($data, $sparse = false) { |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Validate a field. |
||
| 653 | * |
||
| 654 | * @param mixed $value The value to validate. |
||
| 655 | * @param ValidationField $field A validation object to add errors to. |
||
| 656 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 657 | * @return mixed|Invalid Returns a clean version of the value with all extra fields stripped out or invalid if the value |
||
| 658 | * is completely invalid. |
||
| 659 | */ |
||
| 660 | 120 | protected function validateField($value, ValidationField $field, $sparse = false) { |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Validate an array. |
||
| 721 | * |
||
| 722 | * @param mixed $value The value to validate. |
||
| 723 | * @param ValidationField $field The validation results to add. |
||
| 724 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 725 | * @return array|Invalid Returns an array or invalid if validation fails. |
||
| 726 | */ |
||
| 727 | 15 | protected function validateArray($value, ValidationField $field, $sparse = false) { |
|
| 759 | |||
| 760 | /** |
||
| 761 | * Validate a boolean value. |
||
| 762 | * |
||
| 763 | * @param mixed $value The value to validate. |
||
| 764 | * @param ValidationField $field The validation results to add. |
||
| 765 | * @return bool|Invalid Returns the cleaned value or invalid if validation fails. |
||
| 766 | */ |
||
| 767 | 21 | protected function validateBoolean($value, ValidationField $field) { |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Validate a date time. |
||
| 778 | * |
||
| 779 | * @param mixed $value The value to validate. |
||
| 780 | * @param ValidationField $field The validation results to add. |
||
| 781 | * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid. |
||
| 782 | */ |
||
| 783 | 13 | protected function validateDatetime($value, ValidationField $field) { |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Validate a float. |
||
| 811 | * |
||
| 812 | * @param mixed $value The value to validate. |
||
| 813 | * @param ValidationField $field The validation results to add. |
||
| 814 | * @return float|Invalid Returns a number or **null** if validation fails. |
||
| 815 | */ |
||
| 816 | 9 | protected function validateNumber($value, ValidationField $field) { |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Validate and integer. |
||
| 827 | * |
||
| 828 | * @param mixed $value The value to validate. |
||
| 829 | * @param ValidationField $field The validation results to add. |
||
| 830 | * @return int|Invalid Returns the cleaned value or **null** if validation fails. |
||
| 831 | */ |
||
| 832 | 25 | protected function validateInteger($value, ValidationField $field) { |
|
| 841 | |||
| 842 | /** |
||
| 843 | * Validate an object. |
||
| 844 | * |
||
| 845 | * @param mixed $value The value to validate. |
||
| 846 | * @param ValidationField $field The validation results to add. |
||
| 847 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 848 | * @return object|Invalid Returns a clean object or **null** if validation fails. |
||
| 849 | */ |
||
| 850 | 80 | protected function validateObject($value, ValidationField $field, $sparse = false) { |
|
| 862 | |||
| 863 | /** |
||
| 864 | * Validate data against the schema and return the result. |
||
| 865 | * |
||
| 866 | * @param array|\ArrayAccess $data The data to validate. |
||
| 867 | * @param ValidationField $field This argument will be filled with the validation result. |
||
| 868 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 869 | * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
| 870 | * or invalid if there are no valid properties. |
||
| 871 | */ |
||
| 872 | 79 | protected function validateProperties($data, ValidationField $field, $sparse = false) { |
|
| 929 | |||
| 930 | /** |
||
| 931 | * Validate a string. |
||
| 932 | * |
||
| 933 | * @param mixed $value The value to validate. |
||
| 934 | * @param ValidationField $field The validation results to add. |
||
| 935 | * @return string|Invalid Returns the valid string or **null** if validation fails. |
||
| 936 | */ |
||
| 937 | 55 | protected function validateString($value, ValidationField $field) { |
|
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Validate a unix timestamp. |
||
| 1029 | * |
||
| 1030 | * @param mixed $value The value to validate. |
||
| 1031 | * @param ValidationField $field The field being validated. |
||
| 1032 | * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate. |
||
| 1033 | */ |
||
| 1034 | 8 | protected function validateTimestamp($value, ValidationField $field) { |
|
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Validate a null value. |
||
| 1048 | * |
||
| 1049 | * @param mixed $value The value to validate. |
||
| 1050 | * @param ValidationField $field The error collector for the field. |
||
| 1051 | * @return null|Invalid Returns **null** or invalid. |
||
| 1052 | */ |
||
| 1053 | protected function validateNull($value, ValidationField $field) { |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Validate a value against an enum. |
||
| 1063 | * |
||
| 1064 | * @param mixed $value The value to test. |
||
| 1065 | * @param ValidationField $field The validation object for adding errors. |
||
| 1066 | * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise. |
||
| 1067 | */ |
||
| 1068 | 118 | protected function validateEnum($value, ValidationField $field) { |
|
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Call all of the validators attached to a field. |
||
| 1090 | * |
||
| 1091 | * @param mixed $value The field value being validated. |
||
| 1092 | * @param ValidationField $field The validation object to add errors. |
||
| 1093 | */ |
||
| 1094 | 118 | protected function callValidators($value, ValidationField $field) { |
|
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Specify data which should be serialized to JSON. |
||
| 1117 | * |
||
| 1118 | * This method specifically returns data compatible with the JSON schema format. |
||
| 1119 | * |
||
| 1120 | * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource. |
||
| 1121 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 1122 | * @link http://json-schema.org/ |
||
| 1123 | */ |
||
| 1124 | public function jsonSerialize() { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Look up a type based on its alias. |
||
| 1162 | * |
||
| 1163 | * @param string $alias The type alias or type name to lookup. |
||
| 1164 | * @return mixed |
||
| 1165 | */ |
||
| 1166 | 143 | protected function getType($alias) { |
|
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Get the class that's used to contain validation information. |
||
| 1180 | * |
||
| 1181 | * @return Validation|string Returns the validation class. |
||
| 1182 | */ |
||
| 1183 | 120 | public function getValidationClass() { |
|
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Set the class that's used to contain validation information. |
||
| 1189 | * |
||
| 1190 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
| 1191 | * @return $this |
||
| 1192 | */ |
||
| 1193 | 1 | public function setValidationClass($class) { |
|
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Create a new validation instance. |
||
| 1204 | * |
||
| 1205 | * @return Validation Returns a validation object. |
||
| 1206 | */ |
||
| 1207 | 120 | protected function createValidation() { |
|
| 1217 | |||
| 1218 | /** |
||
| 1219 | * Check whether or not a value is an array or accessible like an array. |
||
| 1220 | * |
||
| 1221 | * @param mixed $value The value to check. |
||
| 1222 | * @return bool Returns **true** if the value can be used like an array or **false** otherwise. |
||
| 1223 | */ |
||
| 1224 | 80 | private function isArray($value) { |
|
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Cast a value to an array. |
||
| 1230 | * |
||
| 1231 | * @param \Traversable $value The value to convert. |
||
| 1232 | * @return array Returns an array. |
||
| 1233 | */ |
||
| 1234 | private function toArray(\Traversable $value) { |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Return a sparse version of this schema. |
||
| 1243 | * |
||
| 1244 | * A sparse schema has no required properties. |
||
| 1245 | * |
||
| 1246 | * @return Schema Returns a new sparse schema. |
||
| 1247 | */ |
||
| 1248 | 2 | public function withSparse() { |
|
| 1252 | |||
| 1253 | /** |
||
| 1254 | * The internal implementation of `Schema::withSparse()`. |
||
| 1255 | * |
||
| 1256 | * @param array|Schema $schema The schema to make sparse. |
||
| 1257 | * @param \SplObjectStorage $schemas Collected sparse schemas that have already been made. |
||
| 1258 | * @return mixed |
||
| 1259 | */ |
||
| 1260 | 2 | private function withSparseInternal($schema, \SplObjectStorage $schemas) { |
|
| 1288 | } |
||
| 1289 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.