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 | 142 | 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 | * Return the validation flags. |
||
98 | * |
||
99 | * @return int Returns a bitwise combination of flags. |
||
100 | */ |
||
101 | 1 | public function getFlags() { |
|
104 | |||
105 | /** |
||
106 | * Set the validation flags. |
||
107 | * |
||
108 | * @param int $flags One or more of the **Schema::FLAG_*** constants. |
||
109 | * @return Schema Returns the current instance for fluent calls. |
||
110 | */ |
||
111 | 8 | public function setFlags($flags) { |
|
119 | |||
120 | /** |
||
121 | * Whether or not the schema has a flag (or combination of flags). |
||
122 | * |
||
123 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
124 | * @return bool Returns **true** if all of the flags are set or **false** otherwise. |
||
125 | */ |
||
126 | 8 | public function hasFlag($flag) { |
|
129 | |||
130 | /** |
||
131 | * Set a flag. |
||
132 | * |
||
133 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
134 | * @param bool $value Either true or false. |
||
135 | * @return $this |
||
136 | */ |
||
137 | 1 | public function setFlag($flag, $value) { |
|
145 | |||
146 | /** |
||
147 | * Merge a schema with this one. |
||
148 | * |
||
149 | * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
||
150 | */ |
||
151 | 2 | public function merge(Schema $schema) { |
|
175 | |||
176 | /** |
||
177 | * Returns the internal schema array. |
||
178 | * |
||
179 | * @return array |
||
180 | * @see Schema::jsonSerialize() |
||
181 | */ |
||
182 | 11 | public function getSchemaArray() { |
|
185 | |||
186 | /** |
||
187 | * Parse a schema in short form into a full schema array. |
||
188 | * |
||
189 | * @param array $arr The array to parse into a schema. |
||
190 | * @return array The full schema array. |
||
191 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
192 | */ |
||
193 | 142 | protected function parse(array $arr) { |
|
225 | |||
226 | /** |
||
227 | * Parse a schema node. |
||
228 | * |
||
229 | * @param array $node The node to parse. |
||
230 | * @param mixed $value Additional information from the node. |
||
231 | * @return array Returns a JSON schema compatible node. |
||
232 | */ |
||
233 | 137 | private function parseNode($node, $value = null) { |
|
280 | |||
281 | /** |
||
282 | * Parse the schema for an object's properties. |
||
283 | * |
||
284 | * @param array $arr An object property schema. |
||
285 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
286 | */ |
||
287 | 87 | private function parseProperties(array $arr) { |
|
313 | |||
314 | /** |
||
315 | * Parse a short parameter string into a full array parameter. |
||
316 | * |
||
317 | * @param string $key The short parameter string to parse. |
||
318 | * @param array $value An array of other information that might help resolve ambiguity. |
||
319 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
320 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
321 | */ |
||
322 | 135 | public function parseShortParam($key, $value = []) { |
|
380 | |||
381 | /** |
||
382 | * Add a custom validator to to validate the schema. |
||
383 | * |
||
384 | * @param string $fieldname The name of the field to validate, if any. |
||
385 | * |
||
386 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
387 | * @param callable $callback The callback to validate with. |
||
388 | * @return Schema Returns `$this` for fluent calls. |
||
389 | */ |
||
390 | 2 | public function addValidator($fieldname, callable $callback) { |
|
394 | |||
395 | /** |
||
396 | * Require one of a given set of fields in the schema. |
||
397 | * |
||
398 | * @param array $required The field names to require. |
||
399 | * @param string $fieldname The name of the field to attach to. |
||
400 | * @param int $count The count of required items. |
||
401 | * @return Schema Returns `$this` for fluent calls. |
||
402 | */ |
||
403 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
452 | |||
453 | /** |
||
454 | * Validate data against the schema. |
||
455 | * |
||
456 | * @param mixed $data The data to validate. |
||
457 | * @param bool $sparse Whether or not this is a sparse validation. |
||
458 | * @return mixed Returns a cleaned version of the data. |
||
459 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
460 | */ |
||
461 | 116 | public function validate($data, $sparse = false) { |
|
477 | |||
478 | /** |
||
479 | * Validate data against the schema and return the result. |
||
480 | * |
||
481 | * @param mixed $data The data to validate. |
||
482 | * @param bool $sparse Whether or not to do a sparse validation. |
||
483 | * @return bool Returns true if the data is valid. False otherwise. |
||
484 | */ |
||
485 | 33 | public function isValid($data, $sparse = false) { |
|
493 | |||
494 | /** |
||
495 | * Validate a field. |
||
496 | * |
||
497 | * @param mixed $value The value to validate. |
||
498 | * @param ValidationField $field A validation object to add errors to. |
||
499 | * @param bool $sparse Whether or not this is a sparse validation. |
||
500 | * @return mixed|Invalid Returns a clean version of the value with all extra fields stripped out or invalid if the value |
||
501 | * is completely invalid. |
||
502 | */ |
||
503 | 116 | protected function validateField($value, ValidationField $field, $sparse = false) { |
|
561 | |||
562 | /** |
||
563 | * Validate an array. |
||
564 | * |
||
565 | * @param mixed $value The value to validate. |
||
566 | * @param ValidationField $field The validation results to add. |
||
567 | * @param bool $sparse Whether or not this is a sparse validation. |
||
568 | * @return array|Invalid Returns an array or invalid if validation fails. |
||
569 | */ |
||
570 | 12 | protected function validateArray($value, ValidationField $field, $sparse = false) { |
|
600 | |||
601 | /** |
||
602 | * Validate a boolean value. |
||
603 | * |
||
604 | * @param mixed $value The value to validate. |
||
605 | * @param ValidationField $field The validation results to add. |
||
606 | * @return bool|Invalid Returns the cleaned value or invalid if validation fails. |
||
607 | */ |
||
608 | 21 | protected function validateBoolean($value, ValidationField $field) { |
|
609 | 21 | $value = $value === null ? $value : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
|
610 | 21 | if ($value === null) { |
|
611 | 5 | $field->addTypeError('boolean'); |
|
612 | 5 | return Invalid::value(); |
|
613 | } |
||
614 | 17 | return $value; |
|
615 | } |
||
616 | |||
617 | /** |
||
618 | * Validate a date time. |
||
619 | * |
||
620 | * @param mixed $value The value to validate. |
||
621 | * @param ValidationField $field The validation results to add. |
||
622 | * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid. |
||
623 | */ |
||
624 | 13 | protected function validateDatetime($value, ValidationField $field) { |
|
649 | |||
650 | /** |
||
651 | * Validate a float. |
||
652 | * |
||
653 | * @param mixed $value The value to validate. |
||
654 | * @param ValidationField $field The validation results to add. |
||
655 | * @return float|Invalid Returns a number or **null** if validation fails. |
||
656 | */ |
||
657 | 9 | protected function validateNumber($value, ValidationField $field) { |
|
665 | |||
666 | /** |
||
667 | * Validate and integer. |
||
668 | * |
||
669 | * @param mixed $value The value to validate. |
||
670 | * @param ValidationField $field The validation results to add. |
||
671 | * @return int|Invalid Returns the cleaned value or **null** if validation fails. |
||
672 | */ |
||
673 | 22 | protected function validateInteger($value, ValidationField $field) { |
|
682 | |||
683 | /** |
||
684 | * Validate an object. |
||
685 | * |
||
686 | * @param mixed $value The value to validate. |
||
687 | * @param ValidationField $field The validation results to add. |
||
688 | * @param bool $sparse Whether or not this is a sparse validation. |
||
689 | * @return object|Invalid Returns a clean object or **null** if validation fails. |
||
690 | */ |
||
691 | 76 | protected function validateObject($value, ValidationField $field, $sparse = false) { |
|
701 | |||
702 | /** |
||
703 | * Validate data against the schema and return the result. |
||
704 | * |
||
705 | * @param array $data The data to validate. |
||
706 | * @param ValidationField $field This argument will be filled with the validation result. |
||
707 | * @param bool $sparse Whether or not this is a sparse validation. |
||
708 | * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
709 | * or invalid if there are no valid properties. |
||
710 | */ |
||
711 | 75 | protected function validateProperties(array $data, ValidationField $field, $sparse = false) { |
|
763 | |||
764 | /** |
||
765 | * Validate a string. |
||
766 | * |
||
767 | * @param mixed $value The value to validate. |
||
768 | * @param ValidationField $field The validation results to add. |
||
769 | * @return string|Invalid Returns the valid string or **null** if validation fails. |
||
770 | */ |
||
771 | 51 | protected function validateString($value, ValidationField $field) { |
|
860 | |||
861 | /** |
||
862 | * Validate a unix timestamp. |
||
863 | * |
||
864 | * @param mixed $value The value to validate. |
||
865 | * @param ValidationField $field The field being validated. |
||
866 | * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate. |
||
867 | */ |
||
868 | 8 | protected function validateTimestamp($value, ValidationField $field) { |
|
879 | |||
880 | /** |
||
881 | * Validate a null value. |
||
882 | * |
||
883 | * @param mixed $value The value to validate. |
||
884 | * @param ValidationField $field The error collector for the field. |
||
885 | * @return null|Invalid Returns **null** or invalid. |
||
886 | */ |
||
887 | protected function validateNull($value, ValidationField $field) { |
||
894 | |||
895 | /** |
||
896 | * Validate a value against an enum. |
||
897 | * |
||
898 | * @param mixed $value The value to test. |
||
899 | * @param ValidationField $field The validation object for adding errors. |
||
900 | * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise. |
||
901 | */ |
||
902 | 114 | protected function validateEnum($value, ValidationField $field) { |
|
921 | |||
922 | /** |
||
923 | * Call all of the validators attached to a field. |
||
924 | * |
||
925 | * @param mixed $value The field value being validated. |
||
926 | * @param ValidationField $field The validation object to add errors. |
||
927 | */ |
||
928 | 114 | protected function callValidators($value, ValidationField $field) { |
|
948 | |||
949 | /** |
||
950 | * Specify data which should be serialized to JSON. |
||
951 | * |
||
952 | * This method specifically returns data compatible with the JSON schema format. |
||
953 | * |
||
954 | * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource. |
||
955 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
956 | * @link http://json-schema.org/ |
||
957 | */ |
||
958 | public function jsonSerialize() { |
||
993 | |||
994 | /** |
||
995 | * Look up a type based on its alias. |
||
996 | * |
||
997 | * @param string $alias The type alias or type name to lookup. |
||
998 | * @return mixed |
||
999 | */ |
||
1000 | 132 | protected function getType($alias) { |
|
1011 | |||
1012 | /** |
||
1013 | * Get the class that's used to contain validation information. |
||
1014 | * |
||
1015 | * @return Validation|string Returns the validation class. |
||
1016 | */ |
||
1017 | 116 | public function getValidationClass() { |
|
1020 | |||
1021 | /** |
||
1022 | * Set the class that's used to contain validation information. |
||
1023 | * |
||
1024 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
1025 | * @return $this |
||
1026 | */ |
||
1027 | 1 | public function setValidationClass($class) { |
|
1035 | |||
1036 | /** |
||
1037 | * Create a new validation instance. |
||
1038 | * |
||
1039 | * @return Validation Returns a validation object. |
||
1040 | */ |
||
1041 | 116 | protected function createValidation() { |
|
1051 | } |
||
1052 |
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.