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 | /// Constants /// |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Throw a notice when extraneous properties are encountered during validation. |
||
| 18 | */ |
||
| 19 | const FLAG_EXTRA_PROPERTIES_NOTICE = 0x1; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Throw a ValidationException when extraneous properties are encountered during validation. |
||
| 23 | */ |
||
| 24 | const FLAG_EXTRA_PROPERTIES_EXCEPTION = 0x2; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array All the known types. |
||
| 28 | * |
||
| 29 | * If this is ever given some sort of public access then remove the static. |
||
| 30 | */ |
||
| 31 | private static $types = [ |
||
| 32 | 'a' => 'array', |
||
| 33 | 'o' => 'object', |
||
| 34 | 'i' => 'integer', |
||
| 35 | 'int' => 'integer', |
||
| 36 | 's' => 'string', |
||
| 37 | 'str' => 'string', |
||
| 38 | 'f' => 'float', |
||
| 39 | 'b' => 'boolean', |
||
| 40 | 'bool' => 'boolean', |
||
| 41 | 'ts' => 'timestamp', |
||
| 42 | 'dt' => 'datetime' |
||
| 43 | ]; |
||
| 44 | |||
| 45 | private $schema = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int A bitwise combination of the various **Schema::FLAG_*** constants. |
||
| 49 | */ |
||
| 50 | private $flags = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array An array of callbacks that will custom validate the schema. |
||
| 54 | */ |
||
| 55 | private $validators = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string|Validation The name of the class or an instance that will be cloned. |
||
| 59 | */ |
||
| 60 | private $validationClass = Validation::class; |
||
| 61 | |||
| 62 | |||
| 63 | /// Methods /// |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Initialize an instance of a new {@link Schema} class. |
||
| 67 | * |
||
| 68 | * @param array $schema The array schema to validate against. |
||
| 69 | */ |
||
| 70 | 97 | public function __construct($schema = []) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Grab the schema's current description. |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 1 | public function getDescription() { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Set the description for the schema. |
||
| 85 | * |
||
| 86 | * @param string $description The new description. |
||
| 87 | * @throws \InvalidArgumentException Throws an exception when the provided description is not a string. |
||
| 88 | * @return Schema |
||
| 89 | */ |
||
| 90 | 2 | public function setDescription($description) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Return the validation flags. |
||
| 102 | * |
||
| 103 | * @return int Returns a bitwise combination of flags. |
||
| 104 | */ |
||
| 105 | 1 | public function getFlags() { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Set the validation flags. |
||
| 111 | * |
||
| 112 | * @param int $flags One or more of the **Schema::FLAG_*** constants. |
||
| 113 | * @return Schema Returns the current instance for fluent calls. |
||
| 114 | */ |
||
| 115 | 8 | public function setFlags($flags) { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Whether or not the schema has a flag (or combination of flags). |
||
| 126 | * |
||
| 127 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
| 128 | * @return bool Returns **true** if all of the flags are set or **false** otherwise. |
||
| 129 | */ |
||
| 130 | 8 | public function hasFlag($flag) { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Set a flag. |
||
| 136 | * |
||
| 137 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
| 138 | * @param bool $value Either true or false. |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | 1 | public function setFlag($flag, $value) { |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Merge a schema with this one. |
||
| 152 | * |
||
| 153 | * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
||
| 154 | */ |
||
| 155 | 2 | public function merge(Schema $schema) { |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Parse a schema in short form into a full schema array. |
||
| 182 | * |
||
| 183 | * @param array $arr The array to parse into a schema. |
||
| 184 | * @return array The full schema array. |
||
| 185 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
| 186 | */ |
||
| 187 | 97 | public function parse(array $arr) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Parse a schema node. |
||
| 222 | * |
||
| 223 | * @param array $node The node to parse. |
||
| 224 | * @param mixed $value Additional information from the node. |
||
| 225 | * @return array Returns a JSON schema compatible node. |
||
| 226 | */ |
||
| 227 | 92 | private function parseNode($node, $value = null) { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Parse the schema for an object's properties. |
||
| 267 | * |
||
| 268 | * @param array $arr An object property schema. |
||
| 269 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
| 270 | */ |
||
| 271 | 68 | private function parseProperties(array $arr) { |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Parse a short parameter string into a full array parameter. |
||
| 300 | * |
||
| 301 | * @param string $key The short parameter string to parse. |
||
| 302 | * @param array $value An array of other information that might help resolve ambiguity. |
||
| 303 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
| 304 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
| 305 | */ |
||
| 306 | 92 | public function parseShortParam($key, $value = []) { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Add a custom validator to to validate the schema. |
||
| 349 | * |
||
| 350 | * @param string $fieldname The name of the field to validate, if any. |
||
| 351 | * |
||
| 352 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
| 353 | * @param callable $callback The callback to validate with. |
||
| 354 | * @return Schema Returns `$this` for fluent calls. |
||
| 355 | */ |
||
| 356 | 2 | public function addValidator($fieldname, callable $callback) { |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Require one of a given set of fields in the schema. |
||
| 363 | * |
||
| 364 | * @param array $required The field names to require. |
||
| 365 | * @param string $fieldname The name of the field to attach to. |
||
| 366 | * @param int $count The count of required items. |
||
| 367 | * @return Schema Returns `$this` for fluent calls. |
||
| 368 | */ |
||
| 369 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Validate data against the schema. |
||
| 421 | * |
||
| 422 | * @param mixed $data The data to validate. |
||
| 423 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 424 | * @return mixed Returns a cleaned version of the data. |
||
| 425 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
| 426 | */ |
||
| 427 | 75 | public function validate($data, $sparse = false) { |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Validate data against the schema and return the result. |
||
| 441 | * |
||
| 442 | * @param array &$data The data to validate. |
||
| 443 | * @param bool $sparse Whether or not to do a sparse validation. |
||
| 444 | * @return bool Returns true if the data is valid. False otherwise. |
||
| 445 | */ |
||
| 446 | 21 | public function isValid(array &$data, $sparse = false) { |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Validate a field. |
||
| 457 | * |
||
| 458 | * @param mixed $value The value to validate. |
||
| 459 | * @param array|Schema $field Parameters on the field. |
||
| 460 | * @param Validation $validation A validation object to add errors to. |
||
| 461 | * @param string $name The name of the field being validated or an empty string for the root. |
||
| 462 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 463 | * @return mixed Returns a clean version of the value with all extra fields stripped out. |
||
| 464 | */ |
||
| 465 | 75 | private function validateField($value, $field, Validation $validation, $name = '', $sparse = false) { |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Add an invalid type error. |
||
| 523 | * |
||
| 524 | * @param Validation $validation The validation to add the error to. |
||
| 525 | * @param string $name The full field name. |
||
| 526 | * @param string $type The type that was checked. |
||
| 527 | * @return $this |
||
| 528 | */ |
||
| 529 | 36 | protected function addTypeError(Validation $validation, $name, $type) { |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Call all of the validators attached to a field. |
||
| 545 | * |
||
| 546 | * @param mixed $value The field value being validated. |
||
| 547 | * @param string $name The full path to the field. |
||
| 548 | * @param Validation $validation The validation object to add errors. |
||
| 549 | * @internal param array $field The field schema. |
||
| 550 | * @internal param bool $sparse Whether this is a sparse validation. |
||
| 551 | */ |
||
| 552 | 75 | private function callValidators($value, $name, Validation $validation) { |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Validate an array. |
||
| 564 | * |
||
| 565 | * @param mixed &$value The value to validate. |
||
| 566 | * @param array $field The field definition. |
||
| 567 | * @param Validation $validation The validation results to add. |
||
| 568 | * @param string $name The name of the field being validated or an empty string for the root. |
||
| 569 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 570 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 571 | */ |
||
| 572 | 10 | private function validateArray(&$value, array $field, Validation $validation, $name = '', $sparse = false) { |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Validate a boolean value. |
||
| 600 | * |
||
| 601 | * @param mixed &$value The value to validate. |
||
| 602 | * @param array $field The field definition. |
||
| 603 | * @return bool Returns true if the value is valid or false otherwise. |
||
| 604 | * @internal param Validation $validation The validation results to add. |
||
| 605 | */ |
||
| 606 | 19 | private function validateBoolean(&$value, array $field) { |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Validate a date time. |
||
| 626 | * |
||
| 627 | * @param mixed &$value The value to validate. |
||
| 628 | * @param array $field The field definition. |
||
| 629 | * @return bool Returns true if <a href='psi_element://$value'>$value</a> is valid or false otherwise. |
||
| 630 | * is valid or false otherwise. |
||
| 631 | * @internal param Validation $validation The validation results to add. |
||
| 632 | */ |
||
| 633 | 6 | private function validateDatetime(&$value, array $field) { |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Validate a float. |
||
| 659 | * |
||
| 660 | * @param mixed &$value The value to validate. |
||
| 661 | * @param array $field The field definition. |
||
| 662 | * @return bool Returns true if <a href='psi_element://$value'>$value</a> is valid or false otherwise. |
||
| 663 | * is valid or false otherwise. |
||
| 664 | * @internal param Validation $validation The validation results to add. |
||
| 665 | */ |
||
| 666 | 7 | private function validateFloat(&$value, array $field) { |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Validate and integer. |
||
| 680 | * |
||
| 681 | * @param mixed &$value The value to validate. |
||
| 682 | * @param array $field The field definition. |
||
| 683 | * @return bool Returns true if <a href='psi_element://$value'>$value</a> is valid or false otherwise. |
||
| 684 | * is valid or false otherwise. |
||
| 685 | * @internal param Validation $validation The validation results to add. |
||
| 686 | */ |
||
| 687 | 20 | private function validateInteger(&$value, array $field) { |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Validate an object. |
||
| 701 | * |
||
| 702 | * @param mixed &$value The value to validate. |
||
| 703 | * @param array $field The field definition. |
||
| 704 | * @param Validation $validation The validation results to add. |
||
| 705 | * @param string $name The name of the field being validated or an empty string for the root. |
||
| 706 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 707 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 708 | */ |
||
| 709 | 58 | private function validateObject(&$value, array $field, Validation $validation, $name = '', $sparse = false) { |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Validate data against the schema and return the result. |
||
| 721 | * |
||
| 722 | * @param array $data The data to validate. |
||
| 723 | * @param array $field The schema array to validate against. |
||
| 724 | * @param Validation $validation This argument will be filled with the validation result. |
||
| 725 | * @param string $name The path to the current path for nested objects. |
||
| 726 | * @param bool $sparse Whether or not this is a sparse validation. |
||
| 727 | * @return array Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
| 728 | */ |
||
| 729 | 58 | private function validateProperties(array $data, array $field, Validation $validation, $name = '', $sparse = false) { |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Validate a string. |
||
| 782 | * |
||
| 783 | * @param mixed &$value The value to validate. |
||
| 784 | * @param array $field The field definition. |
||
| 785 | * @param Validation $validation The validation results to add. |
||
| 786 | * @param string $name The name of the field being validated. |
||
| 787 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 788 | */ |
||
| 789 | 19 | private function validateString(&$value, array $field, Validation $validation, $name = '') { |
|
| 847 | |||
| 848 | /** |
||
| 849 | * Validate a unix timestamp. |
||
| 850 | * |
||
| 851 | * @param mixed &$value The value to validate. |
||
| 852 | * @param array $field The field definition. |
||
| 853 | * @param Validation $validation The validation results to add. |
||
| 854 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 855 | */ |
||
| 856 | 6 | private function validateTimestamp(&$value, array $field, Validation $validation) { |
|
| 867 | |||
| 868 | /** |
||
| 869 | * Specify data which should be serialized to JSON. |
||
| 870 | * |
||
| 871 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 872 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
| 873 | * which is a value of any type other than a resource. |
||
| 874 | */ |
||
| 875 | 20 | public function jsonSerialize() { |
|
| 884 | |||
| 885 | /** |
||
| 886 | * Look up a type based on its alias. |
||
| 887 | * |
||
| 888 | * @param string $alias The type alias or type name to lookup. |
||
| 889 | * @return mixed |
||
| 890 | */ |
||
| 891 | 9 | private function getType($alias) { |
|
| 901 | |||
| 902 | /** |
||
| 903 | * Look up a value in array. |
||
| 904 | * |
||
| 905 | * @param string|int $key The array key. |
||
| 906 | * @param array $arr The array to search. |
||
| 907 | * @param mixed $default The default if key is not found. |
||
| 908 | * @return mixed Returns the array value or the default. |
||
| 909 | */ |
||
| 910 | 17 | private static function val($key, array $arr, $default = null) { |
|
| 913 | |||
| 914 | /** |
||
| 915 | * Get the class that's used to contain validation information. |
||
| 916 | * |
||
| 917 | * @return Validation|string Returns the validation class. |
||
| 918 | */ |
||
| 919 | 75 | public function getValidationClass() { |
|
| 922 | |||
| 923 | /** |
||
| 924 | * Set the class that's used to contain validation information. |
||
| 925 | * |
||
| 926 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
| 927 | * @return $this |
||
| 928 | */ |
||
| 929 | 1 | public function setValidationClass($class) { |
|
| 937 | |||
| 938 | /** |
||
| 939 | * Create a new validation instance. |
||
| 940 | * |
||
| 941 | * @return Validation Returns a validation object. |
||
| 942 | */ |
||
| 943 | 75 | protected function createValidation() { |
|
| 953 | } |
||
| 954 |
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.