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 | 147 | 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) { |
|
87 | 2 | if (is_string($description)) { |
|
88 | 1 | $this->schema['description'] = $description; |
|
89 | 1 | } else { |
|
90 | 1 | throw new \InvalidArgumentException("The description is not a valid string.", 500); |
|
91 | } |
||
92 | |||
93 | 1 | return $this; |
|
94 | } |
||
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 | * @return $this |
||
151 | */ |
||
152 | 3 | public function merge(Schema $schema) { |
|
156 | |||
157 | /** |
||
158 | * Add another schema to this one. |
||
159 | * |
||
160 | * Adding schemas together is analogous to array addition. When you add a schema it will only add missing information. |
||
161 | * |
||
162 | * @param Schema $schema The schema to add. |
||
163 | * @param bool $addProperties Whether to add properties that don't exist in this schema. |
||
164 | * @return $this |
||
165 | */ |
||
166 | 3 | public function add(Schema $schema, $addProperties = false) { |
|
170 | |||
171 | /** |
||
172 | * The internal implementation of schema merging. |
||
173 | * |
||
174 | * @param array &$target The target of the merge. |
||
175 | * @param array $source The source of the merge. |
||
176 | * @param bool $overwrite Whether or not to replace values. |
||
177 | * @param bool $addProperties Whether or not to add object properties to the target. |
||
178 | * @return array |
||
179 | */ |
||
180 | 6 | private function mergeInternal(array &$target, array $source, $overwrite = true, $addProperties = true) { |
|
232 | |||
233 | // public function overlay(Schema $schema ) |
||
234 | |||
235 | /** |
||
236 | * Returns the internal schema array. |
||
237 | * |
||
238 | * @return array |
||
239 | * @see Schema::jsonSerialize() |
||
240 | */ |
||
241 | 15 | public function getSchemaArray() { |
|
244 | |||
245 | /** |
||
246 | * Parse a short schema and return the associated schema. |
||
247 | * |
||
248 | * @param array $arr The schema array. |
||
249 | * @return Schema Returns a new schema. |
||
250 | */ |
||
251 | 142 | public static function parse(array $arr) { |
|
256 | |||
257 | /** |
||
258 | * Parse a schema in short form into a full schema array. |
||
259 | * |
||
260 | * @param array $arr The array to parse into a schema. |
||
261 | * @return array The full schema array. |
||
262 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
263 | */ |
||
264 | 142 | protected function parseInternal(array $arr) { |
|
296 | |||
297 | /** |
||
298 | * Parse a schema node. |
||
299 | * |
||
300 | * @param array $node The node to parse. |
||
301 | * @param mixed $value Additional information from the node. |
||
302 | * @return array Returns a JSON schema compatible node. |
||
303 | */ |
||
304 | 138 | private function parseNode($node, $value = null) { |
|
354 | |||
355 | /** |
||
356 | * Parse the schema for an object's properties. |
||
357 | * |
||
358 | * @param array $arr An object property schema. |
||
359 | * @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
||
360 | */ |
||
361 | 88 | private function parseProperties(array $arr) { |
|
387 | |||
388 | /** |
||
389 | * Parse a short parameter string into a full array parameter. |
||
390 | * |
||
391 | * @param string $key The short parameter string to parse. |
||
392 | * @param array $value An array of other information that might help resolve ambiguity. |
||
393 | * @return array Returns an array in the form `[string name, array param, bool required]`. |
||
394 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
395 | */ |
||
396 | 138 | public function parseShortParam($key, $value = []) { |
|
454 | |||
455 | /** |
||
456 | * Add a custom validator to to validate the schema. |
||
457 | * |
||
458 | * @param string $fieldname The name of the field to validate, if any. |
||
459 | * |
||
460 | * If you are adding a validator to a deeply nested field then separate the path with dots. |
||
461 | * @param callable $callback The callback to validate with. |
||
462 | * @return Schema Returns `$this` for fluent calls. |
||
463 | */ |
||
464 | 2 | public function addValidator($fieldname, callable $callback) { |
|
468 | |||
469 | /** |
||
470 | * Require one of a given set of fields in the schema. |
||
471 | * |
||
472 | * @param array $required The field names to require. |
||
473 | * @param string $fieldname The name of the field to attach to. |
||
474 | * @param int $count The count of required items. |
||
475 | * @return Schema Returns `$this` for fluent calls. |
||
476 | */ |
||
477 | 1 | public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
526 | |||
527 | /** |
||
528 | * Validate data against the schema. |
||
529 | * |
||
530 | * @param mixed $data The data to validate. |
||
531 | * @param bool $sparse Whether or not this is a sparse validation. |
||
532 | * @return mixed Returns a cleaned version of the data. |
||
533 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
534 | */ |
||
535 | 117 | public function validate($data, $sparse = false) { |
|
551 | |||
552 | /** |
||
553 | * Validate data against the schema and return the result. |
||
554 | * |
||
555 | * @param mixed $data The data to validate. |
||
556 | * @param bool $sparse Whether or not to do a sparse validation. |
||
557 | * @return bool Returns true if the data is valid. False otherwise. |
||
558 | */ |
||
559 | 33 | public function isValid($data, $sparse = false) { |
|
567 | |||
568 | /** |
||
569 | * Validate a field. |
||
570 | * |
||
571 | * @param mixed $value The value to validate. |
||
572 | * @param ValidationField $field A validation object to add errors to. |
||
573 | * @param bool $sparse Whether or not this is a sparse validation. |
||
574 | * @return mixed|Invalid Returns a clean version of the value with all extra fields stripped out or invalid if the value |
||
575 | * is completely invalid. |
||
576 | */ |
||
577 | 117 | protected function validateField($value, ValidationField $field, $sparse = false) { |
|
635 | |||
636 | /** |
||
637 | * Validate an array. |
||
638 | * |
||
639 | * @param mixed $value The value to validate. |
||
640 | * @param ValidationField $field The validation results to add. |
||
641 | * @param bool $sparse Whether or not this is a sparse validation. |
||
642 | * @return array|Invalid Returns an array or invalid if validation fails. |
||
643 | */ |
||
644 | 13 | protected function validateArray($value, ValidationField $field, $sparse = false) { |
|
675 | |||
676 | /** |
||
677 | * Validate a boolean value. |
||
678 | * |
||
679 | * @param mixed $value The value to validate. |
||
680 | * @param ValidationField $field The validation results to add. |
||
681 | * @return bool|Invalid Returns the cleaned value or invalid if validation fails. |
||
682 | */ |
||
683 | 21 | protected function validateBoolean($value, ValidationField $field) { |
|
691 | |||
692 | /** |
||
693 | * Validate a date time. |
||
694 | * |
||
695 | * @param mixed $value The value to validate. |
||
696 | * @param ValidationField $field The validation results to add. |
||
697 | * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid. |
||
698 | */ |
||
699 | 13 | protected function validateDatetime($value, ValidationField $field) { |
|
724 | |||
725 | /** |
||
726 | * Validate a float. |
||
727 | * |
||
728 | * @param mixed $value The value to validate. |
||
729 | * @param ValidationField $field The validation results to add. |
||
730 | * @return float|Invalid Returns a number or **null** if validation fails. |
||
731 | */ |
||
732 | 9 | protected function validateNumber($value, ValidationField $field) { |
|
740 | |||
741 | /** |
||
742 | * Validate and integer. |
||
743 | * |
||
744 | * @param mixed $value The value to validate. |
||
745 | * @param ValidationField $field The validation results to add. |
||
746 | * @return int|Invalid Returns the cleaned value or **null** if validation fails. |
||
747 | */ |
||
748 | 23 | protected function validateInteger($value, ValidationField $field) { |
|
757 | |||
758 | /** |
||
759 | * Validate an object. |
||
760 | * |
||
761 | * @param mixed $value The value to validate. |
||
762 | * @param ValidationField $field The validation results to add. |
||
763 | * @param bool $sparse Whether or not this is a sparse validation. |
||
764 | * @return object|Invalid Returns a clean object or **null** if validation fails. |
||
765 | */ |
||
766 | 77 | protected function validateObject($value, ValidationField $field, $sparse = false) { |
|
778 | |||
779 | /** |
||
780 | * Validate data against the schema and return the result. |
||
781 | * |
||
782 | * @param array|\ArrayAccess $data The data to validate. |
||
783 | * @param ValidationField $field This argument will be filled with the validation result. |
||
784 | * @param bool $sparse Whether or not this is a sparse validation. |
||
785 | * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types. |
||
786 | * or invalid if there are no valid properties. |
||
787 | */ |
||
788 | 76 | protected function validateProperties($data, ValidationField $field, $sparse = false) { |
|
845 | |||
846 | /** |
||
847 | * Validate a string. |
||
848 | * |
||
849 | * @param mixed $value The value to validate. |
||
850 | * @param ValidationField $field The validation results to add. |
||
851 | * @return string|Invalid Returns the valid string or **null** if validation fails. |
||
852 | */ |
||
853 | 52 | protected function validateString($value, ValidationField $field) { |
|
942 | |||
943 | /** |
||
944 | * Validate a unix timestamp. |
||
945 | * |
||
946 | * @param mixed $value The value to validate. |
||
947 | * @param ValidationField $field The field being validated. |
||
948 | * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate. |
||
949 | */ |
||
950 | 8 | protected function validateTimestamp($value, ValidationField $field) { |
|
961 | |||
962 | /** |
||
963 | * Validate a null value. |
||
964 | * |
||
965 | * @param mixed $value The value to validate. |
||
966 | * @param ValidationField $field The error collector for the field. |
||
967 | * @return null|Invalid Returns **null** or invalid. |
||
968 | */ |
||
969 | protected function validateNull($value, ValidationField $field) { |
||
976 | |||
977 | /** |
||
978 | * Validate a value against an enum. |
||
979 | * |
||
980 | * @param mixed $value The value to test. |
||
981 | * @param ValidationField $field The validation object for adding errors. |
||
982 | * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise. |
||
983 | */ |
||
984 | 115 | protected function validateEnum($value, ValidationField $field) { |
|
1003 | |||
1004 | /** |
||
1005 | * Call all of the validators attached to a field. |
||
1006 | * |
||
1007 | * @param mixed $value The field value being validated. |
||
1008 | * @param ValidationField $field The validation object to add errors. |
||
1009 | */ |
||
1010 | 115 | protected function callValidators($value, ValidationField $field) { |
|
1030 | |||
1031 | /** |
||
1032 | * Specify data which should be serialized to JSON. |
||
1033 | * |
||
1034 | * This method specifically returns data compatible with the JSON schema format. |
||
1035 | * |
||
1036 | * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource. |
||
1037 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
1038 | * @link http://json-schema.org/ |
||
1039 | */ |
||
1040 | public function jsonSerialize() { |
||
1075 | |||
1076 | /** |
||
1077 | * Look up a type based on its alias. |
||
1078 | * |
||
1079 | * @param string $alias The type alias or type name to lookup. |
||
1080 | * @return mixed |
||
1081 | */ |
||
1082 | 137 | protected function getType($alias) { |
|
1093 | |||
1094 | /** |
||
1095 | * Get the class that's used to contain validation information. |
||
1096 | * |
||
1097 | * @return Validation|string Returns the validation class. |
||
1098 | */ |
||
1099 | 117 | public function getValidationClass() { |
|
1102 | |||
1103 | /** |
||
1104 | * Set the class that's used to contain validation information. |
||
1105 | * |
||
1106 | * @param Validation|string $class Either the name of a class or a class that will be cloned. |
||
1107 | * @return $this |
||
1108 | */ |
||
1109 | 1 | public function setValidationClass($class) { |
|
1117 | |||
1118 | /** |
||
1119 | * Create a new validation instance. |
||
1120 | * |
||
1121 | * @return Validation Returns a validation object. |
||
1122 | */ |
||
1123 | 117 | protected function createValidation() { |
|
1133 | |||
1134 | /** |
||
1135 | * Check whether or not a value is an array or accessible like an array. |
||
1136 | * |
||
1137 | * @param mixed $value The value to check. |
||
1138 | * @return bool Returns **true** if the value can be used like an array or **false** otherwise. |
||
1139 | */ |
||
1140 | 77 | private function isArray($value) { |
|
1143 | |||
1144 | /** |
||
1145 | * Cast a value to an array. |
||
1146 | * |
||
1147 | * @param \Traversable $value The value to convert. |
||
1148 | * @return array Returns an array. |
||
1149 | */ |
||
1150 | private function toArray(\Traversable $value) { |
||
1156 | } |
||
1157 |
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.