Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 16 | class Schema implements \JsonSerializable { |
||
| 17 | /// Properties /// |
||
| 18 | protected $schema = []; |
||
| 19 | |||
| 20 | protected static $types = [ |
||
| 21 | // '@' => 'file', |
||
| 22 | 'a' => 'array', |
||
| 23 | 'o' => 'object', |
||
| 24 | '=' => 'base64', |
||
| 25 | 'i' => 'integer', |
||
| 26 | 's' => 'string', |
||
| 27 | 'f' => 'float', |
||
| 28 | 'b' => 'boolean', |
||
| 29 | 'ts' => 'timestamp', |
||
| 30 | 'dt' => 'datetime' |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array An array of callbacks that will custom validate the schema. |
||
| 35 | */ |
||
| 36 | protected $validators = []; |
||
| 37 | |||
| 38 | /// Methods /// |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Initialize an instance of a new {@link Schema} class. |
||
| 42 | * |
||
| 43 | * @param array $schema The array schema to validate against. |
||
| 44 | */ |
||
| 45 | 74 | public function __construct($schema = []) { |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Create a new schema and return it. |
||
| 51 | * |
||
| 52 | * @param array $schema The schema array. |
||
| 53 | * @return Schema Returns the newly created and parsed schema. |
||
| 54 | */ |
||
| 55 | 24 | public static function create($schema = []) { |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Parse a schema in short form into a full schema array. |
||
| 62 | * |
||
| 63 | * @param array $arr The array to parse into a schema. |
||
| 64 | * @return array The full schema array. |
||
| 65 | * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
||
| 66 | */ |
||
| 67 | 74 | public static function parseSchema(array $arr) { |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Parse a short parameter string into a full array parameter. |
||
| 145 | * |
||
| 146 | * @param string $str The short parameter string to parse. |
||
| 147 | * @param array $other An array of other information that might help resolve ambiguity. |
||
| 148 | * @return array Returns an array in the form [name, [param]]. |
||
| 149 | * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
||
| 150 | */ |
||
| 151 | 74 | public static function parseShortParam($str, $other = []) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Add a custom validator to to validate the schema. |
||
| 187 | * |
||
| 188 | * @param string $fieldname The name of the field to validate, if any. |
||
| 189 | * @param callable $callback The callback to validate with. |
||
| 190 | * @return Schema Returns `$this` for fluent calls. |
||
| 191 | */ |
||
| 192 | 2 | public function addValidator($fieldname, callable $callback) { |
|
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Require one of a given set of fields in the schema. |
||
| 200 | * |
||
| 201 | * @param array $fieldnames The field names to require. |
||
| 202 | * @param int $count The count of required items. |
||
| 203 | * @return Schema Returns `$this` for fluent calls. |
||
| 204 | */ |
||
| 205 | 1 | public function requireOneOf(array $fieldnames, $count = 1) { |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Validate data against the schema. |
||
| 259 | * |
||
| 260 | * @param array &$data The data to validate. |
||
| 261 | * @param Validation &$validation This argument will be filled with the validation result. |
||
| 262 | * @return bool Returns true if the data is valid, false otherwise. |
||
| 263 | * @throws ValidationException Throws an exception when the data does not validate against the schema. |
||
| 264 | */ |
||
| 265 | 14 | public function validate(array &$data, Validation &$validation = null) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Validate data against the schema and return the result. |
||
| 279 | * |
||
| 280 | * @param array &$data The data to validate. |
||
| 281 | * @param Validation &$validation This argument will be filled with the validation result. |
||
| 282 | * @return bool Returns true if the data is valid. False otherwise. |
||
| 283 | */ |
||
| 284 | 55 | public function isValid(array &$data, Validation &$validation = null) { |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Validate data against the schema and return the result. |
||
| 290 | * |
||
| 291 | * @param array &$data The data to validate. |
||
| 292 | * @param array $schema The schema array to validate against. |
||
| 293 | * @param Validation &$validation This argument will be filled with the validation result. |
||
| 294 | * @param string $path The path to the current path for nested objects. |
||
| 295 | * @return bool Returns true if the data is valid. False otherwise. |
||
| 296 | */ |
||
| 297 | 68 | protected function isValidInternal(array &$data, array $schema, Validation &$validation = null, $path = '') { |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Validate a field. |
||
| 328 | * |
||
| 329 | * @param mixed &$value The value to validate. |
||
| 330 | * @param array $field Parameters on the field. |
||
| 331 | * @param Validation $validation A validation object to add errors to. |
||
| 332 | * @throws \InvalidArgumentException Throws an exception when there is something wrong in the {@link $params}. |
||
| 333 | * @internal param string $fieldname The name of the field to validate. |
||
| 334 | * @return bool Returns true if the field is valid, false otherwise. |
||
| 335 | */ |
||
| 336 | 68 | protected function validateField(&$value, array $field, Validation $validation) { |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Validate an array. |
||
| 411 | * |
||
| 412 | * @param mixed &$value The value to validate. |
||
| 413 | * @param array $field The field definition. |
||
| 414 | * @param Validation $validation The validation results to add. |
||
| 415 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 416 | */ |
||
| 417 | 8 | protected function validateArray(&$value, array $field, Validation $validation) { |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Validate a base64 string. |
||
| 442 | * |
||
| 443 | * @param mixed &$value The value to validate. |
||
| 444 | * @param array $field The field definition. |
||
| 445 | * @param Validation $validation The validation results to add. |
||
| 446 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 447 | */ |
||
| 448 | 6 | protected function validateBase64(&$value, array $field, Validation $validation) { |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Validate a boolean value. |
||
| 469 | * |
||
| 470 | * @param mixed &$value The value to validate. |
||
| 471 | * @param array $field The field definition. |
||
| 472 | * @param Validation $validation The validation results to add. |
||
| 473 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 474 | */ |
||
| 475 | 18 | protected function validateBoolean(&$value, array $field, Validation $validation) { |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Validate a date time. |
||
| 495 | * |
||
| 496 | * @param mixed &$value The value to validate. |
||
| 497 | * @param array $field The field definition. |
||
| 498 | * @param Validation $validation The validation results to add. |
||
| 499 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 500 | */ |
||
| 501 | 3 | protected function validateDatetime(&$value, array $field, Validation $validation) { |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Validate a float. |
||
| 527 | * |
||
| 528 | * @param mixed &$value The value to validate. |
||
| 529 | * @param array $field The field definition. |
||
| 530 | * @param Validation $validation The validation results to add. |
||
| 531 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 532 | */ |
||
| 533 | 6 | View Code Duplication | protected function validateFloat(&$value, array $field, Validation $validation) { |
| 544 | |||
| 545 | /** |
||
| 546 | * Validate and integer. |
||
| 547 | * |
||
| 548 | * @param mixed &$value The value to validate. |
||
| 549 | * @param array $field The field definition. |
||
| 550 | * @param Validation $validation The validation results to add. |
||
| 551 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 552 | */ |
||
| 553 | 11 | View Code Duplication | protected function validateInteger(&$value, array $field, Validation $validation) { |
| 564 | |||
| 565 | /** |
||
| 566 | * Validate an object. |
||
| 567 | * |
||
| 568 | * @param mixed &$value The value to validate. |
||
| 569 | * @param array $field The field definition. |
||
| 570 | * @param Validation $validation The validation results to add. |
||
| 571 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 572 | */ |
||
| 573 | 8 | protected function validateObject(&$value, array $field, Validation $validation) { |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Validate a required field. |
||
| 586 | * |
||
| 587 | * @param mixed &$value The field value. |
||
| 588 | * @param array $field The field definition. |
||
| 589 | * @param Validation $validation A {@link Validation} object to collect errors. |
||
| 590 | * @return bool|null Returns one of the following: |
||
| 591 | * - null: The field is not required. |
||
| 592 | * - true: The field is required and {@link $value} is not empty. |
||
| 593 | * - false: The field is required and {@link $value} is empty. |
||
| 594 | */ |
||
| 595 | 68 | protected function validateRequired(&$value, array $field, Validation $validation) { |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Validate a string. |
||
| 623 | * |
||
| 624 | * @param mixed &$value The value to validate. |
||
| 625 | * @param array $field The field definition. |
||
| 626 | * @param Validation $validation The validation results to add. |
||
| 627 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 628 | */ |
||
| 629 | 9 | View Code Duplication | protected function validateString(&$value, array $field, Validation $validation) { |
| 640 | |||
| 641 | /** |
||
| 642 | * Validate a unix timestamp. |
||
| 643 | * |
||
| 644 | * @param mixed &$value The value to validate. |
||
| 645 | * @param array $field The field definition. |
||
| 646 | * @param Validation $validation The validation results to add. |
||
| 647 | * @return bool Returns true if {@link $value} is valid or false otherwise. |
||
| 648 | */ |
||
| 649 | 5 | protected function validateTimestamp(&$value, array $field, Validation $validation) { |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Specify data which should be serialized to JSON. |
||
| 663 | * |
||
| 664 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 665 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
| 666 | * which is a value of any type other than a resource. |
||
| 667 | */ |
||
| 668 | 7 | public function jsonSerialize() { |
|
| 671 | } |
||
| 672 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.