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 bool |
||
| 93 | */ |
||
| 94 | protected $isVersioning; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string[] |
||
| 98 | */ |
||
| 99 | protected $searchable = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $minLength; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | protected $maxLength; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var int |
||
| 113 | */ |
||
| 114 | protected $minItems; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var int |
||
| 118 | */ |
||
| 119 | protected $maxItems; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var float |
||
| 123 | */ |
||
| 124 | protected $numericMinimum; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var float |
||
| 128 | */ |
||
| 129 | protected $numericMaximum; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var SchemaEnum |
||
| 133 | */ |
||
| 134 | protected $enum; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | protected $regexPattern; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $documentClass; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var array<string> |
||
| 148 | */ |
||
| 149 | protected $constraints; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var array<string> |
||
| 153 | */ |
||
| 154 | protected $textIndexes; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var boolean |
||
| 158 | */ |
||
| 159 | protected $expose; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * these are the BSON primitive types. |
||
| 163 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 164 | * every type set *not* in this set will be carried over to 'format' |
||
| 165 | * |
||
| 166 | * @var string[] |
||
| 167 | */ |
||
| 168 | protected $primitiveTypes = [ |
||
| 169 | 'array', |
||
| 170 | 'boolean', |
||
| 171 | 'integer', |
||
| 172 | 'number', |
||
| 173 | 'null', |
||
| 174 | 'object', |
||
| 175 | 'string' |
||
| 176 | ]; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * those are types that when they are required, a minimal length |
||
| 180 | * shall be specified in schema (or allow null if not required; that will lead |
||
| 181 | * to the inclusion of "null" in the "type" property array) |
||
| 182 | * |
||
| 183 | * @var array |
||
| 184 | */ |
||
| 185 | protected $minLengthTypes = [ |
||
| 186 | 'integer', |
||
| 187 | 'number', |
||
| 188 | 'float', |
||
| 189 | 'double', |
||
| 190 | 'decimal', |
||
| 191 | 'string', |
||
| 192 | 'date', |
||
| 193 | 'extref' |
||
| 194 | ]; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * known non-primitive types we map to primitives here. |
||
| 198 | * the type itself is set to the format. |
||
| 199 | * |
||
| 200 | * @var string[] |
||
| 201 | */ |
||
| 202 | protected $specialTypeMapping = [ |
||
| 203 | 'extref' => 'string', |
||
| 204 | 'translatable' => 'object', |
||
| 205 | 'date' => 'string', |
||
| 206 | 'float' => 'number', |
||
| 207 | 'double' => 'number', |
||
| 208 | 'decimal' => 'number' |
||
| 209 | ]; |
||
| 210 | |||
| 211 | protected $formatOverrides = [ |
||
| 212 | 'date' => 'date-time' |
||
| 213 | ]; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Build properties |
||
| 217 | */ |
||
| 218 | 2 | public function __construct() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * set title |
||
| 225 | * |
||
| 226 | * @param string $title title |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | 2 | public function setTitle($title) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * get title |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 2 | public function getTitle() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * set description |
||
| 247 | * |
||
| 248 | * @param string $description description |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | public function setDescription($description) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * get description |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getDescription() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * set type |
||
| 269 | * |
||
| 270 | * @param string|array $types types |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | public function setType($types) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * get type |
||
| 312 | * |
||
| 313 | * @return SchemaType type |
||
| 314 | */ |
||
| 315 | public function getType() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * get MinLengthTypes |
||
| 322 | * |
||
| 323 | * @return array MinLengthTypes |
||
| 324 | */ |
||
| 325 | public function getMinLengthTypes() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * get format |
||
| 332 | * |
||
| 333 | * @return string format |
||
| 334 | */ |
||
| 335 | public function getFormat() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * sets format |
||
| 342 | * |
||
| 343 | * @param string $format format |
||
| 344 | * |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | public function setFormat($format) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * get numeric minimum |
||
| 354 | * |
||
| 355 | * @return float numeric minimum |
||
| 356 | */ |
||
| 357 | public function getNumericMinimum() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * set numeric minimum |
||
| 364 | * |
||
| 365 | * @param float $numericMinimum numeric mimimum |
||
| 366 | * |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | public function setNumericMinimum($numericMinimum) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * get numeric maximum |
||
| 376 | * |
||
| 377 | * @return float numeric maximum |
||
| 378 | */ |
||
| 379 | public function getNumericMaximum() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * set numeric maximum |
||
| 386 | * |
||
| 387 | * @param float $numericMaximum maximum |
||
| 388 | * |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | public function setNumericMaximum($numericMaximum) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * set min length |
||
| 398 | * |
||
| 399 | * @return int length |
||
| 400 | */ |
||
| 401 | public function getMinLength() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * get min length |
||
| 408 | * |
||
| 409 | * @param int $minLength length |
||
| 410 | * |
||
| 411 | * @return void |
||
| 412 | */ |
||
| 413 | public function setMinLength($minLength) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * gets maxlength |
||
| 420 | * |
||
| 421 | * @return int length |
||
| 422 | */ |
||
| 423 | public function getMaxLength() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * set maxlength |
||
| 430 | * |
||
| 431 | * @param int $maxLength length |
||
| 432 | * |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | public function setMaxLength($maxLength) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * set min Items |
||
| 442 | * |
||
| 443 | * @return int Items |
||
| 444 | */ |
||
| 445 | public function getMinItems() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * get min Items |
||
| 452 | * |
||
| 453 | * @param int $minItems length |
||
| 454 | * |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | public function setMinItems($minItems) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * gets maxItems |
||
| 464 | * |
||
| 465 | * @return int Items |
||
| 466 | */ |
||
| 467 | public function getMaxItems() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * set maxItems |
||
| 474 | * |
||
| 475 | * @param int $maxItems Items |
||
| 476 | * |
||
| 477 | * @return void |
||
| 478 | */ |
||
| 479 | public function setMaxItems($maxItems) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * get Enum |
||
| 486 | * |
||
| 487 | * @return array Enum |
||
|
|
|||
| 488 | */ |
||
| 489 | public function getEnum() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * set Enum |
||
| 496 | * |
||
| 497 | * @param array $enum enum |
||
| 498 | * |
||
| 499 | * @return void |
||
| 500 | */ |
||
| 501 | public function setEnum(array $enum) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * get regex pattern |
||
| 508 | * |
||
| 509 | * @return string pattern |
||
| 510 | */ |
||
| 511 | public function getRegexPattern() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * set regex pattern |
||
| 518 | * |
||
| 519 | * @param string $regexPattern regex pattern |
||
| 520 | * |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | public function setRegexPattern($regexPattern) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * get DocumentClass |
||
| 530 | * |
||
| 531 | * @return string DocumentClass |
||
| 532 | */ |
||
| 533 | public function getDocumentClass() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * set DocumentClass |
||
| 540 | * |
||
| 541 | * @param string $documentClass documentClass |
||
| 542 | * |
||
| 543 | * @return void |
||
| 544 | */ |
||
| 545 | public function setDocumentClass($documentClass) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * get Constraints |
||
| 552 | * |
||
| 553 | * @return mixed Constraints |
||
| 554 | */ |
||
| 555 | public function getConstraints() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * set Constraints |
||
| 562 | * |
||
| 563 | * @param mixed $constraints constraints |
||
| 564 | * |
||
| 565 | * @return void |
||
| 566 | */ |
||
| 567 | public function setConstraints($constraints) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * add a constraint |
||
| 574 | * |
||
| 575 | * @param string $name constraint name |
||
| 576 | * |
||
| 577 | * @return void |
||
| 578 | */ |
||
| 579 | public function addConstraint($name) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * set items |
||
| 586 | * |
||
| 587 | * @param Schema $items items schema |
||
| 588 | * |
||
| 589 | * @return void |
||
| 590 | */ |
||
| 591 | public function setItems($items) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * get items |
||
| 598 | * |
||
| 599 | * @return Schema |
||
| 600 | */ |
||
| 601 | public function getItems() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * add a property |
||
| 608 | * |
||
| 609 | * @param string $name property name |
||
| 610 | * @param Schema $property property |
||
| 611 | * |
||
| 612 | * @return void |
||
| 613 | */ |
||
| 614 | public function addProperty($name, $property) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * removes a property |
||
| 621 | * |
||
| 622 | * @param string $name property name |
||
| 623 | * |
||
| 624 | * @return void |
||
| 625 | */ |
||
| 626 | public function removeProperty($name) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * returns a property |
||
| 635 | * |
||
| 636 | * @param string $name property name |
||
| 637 | * |
||
| 638 | * @return null|Schema property |
||
| 639 | */ |
||
| 640 | public function getProperty($name = null) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * get properties |
||
| 650 | * |
||
| 651 | * @return ArrayCollection|null |
||
| 652 | */ |
||
| 653 | public function getProperties() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * set additionalProperties on schema |
||
| 664 | * |
||
| 665 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 666 | * |
||
| 667 | * @return void |
||
| 668 | */ |
||
| 669 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * get addtionalProperties for schema |
||
| 676 | * |
||
| 677 | * @return SchemaAdditionalProperties |
||
| 678 | */ |
||
| 679 | public function getAdditionalProperties() |
||
| 683 | |||
| 684 | /** |
||
| 685 | * set required variables |
||
| 686 | * |
||
| 687 | * @param string[] $required array of required fields |
||
| 688 | * |
||
| 689 | * @return void |
||
| 690 | */ |
||
| 691 | public function setRequired(array $required) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * get required fields |
||
| 699 | * |
||
| 700 | * @return string[]|null |
||
| 701 | */ |
||
| 702 | public function getRequired() |
||
| 711 | |||
| 712 | /** |
||
| 713 | * set translatable flag |
||
| 714 | * |
||
| 715 | * This flag is a local extension to json schema. |
||
| 716 | * |
||
| 717 | * @param boolean $translatable translatable flag |
||
| 718 | * |
||
| 719 | * @return void |
||
| 720 | */ |
||
| 721 | public function setTranslatable($translatable) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * get translatable flag |
||
| 732 | * |
||
| 733 | * @return boolean |
||
| 734 | */ |
||
| 735 | public function isTranslatable() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * set a array of urls that can extref refer to |
||
| 747 | * |
||
| 748 | * @param array $refCollection urls |
||
| 749 | * |
||
| 750 | * @return void |
||
| 751 | */ |
||
| 752 | public function setRefCollection(array $refCollection) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * get a collection of urls that can extref refer to |
||
| 759 | * |
||
| 760 | * @return array |
||
| 761 | */ |
||
| 762 | public function getRefCollection() |
||
| 771 | |||
| 772 | /** |
||
| 773 | * set an array of possible event names |
||
| 774 | * |
||
| 775 | * @param array $eventNames event names |
||
| 776 | * |
||
| 777 | * @return void |
||
| 778 | */ |
||
| 779 | public function setEventNames(array $eventNames) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * get a collection of possible event names |
||
| 786 | * |
||
| 787 | * @return array |
||
| 788 | */ |
||
| 789 | public function getEventNames() |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Set the readOnly flag |
||
| 801 | * |
||
| 802 | * @param bool $readOnly ReadOnly flag |
||
| 803 | * |
||
| 804 | * @return void |
||
| 805 | */ |
||
| 806 | public function setReadOnly($readOnly) |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Get the readOnly flag. |
||
| 813 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 814 | * |
||
| 815 | * @return bool|null true if readOnly isset to true or null if not |
||
| 816 | */ |
||
| 817 | public function getReadOnly() |
||
| 821 | |||
| 822 | /** |
||
| 823 | * get RecordOriginModifiable |
||
| 824 | * |
||
| 825 | * @return boolean RecordOriginModifiable |
||
| 826 | */ |
||
| 827 | public function isRecordOriginModifiable() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * set RecordOriginModifiable |
||
| 834 | * |
||
| 835 | * @param boolean $recordOriginModifiable recordOriginModifiable |
||
| 836 | * |
||
| 837 | * @return void |
||
| 838 | */ |
||
| 839 | public function setRecordOriginModifiable($recordOriginModifiable) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * get RecordOriginException |
||
| 846 | * |
||
| 847 | * @return boolean RecordOriginException |
||
| 848 | */ |
||
| 849 | public function isRecordOriginException() |
||
| 853 | |||
| 854 | /** |
||
| 855 | * set RecordOriginException |
||
| 856 | * |
||
| 857 | * @param boolean $recordOriginException recordOriginException |
||
| 858 | * |
||
| 859 | * @return void |
||
| 860 | */ |
||
| 861 | public function setRecordOriginException($recordOriginException) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * isVersioning |
||
| 868 | * |
||
| 869 | * @return bool IsVersioning |
||
| 870 | */ |
||
| 871 | public function isVersioning() |
||
| 875 | |||
| 876 | /** |
||
| 877 | * set IsVersioning |
||
| 878 | * |
||
| 879 | * @param bool $isVersioning isVersioning |
||
| 880 | * |
||
| 881 | * @return void |
||
| 882 | */ |
||
| 883 | public function setIsVersioning($isVersioning) |
||
| 887 | |||
| 888 | /** |
||
| 889 | * set searchable variables |
||
| 890 | * |
||
| 891 | * @param string[] $searchable array of searchable fields |
||
| 892 | * |
||
| 893 | * @return void |
||
| 894 | */ |
||
| 895 | public function setSearchable($searchable) |
||
| 899 | |||
| 900 | /** |
||
| 901 | * get searchable fields |
||
| 902 | * |
||
| 903 | * @return string[]|null |
||
| 904 | */ |
||
| 905 | public function getSearchable() |
||
| 912 | |||
| 913 | /** |
||
| 914 | * @return array |
||
| 915 | */ |
||
| 916 | public function getTextIndexes() |
||
| 920 | |||
| 921 | /** |
||
| 922 | * get textIndexes fields |
||
| 923 | * |
||
| 924 | * @param array $textIndexes Data array of special text search values |
||
| 925 | * |
||
| 926 | * @return void |
||
| 927 | */ |
||
| 928 | public function setTextIndexes($textIndexes) |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Visible item in schema output |
||
| 935 | * |
||
| 936 | * @return bool |
||
| 937 | */ |
||
| 938 | public function isExpose() |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Set visible en schema serializer |
||
| 945 | * |
||
| 946 | * @param bool $expose boolean value |
||
| 947 | * |
||
| 948 | * @return void |
||
| 949 | */ |
||
| 950 | public function setExpose($expose) |
||
| 954 | } |
||
| 955 |
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.