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 | 'n' => 'number', |
||
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 | 128 | 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 | 128 | 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 | 123 | 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 | 83 | 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 | 123 | 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 | 106 | public function validate($data, $sparse = false) { |
|
438 | |||
439 | /** |
||
440 | * Validate data against the schema and return the result. |
||
441 | * |
||
442 | * @param mixed $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 | 33 | public function isValid($data, $sparse = false) { |
|
454 | |||
455 | /** |
||
456 | * Validate a field. |
||
457 | * |
||
458 | * @param mixed $value The value to validate. |
||
459 | * @param ValidationField $field A validation object to add errors to. |
||
460 | * @param bool $sparse Whether or not this is a sparse validation. |
||
461 | * @return mixed Returns a clean version of the value with all extra fields stripped out. |
||
462 | */ |
||
463 | 106 | private function validateField($value, ValidationField $field, $sparse = false) { |
|
519 | |||
520 | /** |
||
521 | * Call all of the validators attached to a field. |
||
522 | * |
||
523 | * @param mixed $value The field value being validated. |
||
524 | * @param ValidationField $field The validation object to add errors. |
||
525 | */ |
||
526 | 105 | private function callValidators($value, ValidationField $field) { |
|
535 | |||
536 | /** |
||
537 | * Validate an array. |
||
538 | * |
||
539 | * @param mixed $value The value to validate. |
||
540 | * @param ValidationField $field The validation results to add. |
||
541 | * @param bool $sparse Whether or not this is a sparse validation. |
||
542 | * @return array|null Returns an array or **null** if validation fails. |
||
543 | */ |
||
544 | 10 | private function validateArray($value, ValidationField $field, $sparse = false) { |
|
574 | |||
575 | /** |
||
576 | * Validate a boolean value. |
||
577 | * |
||
578 | * @param mixed $value The value to validate. |
||
579 | * @param ValidationField $field The validation results to add. |
||
580 | * @return bool|null Returns the cleaned value or **null** if validation fails. |
||
581 | */ |
||
582 | 19 | private function validateBoolean($value, ValidationField $field) { |
|
589 | |||
590 | /** |
||
591 | * Validate a date time. |
||
592 | * |
||
593 | * @param mixed $value The value to validate. |
||
594 | * @param ValidationField $field The validation results to add. |
||
595 | * @return \DateTimeInterface|null Returns the cleaned value or **null** if it isn't valid. |
||
596 | */ |
||
597 | 11 | private function validateDatetime($value, ValidationField $field) { |
|
622 | |||
623 | /** |
||
624 | * Validate a float. |
||
625 | * |
||
626 | * @param mixed $value The value to validate. |
||
627 | * @param ValidationField $field The validation results to add. |
||
628 | * @return float|int|null Returns a number or **null** if validation fails. |
||
629 | */ |
||
630 | 7 | private function validateNumber($value, ValidationField $field) { |
|
641 | |||
642 | /** |
||
643 | * Validate and integer. |
||
644 | * |
||
645 | * @param mixed $value The value to validate. |
||
646 | * @param ValidationField $field The validation results to add. |
||
647 | * @return int|null Returns the cleaned value or **null** if validation fails. |
||
648 | */ |
||
649 | 20 | private function validateInteger($value, ValidationField $field) { |
|
660 | |||
661 | /** |
||
662 | * Validate an object. |
||
663 | * |
||
664 | * @param mixed $value The value to validate. |
||
665 | * @param ValidationField $field The validation results to add. |
||
666 | * @param bool $sparse Whether or not this is a sparse validation. |
||
667 | * @return object|null Returns a clean object or **null** if validation fails. |
||
668 | */ |
||
669 | 73 | private function validateObject($value, ValidationField $field, $sparse = false) { |
|
679 | |||
680 | /** |
||
681 | * Validate data against the schema and return the result. |
||
682 | * |
||
683 | * @param array $data The data to validate. |
||
684 | * @param ValidationField $field This argument will be filled with the validation result. |
||
685 | * @param bool $sparse Whether or not this is a sparse validation. |
||
686 | * @return array Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
687 | */ |
||
688 | 73 | private function validateProperties(array $data, ValidationField $field, $sparse = false) { |
|
743 | |||
744 | /** |
||
745 | * Validate a string. |
||
746 | * |
||
747 | * @param mixed $value The value to validate. |
||
748 | * @param ValidationField $field The validation results to add. |
||
749 | * @return string|null Returns the valid string or **null** if validation fails. |
||
750 | */ |
||
751 | 49 | private function validateString($value, ValidationField $field) { |
|
838 | |||
839 | /** |
||
840 | * Validate a unix timestamp. |
||
841 | * |
||
842 | * @param mixed $value The value to validate. |
||
843 | * @param ValidationField $field The field being validated. |
||
844 | * @return int|null Returns a valid timestamp or **null** if the value doesn't validate. |
||
845 | */ |
||
846 | 6 | private function validateTimestamp($value, ValidationField $field) { |
|
857 | |||
858 | /** |
||
859 | * Validate a value against an enum. |
||
860 | * |
||
861 | * @param mixed $value The value to test. |
||
862 | * @param ValidationField $field The validation object for adding errors. |
||
863 | * @return bool Returns **true** if the value one of the enumerated values or **false** otherwise. |
||
864 | */ |
||
865 | 105 | private function validateEnum($value, ValidationField $field) { |
|
884 | |||
885 | /** |
||
886 | * Specify data which should be serialized to JSON. |
||
887 | * |
||
888 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
889 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
890 | * which is a value of any type other than a resource. |
||
891 | */ |
||
892 | 20 | public function jsonSerialize() { |
|
901 | |||
902 | /** |
||
903 | * Look up a type based on its alias. |
||
904 | * |
||
905 | * @param string $alias The type alias or type name to lookup. |
||
906 | * @return mixed |
||
907 | */ |
||
908 | 9 | private function getType($alias) { |
|
918 | |||
919 | /** |
||
920 | * Get the class that's used to contain validation information. |
||
921 | * |
||
922 | * @return Validation|string Returns the validation class. |
||
923 | */ |
||
924 | 106 | public function getValidationClass() { |
|
927 | |||
928 | /** |
||
929 | * Set the class that's used to contain validation information. |
||
930 | * |
||
931 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
932 | * @return $this |
||
933 | */ |
||
934 | 1 | public function setValidationClass($class) { |
|
942 | |||
943 | /** |
||
944 | * Create a new validation instance. |
||
945 | * |
||
946 | * @return Validation Returns a validation object. |
||
947 | */ |
||
948 | 106 | protected function createValidation() { |
|
958 | } |
||
959 |
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.