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 |
||
| 17 | class Schema |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $title; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $description; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var SchemaType |
||
| 31 | */ |
||
| 32 | protected $type; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $format; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Schema |
||
| 41 | */ |
||
| 42 | protected $items; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ArrayCollection |
||
| 46 | */ |
||
| 47 | protected $properties; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var SchemaAdditionalProperties |
||
| 51 | */ |
||
| 52 | protected $additionalProperties; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string[] |
||
| 56 | */ |
||
| 57 | protected $required = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | protected $translatable; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $refCollection = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * possible event names this collection implements (queue events) |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $eventNames = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $readOnly = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | protected $recordOriginModifiable; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | protected $recordOriginException; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string[] |
||
| 93 | */ |
||
| 94 | protected $searchable = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $minLength; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $maxLength; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | protected $minItems; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var int |
||
| 113 | */ |
||
| 114 | protected $maxItems; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var float |
||
| 118 | */ |
||
| 119 | protected $numericMinimum; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var float |
||
| 123 | */ |
||
| 124 | protected $numericMaximum; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var SchemaEnum |
||
| 128 | */ |
||
| 129 | protected $enum; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | protected $regexPattern; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | protected $documentClass; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var array<string> |
||
| 143 | */ |
||
| 144 | protected $constraints; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var array<string> |
||
| 148 | */ |
||
| 149 | protected $textIndexes; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * these are the BSON primitive types. |
||
| 153 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 154 | * every type set *not* in this set will be carried over to 'format' |
||
| 155 | * |
||
| 156 | * @var string[] |
||
| 157 | */ |
||
| 158 | protected $primitiveTypes = [ |
||
| 159 | 'array', |
||
| 160 | 'boolean', |
||
| 161 | 'integer', |
||
| 162 | 'number', |
||
| 163 | 'null', |
||
| 164 | 'object', |
||
| 165 | 'string' |
||
| 166 | ]; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * those are types that when they are required, a minimal length |
||
| 170 | * shall be specified in schema (or allow null if not required; that will lead |
||
| 171 | * to the inclusion of "null" in the "type" property array) |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | protected $minLengthTypes = [ |
||
| 176 | 'integer', |
||
| 177 | 'number', |
||
| 178 | 'float', |
||
| 179 | 'double', |
||
| 180 | 'decimal', |
||
| 181 | 'string', |
||
| 182 | 'date', |
||
| 183 | 'extref' |
||
| 184 | ]; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * known non-primitive types we map to primitives here. |
||
| 188 | * the type itself is set to the format. |
||
| 189 | * |
||
| 190 | * @var string[] |
||
| 191 | */ |
||
| 192 | protected $specialTypeMapping = [ |
||
| 193 | 'extref' => 'string', |
||
| 194 | 'translatable' => 'object', |
||
| 195 | 'date' => 'string', |
||
| 196 | 'float' => 'number', |
||
| 197 | 'double' => 'number', |
||
| 198 | 'decimal' => 'number' |
||
| 199 | ]; |
||
| 200 | |||
| 201 | protected $formatOverrides = [ |
||
| 202 | 'date' => 'date-time' |
||
| 203 | ]; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Build properties |
||
| 207 | */ |
||
| 208 | public function __construct() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * set title |
||
| 215 | * |
||
| 216 | * @param string $title title |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function setTitle($title) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * get title |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getTitle() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * set description |
||
| 237 | * |
||
| 238 | * @param string $description description |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function setDescription($description) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * get description |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getDescription() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * set type |
||
| 259 | * |
||
| 260 | * @param string|array $types types |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public function setType($types) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * get type |
||
| 302 | * |
||
| 303 | * @return SchemaType type |
||
| 304 | */ |
||
| 305 | public function getType() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * get MinLengthTypes |
||
| 312 | * |
||
| 313 | * @return array MinLengthTypes |
||
| 314 | */ |
||
| 315 | public function getMinLengthTypes() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * get format |
||
| 322 | * |
||
| 323 | * @return string format |
||
| 324 | */ |
||
| 325 | public function getFormat() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * sets format |
||
| 332 | * |
||
| 333 | * @param string $format format |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | public function setFormat($format) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * get numeric minimum |
||
| 344 | * |
||
| 345 | * @return float numeric minimum |
||
| 346 | */ |
||
| 347 | public function getNumericMinimum() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * set numeric minimum |
||
| 354 | * |
||
| 355 | * @param float $numericMinimum numeric mimimum |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | public function setNumericMinimum($numericMinimum) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * get numeric maximum |
||
| 366 | * |
||
| 367 | * @return float numeric maximum |
||
| 368 | */ |
||
| 369 | public function getNumericMaximum() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * set numeric maximum |
||
| 376 | * |
||
| 377 | * @param float $numericMaximum maximum |
||
| 378 | * |
||
| 379 | * @return void |
||
| 380 | */ |
||
| 381 | public function setNumericMaximum($numericMaximum) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * set min length |
||
| 388 | * |
||
| 389 | * @return int length |
||
| 390 | */ |
||
| 391 | public function getMinLength() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * get min length |
||
| 398 | * |
||
| 399 | * @param int $minLength length |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function setMinLength($minLength) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * gets maxlength |
||
| 410 | * |
||
| 411 | * @return int length |
||
| 412 | */ |
||
| 413 | public function getMaxLength() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * set maxlength |
||
| 420 | * |
||
| 421 | * @param int $maxLength length |
||
| 422 | * |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | public function setMaxLength($maxLength) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * set min Items |
||
| 432 | * |
||
| 433 | * @return int Items |
||
| 434 | */ |
||
| 435 | public function getMinItems() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * get min Items |
||
| 442 | * |
||
| 443 | * @param int $minItems length |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | public function setMinItems($minItems) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * gets maxItems |
||
| 454 | * |
||
| 455 | * @return int Items |
||
| 456 | */ |
||
| 457 | public function getMaxItems() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * set maxItems |
||
| 464 | * |
||
| 465 | * @param int $maxItems Items |
||
| 466 | * |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | public function setMaxItems($maxItems) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * get Enum |
||
| 476 | * |
||
| 477 | * @return array Enum |
||
|
|
|||
| 478 | */ |
||
| 479 | public function getEnum() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * set Enum |
||
| 486 | * |
||
| 487 | * @param array $enum enum |
||
| 488 | * |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | public function setEnum(array $enum) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * get regex pattern |
||
| 498 | * |
||
| 499 | * @return string pattern |
||
| 500 | */ |
||
| 501 | public function getRegexPattern() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * set regex pattern |
||
| 508 | * |
||
| 509 | * @param string $regexPattern regex pattern |
||
| 510 | * |
||
| 511 | * @return void |
||
| 512 | */ |
||
| 513 | public function setRegexPattern($regexPattern) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * get DocumentClass |
||
| 520 | * |
||
| 521 | * @return string DocumentClass |
||
| 522 | */ |
||
| 523 | public function getDocumentClass() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * set DocumentClass |
||
| 530 | * |
||
| 531 | * @param string $documentClass documentClass |
||
| 532 | * |
||
| 533 | * @return void |
||
| 534 | */ |
||
| 535 | public function setDocumentClass($documentClass) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * get Constraints |
||
| 542 | * |
||
| 543 | * @return mixed Constraints |
||
| 544 | */ |
||
| 545 | public function getConstraints() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * set Constraints |
||
| 552 | * |
||
| 553 | * @param mixed $constraints constraints |
||
| 554 | * |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | public function setConstraints($constraints) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * add a constraint |
||
| 564 | * |
||
| 565 | * @param string $name constraint name |
||
| 566 | * |
||
| 567 | * @return void |
||
| 568 | */ |
||
| 569 | public function addConstraint($name) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * set items |
||
| 576 | * |
||
| 577 | * @param Schema $items items schema |
||
| 578 | * |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | public function setItems($items) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * get items |
||
| 588 | * |
||
| 589 | * @return Schema |
||
| 590 | */ |
||
| 591 | public function getItems() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * add a property |
||
| 598 | * |
||
| 599 | * @param string $name property name |
||
| 600 | * @param Schema $property property |
||
| 601 | * |
||
| 602 | * @return void |
||
| 603 | */ |
||
| 604 | public function addProperty($name, $property) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * removes a property |
||
| 611 | * |
||
| 612 | * @param string $name property name |
||
| 613 | * |
||
| 614 | * @return void |
||
| 615 | */ |
||
| 616 | public function removeProperty($name) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * returns a property |
||
| 625 | * |
||
| 626 | * @param string $name property name |
||
| 627 | * |
||
| 628 | * @return void|Schema property |
||
| 629 | */ |
||
| 630 | public function getProperty($name) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * get properties |
||
| 637 | * |
||
| 638 | * @return ArrayCollection|null |
||
| 639 | */ |
||
| 640 | public function getProperties() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * set additionalProperties on schema |
||
| 651 | * |
||
| 652 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 653 | * |
||
| 654 | * @return void |
||
| 655 | */ |
||
| 656 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
||
| 657 | { |
||
| 658 | $this->additionalProperties = $additionalProperties; |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * get addtionalProperties for schema |
||
| 663 | * |
||
| 664 | * @return SchemaAdditionalProperties |
||
| 665 | */ |
||
| 666 | public function getAdditionalProperties() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * set required variables |
||
| 673 | * |
||
| 674 | * @param string[] $required array of required fields |
||
| 675 | * |
||
| 676 | * @return void |
||
| 677 | */ |
||
| 678 | public function setRequired(array $required) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * get required fields |
||
| 686 | * |
||
| 687 | * @return string[]|null |
||
| 688 | */ |
||
| 689 | public function getRequired() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * set translatable flag |
||
| 701 | * |
||
| 702 | * This flag is a local extension to json schema. |
||
| 703 | * |
||
| 704 | * @param boolean $translatable translatable flag |
||
| 705 | * |
||
| 706 | * @return void |
||
| 707 | */ |
||
| 708 | public function setTranslatable($translatable) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * get translatable flag |
||
| 719 | * |
||
| 720 | * @return boolean |
||
| 721 | */ |
||
| 722 | public function isTranslatable() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * set a array of urls that can extref refer to |
||
| 734 | * |
||
| 735 | * @param array $refCollection urls |
||
| 736 | * |
||
| 737 | * @return void |
||
| 738 | */ |
||
| 739 | public function setRefCollection(array $refCollection) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * get a collection of urls that can extref refer to |
||
| 746 | * |
||
| 747 | * @return array |
||
| 748 | */ |
||
| 749 | public function getRefCollection() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * set an array of possible event names |
||
| 761 | * |
||
| 762 | * @param array $eventNames event names |
||
| 763 | * |
||
| 764 | * @return void |
||
| 765 | */ |
||
| 766 | public function setEventNames(array $eventNames) |
||
| 770 | |||
| 771 | /** |
||
| 772 | * get a collection of possible event names |
||
| 773 | * |
||
| 774 | * @return array |
||
| 775 | */ |
||
| 776 | public function getEventNames() |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Set the readOnly flag |
||
| 788 | * |
||
| 789 | * @param bool $readOnly ReadOnly flag |
||
| 790 | * |
||
| 791 | * @return void |
||
| 792 | */ |
||
| 793 | public function setReadOnly($readOnly) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Get the readOnly flag. |
||
| 800 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 801 | * |
||
| 802 | * @return bool|null true if readOnly isset to true or null if not |
||
| 803 | */ |
||
| 804 | public function getReadOnly() |
||
| 808 | |||
| 809 | /** |
||
| 810 | * get RecordOriginModifiable |
||
| 811 | * |
||
| 812 | * @return boolean RecordOriginModifiable |
||
| 813 | */ |
||
| 814 | public function isRecordOriginModifiable() |
||
| 818 | |||
| 819 | /** |
||
| 820 | * set RecordOriginModifiable |
||
| 821 | * |
||
| 822 | * @param boolean $recordOriginModifiable recordOriginModifiable |
||
| 823 | * |
||
| 824 | * @return void |
||
| 825 | */ |
||
| 826 | public function setRecordOriginModifiable($recordOriginModifiable) |
||
| 830 | |||
| 831 | /** |
||
| 832 | * get RecordOriginException |
||
| 833 | * |
||
| 834 | * @return boolean RecordOriginException |
||
| 835 | */ |
||
| 836 | public function isRecordOriginException() |
||
| 840 | |||
| 841 | /** |
||
| 842 | * set RecordOriginException |
||
| 843 | * |
||
| 844 | * @param boolean $recordOriginException recordOriginException |
||
| 845 | * |
||
| 846 | * @return void |
||
| 847 | */ |
||
| 848 | public function setRecordOriginException($recordOriginException) |
||
| 852 | |||
| 853 | /** |
||
| 854 | * set searchable variables |
||
| 855 | * |
||
| 856 | * @param string[] $searchable array of searchable fields |
||
| 857 | * |
||
| 858 | * @return void |
||
| 859 | */ |
||
| 860 | public function setSearchable($searchable) |
||
| 864 | |||
| 865 | /** |
||
| 866 | * get searchable fields |
||
| 867 | * |
||
| 868 | * @return string[]|null |
||
| 869 | */ |
||
| 870 | public function getSearchable() |
||
| 877 | |||
| 878 | /** |
||
| 879 | * @return array |
||
| 880 | */ |
||
| 881 | public function getTextIndexes() |
||
| 885 | |||
| 886 | /** |
||
| 887 | * get textIndexes fields |
||
| 888 | * |
||
| 889 | * @param array $textIndexes Data array of special text search values |
||
| 890 | * |
||
| 891 | * @return void |
||
| 892 | */ |
||
| 893 | public function setTextIndexes($textIndexes) |
||
| 897 | } |
||
| 898 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.