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 | * Throw a notice when extraneous properties are encountered during validation. |
||
16 | */ |
||
17 | const FLAG_EXTRA_PROPERTIES_NOTICE = 0x1; |
||
18 | |||
19 | /** |
||
20 | * Throw a ValidationException when extraneous properties are encountered during validation. |
||
21 | */ |
||
22 | const FLAG_EXTRA_PROPERTIES_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 | 'a' => 'array', |
||
31 | 'o' => 'object', |
||
32 | 'i' => 'integer', |
||
33 | 'int' => 'integer', |
||
34 | 's' => 'string', |
||
35 | 'str' => 'string', |
||
36 | 'f' => 'number', |
||
37 | 'float' => 'number', |
||
38 | 'b' => 'boolean', |
||
39 | 'bool' => 'boolean', |
||
40 | 'ts' => 'timestamp', |
||
41 | 'dt' => 'datetime' |
||
42 | ]; |
||
43 | |||
44 | private $schema = []; |
||
45 | |||
46 | /** |
||
47 | * @var int A bitwise combination of the various **Schema::FLAG_*** constants. |
||
48 | */ |
||
49 | private $flags = 0; |
||
50 | |||
51 | /** |
||
52 | * @var array An array of callbacks that will custom validate the schema. |
||
53 | */ |
||
54 | private $validators = []; |
||
55 | |||
56 | /** |
||
57 | * @var string|Validation The name of the class or an instance that will be cloned. |
||
58 | */ |
||
59 | private $validationClass = Validation::class; |
||
60 | |||
61 | |||
62 | /// Methods /// |
||
63 | |||
64 | /** |
||
65 | * Initialize an instance of a new {@link Schema} class. |
||
66 | * |
||
67 | * @param array $schema The array schema to validate against. |
||
68 | */ |
||
69 | 132 | public function __construct($schema = []) { |
|
72 | |||
73 | /** |
||
74 | * Grab the schema's current description. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 1 | public function getDescription() { |
|
81 | |||
82 | /** |
||
83 | * Set the description for the schema. |
||
84 | * |
||
85 | * @param string $description The new description. |
||
86 | * @throws \InvalidArgumentException Throws an exception when the provided description is not a string. |
||
87 | * @return Schema |
||
88 | */ |
||
89 | 2 | public function setDescription($description) { |
|
98 | |||
99 | /** |
||
100 | * Return the validation flags. |
||
101 | * |
||
102 | * @return int Returns a bitwise combination of flags. |
||
103 | */ |
||
104 | 1 | public function getFlags() { |
|
107 | |||
108 | /** |
||
109 | * Set the validation flags. |
||
110 | * |
||
111 | * @param int $flags One or more of the **Schema::FLAG_*** constants. |
||
112 | * @return Schema Returns the current instance for fluent calls. |
||
113 | */ |
||
114 | 8 | public function setFlags($flags) { |
|
122 | |||
123 | /** |
||
124 | * Whether or not the schema has a flag (or combination of flags). |
||
125 | * |
||
126 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
127 | * @return bool Returns **true** if all of the flags are set or **false** otherwise. |
||
128 | */ |
||
129 | 8 | public function hasFlag($flag) { |
|
132 | |||
133 | /** |
||
134 | * Set a flag. |
||
135 | * |
||
136 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
137 | * @param bool $value Either true or false. |
||
138 | * @return $this |
||
139 | */ |
||
140 | 1 | public function setFlag($flag, $value) { |
|
148 | |||
149 | /** |
||
150 | * Merge a schema with this one. |
||
151 | * |
||
152 | * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
||
153 | */ |
||
154 | 2 | public function merge(Schema $schema) { |
|
178 | |||
179 | /** |
||
180 | * Returns the internal schema array. |
||
181 | * |
||
182 | * @return array |
||
183 | * @see Schema::jsonSerialize() |
||
184 | */ |
||
185 | 11 | public function getSchemaArray() { |
|
188 | |||
189 | /** |
||
190 | * Parse a schema in short form into a full schema array. |
||
191 | * |
||
192 | * @param array $arr The array to parse into a schema. |
||
193 | * @return array The full schema array. |
||
194 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
195 | */ |
||
196 | 132 | protected function parse(array $arr) { |
|
228 | |||
229 | /** |
||
230 | * Parse a schema node. |
||
231 | * |
||
232 | * @param array $node The node to parse. |
||
233 | * @param mixed $value Additional information from the node. |
||
234 | * @return array Returns a JSON schema compatible node. |
||
235 | */ |
||
236 | 127 | private function parseNode($node, $value = null) { |
|
283 | |||
284 | /** |
||
285 | * Parse the schema for an object's properties. |
||
286 | * |
||
287 | * @param array $arr An object property schema. |
||
288 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
289 | */ |
||
290 | 85 | private function parseProperties(array $arr) { |
|
316 | |||
317 | /** |
||
318 | * Parse a short parameter string into a full array parameter. |
||
319 | * |
||
320 | * @param string $key The short parameter string to parse. |
||
321 | * @param array $value An array of other information that might help resolve ambiguity. |
||
322 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
323 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
324 | */ |
||
325 | 125 | public function parseShortParam($key, $value = []) { |
|
365 | |||
366 | /** |
||
367 | * Add a custom validator to to validate the schema. |
||
368 | * |
||
369 | * @param string $fieldname The name of the field to validate, if any. |
||
370 | * |
||
371 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
372 | * @param callable $callback The callback to validate with. |
||
373 | * @return Schema Returns `$this` for fluent calls. |
||
374 | */ |
||
375 | 2 | public function addValidator($fieldname, callable $callback) { |
|
379 | |||
380 | /** |
||
381 | * Require one of a given set of fields in the schema. |
||
382 | * |
||
383 | * @param array $required The field names to require. |
||
384 | * @param string $fieldname The name of the field to attach to. |
||
385 | * @param int $count The count of required items. |
||
386 | * @return Schema Returns `$this` for fluent calls. |
||
387 | */ |
||
388 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
437 | |||
438 | /** |
||
439 | * Validate data against the schema. |
||
440 | * |
||
441 | * @param mixed $data The data to validate. |
||
442 | * @param bool $sparse Whether or not this is a sparse validation. |
||
443 | * @return mixed Returns a cleaned version of the data. |
||
444 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
445 | */ |
||
446 | 106 | public function validate($data, $sparse = false) { |
|
457 | |||
458 | /** |
||
459 | * Validate data against the schema and return the result. |
||
460 | * |
||
461 | * @param mixed $data The data to validate. |
||
462 | * @param bool $sparse Whether or not to do a sparse validation. |
||
463 | * @return bool Returns true if the data is valid. False otherwise. |
||
464 | */ |
||
465 | 33 | public function isValid($data, $sparse = false) { |
|
473 | |||
474 | /** |
||
475 | * Validate a field. |
||
476 | * |
||
477 | * @param mixed $value The value to validate. |
||
478 | * @param ValidationField $field A validation object to add errors to. |
||
479 | * @param bool $sparse Whether or not this is a sparse validation. |
||
480 | * @return mixed Returns a clean version of the value with all extra fields stripped out. |
||
481 | */ |
||
482 | 106 | private function validateField($value, ValidationField $field, $sparse = false) { |
|
538 | |||
539 | /** |
||
540 | * Call all of the validators attached to a field. |
||
541 | * |
||
542 | * @param mixed $value The field value being validated. |
||
543 | * @param ValidationField $field The validation object to add errors. |
||
544 | */ |
||
545 | 105 | private function callValidators($value, ValidationField $field) { |
|
554 | |||
555 | /** |
||
556 | * Validate an array. |
||
557 | * |
||
558 | * @param mixed $value The value to validate. |
||
559 | * @param ValidationField $field The validation results to add. |
||
560 | * @param bool $sparse Whether or not this is a sparse validation. |
||
561 | * @return array|null Returns an array or **null** if validation fails. |
||
562 | */ |
||
563 | 10 | private function validateArray($value, ValidationField $field, $sparse = false) { |
|
593 | |||
594 | /** |
||
595 | * Validate a boolean value. |
||
596 | * |
||
597 | * @param mixed $value The value to validate. |
||
598 | * @param ValidationField $field The validation results to add. |
||
599 | * @return bool|null Returns the cleaned value or **null** if validation fails. |
||
600 | */ |
||
601 | 19 | private function validateBoolean($value, ValidationField $field) { |
|
608 | |||
609 | /** |
||
610 | * Validate a date time. |
||
611 | * |
||
612 | * @param mixed $value The value to validate. |
||
613 | * @param ValidationField $field The validation results to add. |
||
614 | * @return \DateTimeInterface|null Returns the cleaned value or **null** if it isn't valid. |
||
615 | */ |
||
616 | 11 | private function validateDatetime($value, ValidationField $field) { |
|
641 | |||
642 | /** |
||
643 | * Validate a float. |
||
644 | * |
||
645 | * @param mixed $value The value to validate. |
||
646 | * @param ValidationField $field The validation results to add. |
||
647 | * @return float|int|null Returns a number or **null** if validation fails. |
||
648 | */ |
||
649 | 7 | private function validateNumber($value, ValidationField $field) { |
|
657 | |||
658 | /** |
||
659 | * Validate and integer. |
||
660 | * |
||
661 | * @param mixed $value The value to validate. |
||
662 | * @param ValidationField $field The validation results to add. |
||
663 | * @return int|null Returns the cleaned value or **null** if validation fails. |
||
664 | */ |
||
665 | 20 | private function validateInteger($value, ValidationField $field) { |
|
674 | |||
675 | /** |
||
676 | * Validate an object. |
||
677 | * |
||
678 | * @param mixed $value The value to validate. |
||
679 | * @param ValidationField $field The validation results to add. |
||
680 | * @param bool $sparse Whether or not this is a sparse validation. |
||
681 | * @return object|null Returns a clean object or **null** if validation fails. |
||
682 | */ |
||
683 | 73 | private function validateObject($value, ValidationField $field, $sparse = false) { |
|
693 | |||
694 | /** |
||
695 | * Validate data against the schema and return the result. |
||
696 | * |
||
697 | * @param array $data The data to validate. |
||
698 | * @param ValidationField $field This argument will be filled with the validation result. |
||
699 | * @param bool $sparse Whether or not this is a sparse validation. |
||
700 | * @return array Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
701 | */ |
||
702 | 73 | private function validateProperties(array $data, ValidationField $field, $sparse = false) { |
|
757 | |||
758 | /** |
||
759 | * Validate a string. |
||
760 | * |
||
761 | * @param mixed $value The value to validate. |
||
762 | * @param ValidationField $field The validation results to add. |
||
763 | * @return string|null Returns the valid string or **null** if validation fails. |
||
764 | */ |
||
765 | 49 | private function validateString($value, ValidationField $field) { |
|
852 | |||
853 | /** |
||
854 | * Validate a unix timestamp. |
||
855 | * |
||
856 | * @param mixed $value The value to validate. |
||
857 | * @param ValidationField $field The field being validated. |
||
858 | * @return int|null Returns a valid timestamp or **null** if the value doesn't validate. |
||
859 | */ |
||
860 | 6 | private function validateTimestamp($value, ValidationField $field) { |
|
871 | |||
872 | /** |
||
873 | * Validate a value against an enum. |
||
874 | * |
||
875 | * @param mixed $value The value to test. |
||
876 | * @param ValidationField $field The validation object for adding errors. |
||
877 | * @return bool Returns **true** if the value one of the enumerated values or **false** otherwise. |
||
878 | */ |
||
879 | 105 | private function validateEnum($value, ValidationField $field) { |
|
898 | |||
899 | /** |
||
900 | * Specify data which should be serialized to JSON. |
||
901 | * |
||
902 | * This method specifically returns data compatible with the JSON schema format. |
||
903 | * |
||
904 | * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource. |
||
905 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
906 | * @link http://json-schema.org/ |
||
907 | */ |
||
908 | public function jsonSerialize() { |
||
943 | |||
944 | /** |
||
945 | * Look up a type based on its alias. |
||
946 | * |
||
947 | * @param string $alias The type alias or type name to lookup. |
||
948 | * @return mixed |
||
949 | */ |
||
950 | 9 | private function getType($alias) { |
|
960 | |||
961 | /** |
||
962 | * Get the class that's used to contain validation information. |
||
963 | * |
||
964 | * @return Validation|string Returns the validation class. |
||
965 | */ |
||
966 | 106 | public function getValidationClass() { |
|
969 | |||
970 | /** |
||
971 | * Set the class that's used to contain validation information. |
||
972 | * |
||
973 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
974 | * @return $this |
||
975 | */ |
||
976 | 1 | public function setValidationClass($class) { |
|
984 | |||
985 | /** |
||
986 | * Create a new validation instance. |
||
987 | * |
||
988 | * @return Validation Returns a validation object. |
||
989 | */ |
||
990 | 106 | protected function createValidation() { |
|
1000 | } |
||
1001 |
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.