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 | '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 | ]; |
||
39 | |||
40 | private $schema = []; |
||
41 | |||
42 | /** |
||
43 | * @var int A bitwise combination of the various **Schema::FLAG_*** constants. |
||
44 | */ |
||
45 | private $flags = 0; |
||
46 | |||
47 | /** |
||
48 | * @var array An array of callbacks that will custom validate the schema. |
||
49 | */ |
||
50 | private $validators = []; |
||
51 | |||
52 | /** |
||
53 | * @var string|Validation The name of the class or an instance that will be cloned. |
||
54 | */ |
||
55 | private $validationClass = Validation::class; |
||
56 | |||
57 | |||
58 | /// Methods /// |
||
59 | |||
60 | /** |
||
61 | * Initialize an instance of a new {@link Schema} class. |
||
62 | * |
||
63 | * @param array $schema The array schema to validate against. |
||
64 | */ |
||
65 | 132 | public function __construct($schema = []) { |
|
68 | |||
69 | /** |
||
70 | * Grab the schema's current description. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 1 | public function getDescription() { |
|
77 | |||
78 | /** |
||
79 | * Set the description for the schema. |
||
80 | * |
||
81 | * @param string $description The new description. |
||
82 | * @throws \InvalidArgumentException Throws an exception when the provided description is not a string. |
||
83 | * @return Schema |
||
84 | */ |
||
85 | 2 | public function setDescription($description) { |
|
94 | |||
95 | /** |
||
96 | * Return the validation flags. |
||
97 | * |
||
98 | * @return int Returns a bitwise combination of flags. |
||
99 | */ |
||
100 | 1 | public function getFlags() { |
|
103 | |||
104 | /** |
||
105 | * Set the validation flags. |
||
106 | * |
||
107 | * @param int $flags One or more of the **Schema::FLAG_*** constants. |
||
108 | * @return Schema Returns the current instance for fluent calls. |
||
109 | */ |
||
110 | 8 | public function setFlags($flags) { |
|
118 | |||
119 | /** |
||
120 | * Whether or not the schema has a flag (or combination of flags). |
||
121 | * |
||
122 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
123 | * @return bool Returns **true** if all of the flags are set or **false** otherwise. |
||
124 | */ |
||
125 | 8 | public function hasFlag($flag) { |
|
128 | |||
129 | /** |
||
130 | * Set a flag. |
||
131 | * |
||
132 | * @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
||
133 | * @param bool $value Either true or false. |
||
134 | * @return $this |
||
135 | */ |
||
136 | 1 | public function setFlag($flag, $value) { |
|
144 | |||
145 | /** |
||
146 | * Merge a schema with this one. |
||
147 | * |
||
148 | * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
||
149 | */ |
||
150 | 2 | public function merge(Schema $schema) { |
|
174 | |||
175 | /** |
||
176 | * Returns the internal schema array. |
||
177 | * |
||
178 | * @return array |
||
179 | * @see Schema::jsonSerialize() |
||
180 | */ |
||
181 | 11 | public function getSchemaArray() { |
|
184 | |||
185 | /** |
||
186 | * Parse a schema in short form into a full schema array. |
||
187 | * |
||
188 | * @param array $arr The array to parse into a schema. |
||
189 | * @return array The full schema array. |
||
190 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
191 | */ |
||
192 | 132 | protected function parse(array $arr) { |
|
224 | |||
225 | /** |
||
226 | * Parse a schema node. |
||
227 | * |
||
228 | * @param array $node The node to parse. |
||
229 | * @param mixed $value Additional information from the node. |
||
230 | * @return array Returns a JSON schema compatible node. |
||
231 | */ |
||
232 | 127 | private function parseNode($node, $value = null) { |
|
279 | |||
280 | /** |
||
281 | * Parse the schema for an object's properties. |
||
282 | * |
||
283 | * @param array $arr An object property schema. |
||
284 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
285 | */ |
||
286 | 85 | private function parseProperties(array $arr) { |
|
312 | |||
313 | /** |
||
314 | * Parse a short parameter string into a full array parameter. |
||
315 | * |
||
316 | * @param string $key The short parameter string to parse. |
||
317 | * @param array $value An array of other information that might help resolve ambiguity. |
||
318 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
319 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
320 | */ |
||
321 | 125 | public function parseShortParam($key, $value = []) { |
|
361 | |||
362 | /** |
||
363 | * Add a custom validator to to validate the schema. |
||
364 | * |
||
365 | * @param string $fieldname The name of the field to validate, if any. |
||
366 | * |
||
367 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
368 | * @param callable $callback The callback to validate with. |
||
369 | * @return Schema Returns `$this` for fluent calls. |
||
370 | */ |
||
371 | 2 | public function addValidator($fieldname, callable $callback) { |
|
375 | |||
376 | /** |
||
377 | * Require one of a given set of fields in the schema. |
||
378 | * |
||
379 | * @param array $required The field names to require. |
||
380 | * @param string $fieldname The name of the field to attach to. |
||
381 | * @param int $count The count of required items. |
||
382 | * @return Schema Returns `$this` for fluent calls. |
||
383 | */ |
||
384 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
433 | |||
434 | /** |
||
435 | * Validate data against the schema. |
||
436 | * |
||
437 | * @param mixed $data The data to validate. |
||
438 | * @param bool $sparse Whether or not this is a sparse validation. |
||
439 | * @return mixed Returns a cleaned version of the data. |
||
440 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
441 | */ |
||
442 | 106 | public function validate($data, $sparse = false) { |
|
453 | |||
454 | /** |
||
455 | * Validate data against the schema and return the result. |
||
456 | * |
||
457 | * @param mixed $data The data to validate. |
||
458 | * @param bool $sparse Whether or not to do a sparse validation. |
||
459 | * @return bool Returns true if the data is valid. False otherwise. |
||
460 | */ |
||
461 | 33 | public function isValid($data, $sparse = false) { |
|
469 | |||
470 | /** |
||
471 | * Validate a field. |
||
472 | * |
||
473 | * @param mixed $value The value to validate. |
||
474 | * @param ValidationField $field A validation object to add errors to. |
||
475 | * @param bool $sparse Whether or not this is a sparse validation. |
||
476 | * @return mixed Returns a clean version of the value with all extra fields stripped out. |
||
477 | */ |
||
478 | 106 | private function validateField($value, ValidationField $field, $sparse = false) { |
|
534 | |||
535 | /** |
||
536 | * Call all of the validators attached to a field. |
||
537 | * |
||
538 | * @param mixed $value The field value being validated. |
||
539 | * @param ValidationField $field The validation object to add errors. |
||
540 | */ |
||
541 | 105 | private function callValidators($value, ValidationField $field) { |
|
550 | |||
551 | /** |
||
552 | * Validate an array. |
||
553 | * |
||
554 | * @param mixed $value The value to validate. |
||
555 | * @param ValidationField $field The validation results to add. |
||
556 | * @param bool $sparse Whether or not this is a sparse validation. |
||
557 | * @return array|null Returns an array or **null** if validation fails. |
||
558 | */ |
||
559 | 10 | private function validateArray($value, ValidationField $field, $sparse = false) { |
|
589 | |||
590 | /** |
||
591 | * Validate a boolean value. |
||
592 | * |
||
593 | * @param mixed $value The value to validate. |
||
594 | * @param ValidationField $field The validation results to add. |
||
595 | * @return bool|null Returns the cleaned value or **null** if validation fails. |
||
596 | */ |
||
597 | 19 | private function validateBoolean($value, ValidationField $field) { |
|
604 | |||
605 | /** |
||
606 | * Validate a date time. |
||
607 | * |
||
608 | * @param mixed $value The value to validate. |
||
609 | * @param ValidationField $field The validation results to add. |
||
610 | * @return \DateTimeInterface|null Returns the cleaned value or **null** if it isn't valid. |
||
611 | */ |
||
612 | 11 | private function validateDatetime($value, ValidationField $field) { |
|
637 | |||
638 | /** |
||
639 | * Validate a float. |
||
640 | * |
||
641 | * @param mixed $value The value to validate. |
||
642 | * @param ValidationField $field The validation results to add. |
||
643 | * @return float|int|null Returns a number or **null** if validation fails. |
||
644 | */ |
||
645 | 7 | private function validateNumber($value, ValidationField $field) { |
|
653 | |||
654 | /** |
||
655 | * Validate and integer. |
||
656 | * |
||
657 | * @param mixed $value The value to validate. |
||
658 | * @param ValidationField $field The validation results to add. |
||
659 | * @return int|null Returns the cleaned value or **null** if validation fails. |
||
660 | */ |
||
661 | 20 | private function validateInteger($value, ValidationField $field) { |
|
670 | |||
671 | /** |
||
672 | * Validate an object. |
||
673 | * |
||
674 | * @param mixed $value The value to validate. |
||
675 | * @param ValidationField $field The validation results to add. |
||
676 | * @param bool $sparse Whether or not this is a sparse validation. |
||
677 | * @return object|null Returns a clean object or **null** if validation fails. |
||
678 | */ |
||
679 | 73 | private function validateObject($value, ValidationField $field, $sparse = false) { |
|
689 | |||
690 | /** |
||
691 | * Validate data against the schema and return the result. |
||
692 | * |
||
693 | * @param array $data The data to validate. |
||
694 | * @param ValidationField $field This argument will be filled with the validation result. |
||
695 | * @param bool $sparse Whether or not this is a sparse validation. |
||
696 | * @return array Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
697 | */ |
||
698 | 73 | private function validateProperties(array $data, ValidationField $field, $sparse = false) { |
|
753 | |||
754 | /** |
||
755 | * Validate a string. |
||
756 | * |
||
757 | * @param mixed $value The value to validate. |
||
758 | * @param ValidationField $field The validation results to add. |
||
759 | * @return string|null Returns the valid string or **null** if validation fails. |
||
760 | */ |
||
761 | 49 | private function validateString($value, ValidationField $field) { |
|
848 | |||
849 | /** |
||
850 | * Validate a unix timestamp. |
||
851 | * |
||
852 | * @param mixed $value The value to validate. |
||
853 | * @param ValidationField $field The field being validated. |
||
854 | * @return int|null Returns a valid timestamp or **null** if the value doesn't validate. |
||
855 | */ |
||
856 | 6 | private function validateTimestamp($value, ValidationField $field) { |
|
867 | |||
868 | /** |
||
869 | * Validate a value against an enum. |
||
870 | * |
||
871 | * @param mixed $value The value to test. |
||
872 | * @param ValidationField $field The validation object for adding errors. |
||
873 | * @return bool Returns **true** if the value one of the enumerated values or **false** otherwise. |
||
874 | */ |
||
875 | 105 | private function validateEnum($value, ValidationField $field) { |
|
894 | |||
895 | /** |
||
896 | * Specify data which should be serialized to JSON. |
||
897 | * |
||
898 | * This method specifically returns data compatible with the JSON schema format. |
||
899 | * |
||
900 | * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource. |
||
901 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
902 | * @link http://json-schema.org/ |
||
903 | */ |
||
904 | public function jsonSerialize() { |
||
939 | |||
940 | /** |
||
941 | * Look up a type based on its alias. |
||
942 | * |
||
943 | * @param string $alias The type alias or type name to lookup. |
||
944 | * @return mixed |
||
945 | */ |
||
946 | 122 | private function getType($alias) { |
|
957 | |||
958 | /** |
||
959 | * Get the class that's used to contain validation information. |
||
960 | * |
||
961 | * @return Validation|string Returns the validation class. |
||
962 | */ |
||
963 | 106 | public function getValidationClass() { |
|
966 | |||
967 | /** |
||
968 | * Set the class that's used to contain validation information. |
||
969 | * |
||
970 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
971 | * @return $this |
||
972 | */ |
||
973 | 1 | public function setValidationClass($class) { |
|
981 | |||
982 | /** |
||
983 | * Create a new validation instance. |
||
984 | * |
||
985 | * @return Validation Returns a validation object. |
||
986 | */ |
||
987 | 106 | protected function createValidation() { |
|
997 | } |
||
998 |
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.