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 | 149 | 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 the ID for the schema. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getID() { |
||
104 | |||
105 | /** |
||
106 | * Set the ID for the schema. |
||
107 | * |
||
108 | * @param string $ID The new ID. |
||
109 | * @throws \InvalidArgumentException Throws an exception when the provided ID is not a string. |
||
110 | * @return Schema |
||
111 | */ |
||
112 | public function setID($ID) { |
||
121 | |||
122 | /** |
||
123 | * Return the validation flags. |
||
124 | * |
||
125 | * @return int Returns a bitwise combination of flags. |
||
126 | */ |
||
127 | 1 | public function getFlags() { |
|
130 | |||
131 | /** |
||
132 | * Set the validation flags. |
||
133 | * |
||
134 | * @param int $flags One or more of the **Schema::FLAG_*** constants. |
||
135 | * @return Schema Returns the current instance for fluent calls. |
||
136 | */ |
||
137 | 8 | public function setFlags($flags) { |
|
145 | |||
146 | /** |
||
147 | * Whether or not the schema has a flag (or combination of flags). |
||
148 | * |
||
149 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
150 | * @return bool Returns **true** if all of the flags are set or **false** otherwise. |
||
151 | */ |
||
152 | 8 | public function hasFlag($flag) { |
|
155 | |||
156 | /** |
||
157 | * Set a flag. |
||
158 | * |
||
159 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
160 | * @param bool $value Either true or false. |
||
161 | * @return $this |
||
162 | */ |
||
163 | 1 | public function setFlag($flag, $value) { |
|
171 | |||
172 | /** |
||
173 | * Merge a schema with this one. |
||
174 | * |
||
175 | * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
||
176 | * @return $this |
||
177 | */ |
||
178 | 3 | public function merge(Schema $schema) { |
|
182 | |||
183 | /** |
||
184 | * Add another schema to this one. |
||
185 | * |
||
186 | * Adding schemas together is analogous to array addition. When you add a schema it will only add missing information. |
||
187 | * |
||
188 | * @param Schema $schema The schema to add. |
||
189 | * @param bool $addProperties Whether to add properties that don't exist in this schema. |
||
190 | * @return $this |
||
191 | */ |
||
192 | 3 | public function add(Schema $schema, $addProperties = false) { |
|
196 | |||
197 | /** |
||
198 | * The internal implementation of schema merging. |
||
199 | * |
||
200 | * @param array &$target The target of the merge. |
||
201 | * @param array $source The source of the merge. |
||
202 | * @param bool $overwrite Whether or not to replace values. |
||
203 | * @param bool $addProperties Whether or not to add object properties to the target. |
||
204 | * @return array |
||
205 | */ |
||
206 | 6 | private function mergeInternal(array &$target, array $source, $overwrite = true, $addProperties = true) { |
|
258 | |||
259 | // public function overlay(Schema $schema ) |
||
260 | |||
261 | /** |
||
262 | * Returns the internal schema array. |
||
263 | * |
||
264 | * @return array |
||
265 | * @see Schema::jsonSerialize() |
||
266 | */ |
||
267 | 15 | public function getSchemaArray() { |
|
270 | |||
271 | /** |
||
272 | * Parse a short schema and return the associated schema. |
||
273 | * |
||
274 | * @param array $arr The schema array. |
||
275 | * @param mixed ...$args Constructor arguments for the schema instance. |
||
276 | * @return static Returns a new schema. |
||
277 | */ |
||
278 | 144 | public static function parse(array $arr, ...$args) { |
|
283 | |||
284 | /** |
||
285 | * Parse a schema in short form into a full schema array. |
||
286 | * |
||
287 | * @param array $arr The array to parse into a schema. |
||
288 | * @return array The full schema array. |
||
289 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
290 | */ |
||
291 | 144 | protected function parseInternal(array $arr) { |
|
323 | |||
324 | /** |
||
325 | * Parse a schema node. |
||
326 | * |
||
327 | * @param array $node The node to parse. |
||
328 | * @param mixed $value Additional information from the node. |
||
329 | * @return array Returns a JSON schema compatible node. |
||
330 | */ |
||
331 | 138 | private function parseNode($node, $value = null) { |
|
381 | |||
382 | /** |
||
383 | * Parse the schema for an object's properties. |
||
384 | * |
||
385 | * @param array $arr An object property schema. |
||
386 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
387 | */ |
||
388 | 88 | private function parseProperties(array $arr) { |
|
414 | |||
415 | /** |
||
416 | * Parse a short parameter string into a full array parameter. |
||
417 | * |
||
418 | * @param string $key The short parameter string to parse. |
||
419 | * @param array $value An array of other information that might help resolve ambiguity. |
||
420 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
421 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
422 | */ |
||
423 | 138 | public function parseShortParam($key, $value = []) { |
|
481 | |||
482 | /** |
||
483 | * Add a custom validator to to validate the schema. |
||
484 | * |
||
485 | * @param string $fieldname The name of the field to validate, if any. |
||
486 | * |
||
487 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
488 | * @param callable $callback The callback to validate with. |
||
489 | * @return Schema Returns `$this` for fluent calls. |
||
490 | */ |
||
491 | 2 | public function addValidator($fieldname, callable $callback) { |
|
495 | |||
496 | /** |
||
497 | * Require one of a given set of fields in the schema. |
||
498 | * |
||
499 | * @param array $required The field names to require. |
||
500 | * @param string $fieldname The name of the field to attach to. |
||
501 | * @param int $count The count of required items. |
||
502 | * @return Schema Returns `$this` for fluent calls. |
||
503 | */ |
||
504 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
553 | |||
554 | /** |
||
555 | * Validate data against the schema. |
||
556 | * |
||
557 | * @param mixed $data The data to validate. |
||
558 | * @param bool $sparse Whether or not this is a sparse validation. |
||
559 | * @return mixed Returns a cleaned version of the data. |
||
560 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
561 | */ |
||
562 | 117 | public function validate($data, $sparse = false) { |
|
578 | |||
579 | /** |
||
580 | * Validate data against the schema and return the result. |
||
581 | * |
||
582 | * @param mixed $data The data to validate. |
||
583 | * @param bool $sparse Whether or not to do a sparse validation. |
||
584 | * @return bool Returns true if the data is valid. False otherwise. |
||
585 | */ |
||
586 | 33 | public function isValid($data, $sparse = false) { |
|
594 | |||
595 | /** |
||
596 | * Validate a field. |
||
597 | * |
||
598 | * @param mixed $value The value to validate. |
||
599 | * @param ValidationField $field A validation object to add errors to. |
||
600 | * @param bool $sparse Whether or not this is a sparse validation. |
||
601 | * @return mixed|Invalid Returns a clean version of the value with all extra fields stripped out or invalid if the value |
||
602 | * is completely invalid. |
||
603 | */ |
||
604 | 117 | protected function validateField($value, ValidationField $field, $sparse = false) { |
|
662 | |||
663 | /** |
||
664 | * Validate an array. |
||
665 | * |
||
666 | * @param mixed $value The value to validate. |
||
667 | * @param ValidationField $field The validation results to add. |
||
668 | * @param bool $sparse Whether or not this is a sparse validation. |
||
669 | * @return array|Invalid Returns an array or invalid if validation fails. |
||
670 | */ |
||
671 | 13 | protected function validateArray($value, ValidationField $field, $sparse = false) { |
|
703 | |||
704 | /** |
||
705 | * Validate a boolean value. |
||
706 | * |
||
707 | * @param mixed $value The value to validate. |
||
708 | * @param ValidationField $field The validation results to add. |
||
709 | * @return bool|Invalid Returns the cleaned value or invalid if validation fails. |
||
710 | */ |
||
711 | 21 | protected function validateBoolean($value, ValidationField $field) { |
|
719 | |||
720 | /** |
||
721 | * Validate a date time. |
||
722 | * |
||
723 | * @param mixed $value The value to validate. |
||
724 | * @param ValidationField $field The validation results to add. |
||
725 | * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid. |
||
726 | */ |
||
727 | 13 | protected function validateDatetime($value, ValidationField $field) { |
|
752 | |||
753 | /** |
||
754 | * Validate a float. |
||
755 | * |
||
756 | * @param mixed $value The value to validate. |
||
757 | * @param ValidationField $field The validation results to add. |
||
758 | * @return float|Invalid Returns a number or **null** if validation fails. |
||
759 | */ |
||
760 | 9 | protected function validateNumber($value, ValidationField $field) { |
|
768 | |||
769 | /** |
||
770 | * Validate and integer. |
||
771 | * |
||
772 | * @param mixed $value The value to validate. |
||
773 | * @param ValidationField $field The validation results to add. |
||
774 | * @return int|Invalid Returns the cleaned value or **null** if validation fails. |
||
775 | */ |
||
776 | 23 | protected function validateInteger($value, ValidationField $field) { |
|
785 | |||
786 | /** |
||
787 | * Validate an object. |
||
788 | * |
||
789 | * @param mixed $value The value to validate. |
||
790 | * @param ValidationField $field The validation results to add. |
||
791 | * @param bool $sparse Whether or not this is a sparse validation. |
||
792 | * @return object|Invalid Returns a clean object or **null** if validation fails. |
||
793 | */ |
||
794 | 77 | protected function validateObject($value, ValidationField $field, $sparse = false) { |
|
806 | |||
807 | /** |
||
808 | * Validate data against the schema and return the result. |
||
809 | * |
||
810 | * @param array|\ArrayAccess $data The data to validate. |
||
811 | * @param ValidationField $field This argument will be filled with the validation result. |
||
812 | * @param bool $sparse Whether or not this is a sparse validation. |
||
813 | * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
814 | * or invalid if there are no valid properties. |
||
815 | */ |
||
816 | 76 | protected function validateProperties($data, ValidationField $field, $sparse = false) { |
|
873 | |||
874 | /** |
||
875 | * Validate a string. |
||
876 | * |
||
877 | * @param mixed $value The value to validate. |
||
878 | * @param ValidationField $field The validation results to add. |
||
879 | * @return string|Invalid Returns the valid string or **null** if validation fails. |
||
880 | */ |
||
881 | 52 | protected function validateString($value, ValidationField $field) { |
|
970 | |||
971 | /** |
||
972 | * Validate a unix timestamp. |
||
973 | * |
||
974 | * @param mixed $value The value to validate. |
||
975 | * @param ValidationField $field The field being validated. |
||
976 | * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate. |
||
977 | */ |
||
978 | 8 | protected function validateTimestamp($value, ValidationField $field) { |
|
989 | |||
990 | /** |
||
991 | * Validate a null value. |
||
992 | * |
||
993 | * @param mixed $value The value to validate. |
||
994 | * @param ValidationField $field The error collector for the field. |
||
995 | * @return null|Invalid Returns **null** or invalid. |
||
996 | */ |
||
997 | protected function validateNull($value, ValidationField $field) { |
||
1004 | |||
1005 | /** |
||
1006 | * Validate a value against an enum. |
||
1007 | * |
||
1008 | * @param mixed $value The value to test. |
||
1009 | * @param ValidationField $field The validation object for adding errors. |
||
1010 | * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise. |
||
1011 | */ |
||
1012 | 115 | protected function validateEnum($value, ValidationField $field) { |
|
1031 | |||
1032 | /** |
||
1033 | * Call all of the validators attached to a field. |
||
1034 | * |
||
1035 | * @param mixed $value The field value being validated. |
||
1036 | * @param ValidationField $field The validation object to add errors. |
||
1037 | */ |
||
1038 | 115 | protected function callValidators($value, ValidationField $field) { |
|
1058 | |||
1059 | /** |
||
1060 | * Specify data which should be serialized to JSON. |
||
1061 | * |
||
1062 | * This method specifically returns data compatible with the JSON schema format. |
||
1063 | * |
||
1064 | * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource. |
||
1065 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
1066 | * @link http://json-schema.org/ |
||
1067 | */ |
||
1068 | public function jsonSerialize() { |
||
1103 | |||
1104 | /** |
||
1105 | * Look up a type based on its alias. |
||
1106 | * |
||
1107 | * @param string $alias The type alias or type name to lookup. |
||
1108 | * @return mixed |
||
1109 | */ |
||
1110 | 137 | protected function getType($alias) { |
|
1121 | |||
1122 | /** |
||
1123 | * Get the class that's used to contain validation information. |
||
1124 | * |
||
1125 | * @return Validation|string Returns the validation class. |
||
1126 | */ |
||
1127 | 117 | public function getValidationClass() { |
|
1130 | |||
1131 | /** |
||
1132 | * Set the class that's used to contain validation information. |
||
1133 | * |
||
1134 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
1135 | * @return $this |
||
1136 | */ |
||
1137 | 1 | public function setValidationClass($class) { |
|
1145 | |||
1146 | /** |
||
1147 | * Create a new validation instance. |
||
1148 | * |
||
1149 | * @return Validation Returns a validation object. |
||
1150 | */ |
||
1151 | 117 | protected function createValidation() { |
|
1161 | |||
1162 | /** |
||
1163 | * Check whether or not a value is an array or accessible like an array. |
||
1164 | * |
||
1165 | * @param mixed $value The value to check. |
||
1166 | * @return bool Returns **true** if the value can be used like an array or **false** otherwise. |
||
1167 | */ |
||
1168 | 77 | private function isArray($value) { |
|
1171 | |||
1172 | /** |
||
1173 | * Cast a value to an array. |
||
1174 | * |
||
1175 | * @param \Traversable $value The value to convert. |
||
1176 | * @return array Returns an array. |
||
1177 | */ |
||
1178 | private function toArray(\Traversable $value) { |
||
1184 | } |
||
1185 |
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.