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 $groups; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var array<string> |
||
| 153 | */ |
||
| 154 | protected $constraints; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var array |
||
| 158 | */ |
||
| 159 | protected $variations; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var array |
||
| 163 | */ |
||
| 164 | protected $onVariation; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var array<string> |
||
| 168 | */ |
||
| 169 | protected $textIndexes; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * these are the BSON primitive types. |
||
| 173 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 174 | * every type set *not* in this set will be carried over to 'format' |
||
| 175 | * |
||
| 176 | * @var string[] |
||
| 177 | */ |
||
| 178 | protected $primitiveTypes = [ |
||
| 179 | 'array', |
||
| 180 | 'boolean', |
||
| 181 | 'integer', |
||
| 182 | 'number', |
||
| 183 | 'null', |
||
| 184 | 'object', |
||
| 185 | 'string' |
||
| 186 | ]; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * those are types that when they are required, a minimal length |
||
| 190 | * shall be specified in schema (or allow null if not required; that will lead |
||
| 191 | * to the inclusion of "null" in the "type" property array) |
||
| 192 | * |
||
| 193 | * @var array |
||
| 194 | */ |
||
| 195 | protected $minLengthTypes = [ |
||
| 196 | 'integer', |
||
| 197 | 'number', |
||
| 198 | 'float', |
||
| 199 | 'double', |
||
| 200 | 'decimal', |
||
| 201 | 'string', |
||
| 202 | 'date', |
||
| 203 | 'extref', |
||
| 204 | 'translatable' |
||
| 205 | ]; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * known non-primitive types we map to primitives here. |
||
| 209 | * the type itself is set to the format. |
||
| 210 | * |
||
| 211 | * @var string[] |
||
| 212 | */ |
||
| 213 | protected $specialTypeMapping = [ |
||
| 214 | 'extref' => 'string', |
||
| 215 | 'translatable' => 'object', |
||
| 216 | 'date' => 'string', |
||
| 217 | 'float' => 'number', |
||
| 218 | 'double' => 'number', |
||
| 219 | 'decimal' => 'number' |
||
| 220 | ]; |
||
| 221 | |||
| 222 | protected $formatOverrides = [ |
||
| 223 | 'date' => 'date-time' |
||
| 224 | ]; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Build properties |
||
| 228 | */ |
||
| 229 | 2 | public function __construct() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * set title |
||
| 236 | * |
||
| 237 | * @param string $title title |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | 2 | public function setTitle($title) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * get title |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | 2 | public function getTitle() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * set description |
||
| 258 | * |
||
| 259 | * @param string $description description |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function setDescription($description) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * get description |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getDescription() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * set type |
||
| 280 | * |
||
| 281 | * @param string|array $types types |
||
| 282 | * |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function setType($types) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * get type |
||
| 323 | * |
||
| 324 | * @return SchemaType type |
||
| 325 | */ |
||
| 326 | public function getType() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * get MinLengthTypes |
||
| 333 | * |
||
| 334 | * @return array MinLengthTypes |
||
| 335 | */ |
||
| 336 | public function getMinLengthTypes() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * get format |
||
| 343 | * |
||
| 344 | * @return string format |
||
| 345 | */ |
||
| 346 | public function getFormat() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * sets format |
||
| 353 | * |
||
| 354 | * @param string $format format |
||
| 355 | * |
||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | public function setFormat($format) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * get numeric minimum |
||
| 365 | * |
||
| 366 | * @return float numeric minimum |
||
| 367 | */ |
||
| 368 | public function getNumericMinimum() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * set numeric minimum |
||
| 375 | * |
||
| 376 | * @param float $numericMinimum numeric mimimum |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | public function setNumericMinimum($numericMinimum) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * get numeric maximum |
||
| 387 | * |
||
| 388 | * @return float numeric maximum |
||
| 389 | */ |
||
| 390 | public function getNumericMaximum() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * set numeric maximum |
||
| 397 | * |
||
| 398 | * @param float $numericMaximum maximum |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | public function setNumericMaximum($numericMaximum) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * set min length |
||
| 409 | * |
||
| 410 | * @return int length |
||
| 411 | */ |
||
| 412 | public function getMinLength() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * get min length |
||
| 419 | * |
||
| 420 | * @param int $minLength length |
||
| 421 | * |
||
| 422 | * @return void |
||
| 423 | */ |
||
| 424 | public function setMinLength($minLength) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * gets maxlength |
||
| 431 | * |
||
| 432 | * @return int length |
||
| 433 | */ |
||
| 434 | public function getMaxLength() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * set maxlength |
||
| 441 | * |
||
| 442 | * @param int $maxLength length |
||
| 443 | * |
||
| 444 | * @return void |
||
| 445 | */ |
||
| 446 | public function setMaxLength($maxLength) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * set min Items |
||
| 453 | * |
||
| 454 | * @return int Items |
||
| 455 | */ |
||
| 456 | public function getMinItems() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * get min Items |
||
| 463 | * |
||
| 464 | * @param int $minItems length |
||
| 465 | * |
||
| 466 | * @return void |
||
| 467 | */ |
||
| 468 | public function setMinItems($minItems) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * gets maxItems |
||
| 475 | * |
||
| 476 | * @return int Items |
||
| 477 | */ |
||
| 478 | public function getMaxItems() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * set maxItems |
||
| 485 | * |
||
| 486 | * @param int $maxItems Items |
||
| 487 | * |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | public function setMaxItems($maxItems) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * get Enum |
||
| 497 | * |
||
| 498 | * @return array Enum |
||
|
|
|||
| 499 | */ |
||
| 500 | public function getEnum() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * set Enum |
||
| 507 | * |
||
| 508 | * @param array $enum enum |
||
| 509 | * |
||
| 510 | * @return void |
||
| 511 | */ |
||
| 512 | public function setEnum(array $enum) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * get regex pattern |
||
| 519 | * |
||
| 520 | * @return string pattern |
||
| 521 | */ |
||
| 522 | public function getRegexPattern() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * set regex pattern |
||
| 529 | * |
||
| 530 | * @param string $regexPattern regex pattern |
||
| 531 | * |
||
| 532 | * @return void |
||
| 533 | */ |
||
| 534 | public function setRegexPattern($regexPattern) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * get DocumentClass |
||
| 541 | * |
||
| 542 | * @return string DocumentClass |
||
| 543 | */ |
||
| 544 | public function getDocumentClass() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * set DocumentClass |
||
| 551 | * |
||
| 552 | * @param string $documentClass documentClass |
||
| 553 | * |
||
| 554 | * @return void |
||
| 555 | */ |
||
| 556 | public function setDocumentClass($documentClass) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * get Groups |
||
| 563 | * |
||
| 564 | * @return array Groups |
||
| 565 | */ |
||
| 566 | public function getGroups() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * set Groups |
||
| 573 | * |
||
| 574 | * @param array $groups groups |
||
| 575 | * |
||
| 576 | * @return void |
||
| 577 | */ |
||
| 578 | public function setGroups($groups) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * get Constraints |
||
| 585 | * |
||
| 586 | * @return mixed Constraints |
||
| 587 | */ |
||
| 588 | public function getConstraints() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * set Constraints |
||
| 595 | * |
||
| 596 | * @param mixed $constraints constraints |
||
| 597 | * |
||
| 598 | * @return void |
||
| 599 | */ |
||
| 600 | public function setConstraints($constraints) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * add a constraint |
||
| 607 | * |
||
| 608 | * @param string $name constraint name |
||
| 609 | * |
||
| 610 | * @return void |
||
| 611 | */ |
||
| 612 | public function addConstraint($name) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * set items |
||
| 619 | * |
||
| 620 | * @param Schema $items items schema |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | public function setItems($items) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * get items |
||
| 631 | * |
||
| 632 | * @return Schema |
||
| 633 | */ |
||
| 634 | public function getItems() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * add a property |
||
| 641 | * |
||
| 642 | * @param string $name property name |
||
| 643 | * @param Schema $property property |
||
| 644 | * |
||
| 645 | * @return void |
||
| 646 | */ |
||
| 647 | public function addProperty($name, $property) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * removes a property |
||
| 654 | * |
||
| 655 | * @param string $name property name |
||
| 656 | * |
||
| 657 | * @return void |
||
| 658 | */ |
||
| 659 | public function removeProperty($name) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * returns a property |
||
| 668 | * |
||
| 669 | * @param string $name property name |
||
| 670 | * |
||
| 671 | * @return null|Schema property |
||
| 672 | */ |
||
| 673 | public function getProperty($name = null) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * get properties |
||
| 683 | * |
||
| 684 | * @return ArrayCollection|null |
||
| 685 | */ |
||
| 686 | public function getProperties() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * set additionalProperties on schema |
||
| 697 | * |
||
| 698 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 699 | * |
||
| 700 | * @return void |
||
| 701 | */ |
||
| 702 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * get addtionalProperties for schema |
||
| 709 | * |
||
| 710 | * @return SchemaAdditionalProperties |
||
| 711 | */ |
||
| 712 | public function getAdditionalProperties() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * set required variables |
||
| 719 | * |
||
| 720 | * @param string[] $required array of required fields |
||
| 721 | * |
||
| 722 | * @return void |
||
| 723 | */ |
||
| 724 | public function setRequired(array $required) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * get required fields |
||
| 732 | * |
||
| 733 | * @return string[]|null |
||
| 734 | */ |
||
| 735 | public function getRequired() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * set translatable flag |
||
| 747 | * |
||
| 748 | * This flag is a local extension to json schema. |
||
| 749 | * |
||
| 750 | * @param boolean $translatable translatable flag |
||
| 751 | * |
||
| 752 | * @return void |
||
| 753 | */ |
||
| 754 | public function setTranslatable($translatable) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * get translatable flag |
||
| 765 | * |
||
| 766 | * @return boolean |
||
| 767 | */ |
||
| 768 | public function isTranslatable() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * set a array of urls that can extref refer to |
||
| 780 | * |
||
| 781 | * @param array $refCollection urls |
||
| 782 | * |
||
| 783 | * @return void |
||
| 784 | */ |
||
| 785 | public function setRefCollection(array $refCollection) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * get a collection of urls that can extref refer to |
||
| 792 | * |
||
| 793 | * @return array |
||
| 794 | */ |
||
| 795 | public function getRefCollection() |
||
| 804 | |||
| 805 | /** |
||
| 806 | * set an array of possible event names |
||
| 807 | * |
||
| 808 | * @param array $eventNames event names |
||
| 809 | * |
||
| 810 | * @return void |
||
| 811 | */ |
||
| 812 | public function setEventNames(array $eventNames) |
||
| 816 | |||
| 817 | /** |
||
| 818 | * get a collection of possible event names |
||
| 819 | * |
||
| 820 | * @return array |
||
| 821 | */ |
||
| 822 | public function getEventNames() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Set the readOnly flag |
||
| 834 | * |
||
| 835 | * @param bool $readOnly ReadOnly flag |
||
| 836 | * |
||
| 837 | * @return void |
||
| 838 | */ |
||
| 839 | public function setReadOnly($readOnly) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Get the readOnly flag. |
||
| 846 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 847 | * |
||
| 848 | * @return bool|null true if readOnly isset to true or null if not |
||
| 849 | */ |
||
| 850 | public function getReadOnly() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * get RecordOriginModifiable |
||
| 857 | * |
||
| 858 | * @return boolean RecordOriginModifiable |
||
| 859 | */ |
||
| 860 | public function isRecordOriginModifiable() |
||
| 864 | |||
| 865 | /** |
||
| 866 | * set RecordOriginModifiable |
||
| 867 | * |
||
| 868 | * @param boolean $recordOriginModifiable recordOriginModifiable |
||
| 869 | * |
||
| 870 | * @return void |
||
| 871 | */ |
||
| 872 | public function setRecordOriginModifiable($recordOriginModifiable) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * get RecordOriginException |
||
| 879 | * |
||
| 880 | * @return boolean RecordOriginException |
||
| 881 | */ |
||
| 882 | public function isRecordOriginException() |
||
| 886 | |||
| 887 | /** |
||
| 888 | * set RecordOriginException |
||
| 889 | * |
||
| 890 | * @param boolean $recordOriginException recordOriginException |
||
| 891 | * |
||
| 892 | * @return void |
||
| 893 | */ |
||
| 894 | public function setRecordOriginException($recordOriginException) |
||
| 898 | |||
| 899 | /** |
||
| 900 | * isVersioning |
||
| 901 | * |
||
| 902 | * @return bool IsVersioning |
||
| 903 | */ |
||
| 904 | public function isVersioning() |
||
| 908 | |||
| 909 | /** |
||
| 910 | * set IsVersioning |
||
| 911 | * |
||
| 912 | * @param bool $isVersioning isVersioning |
||
| 913 | * |
||
| 914 | * @return void |
||
| 915 | */ |
||
| 916 | public function setIsVersioning($isVersioning) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * set searchable variables |
||
| 923 | * |
||
| 924 | * @param string[] $searchable array of searchable fields |
||
| 925 | * |
||
| 926 | * @return void |
||
| 927 | */ |
||
| 928 | public function setSearchable($searchable) |
||
| 932 | |||
| 933 | /** |
||
| 934 | * get searchable fields |
||
| 935 | * |
||
| 936 | * @return string[]|null |
||
| 937 | */ |
||
| 938 | public function getSearchable() |
||
| 945 | |||
| 946 | /** |
||
| 947 | * get Variations |
||
| 948 | * |
||
| 949 | * @return array Variations |
||
| 950 | */ |
||
| 951 | public function getVariations() |
||
| 955 | |||
| 956 | /** |
||
| 957 | * set Variations |
||
| 958 | * |
||
| 959 | * @param array $variations variations |
||
| 960 | * |
||
| 961 | * @return void |
||
| 962 | */ |
||
| 963 | public function setVariations($variations) |
||
| 967 | |||
| 968 | /** |
||
| 969 | * get OnVariation |
||
| 970 | * |
||
| 971 | * @return array OnVariation |
||
| 972 | */ |
||
| 973 | public function getOnVariation() |
||
| 977 | |||
| 978 | /** |
||
| 979 | * set OnVariation |
||
| 980 | * |
||
| 981 | * @param array $onVariation onVariation |
||
| 982 | * |
||
| 983 | * @return void |
||
| 984 | */ |
||
| 985 | public function setOnVariation($onVariation) |
||
| 989 | |||
| 990 | /** |
||
| 991 | * @return array |
||
| 992 | */ |
||
| 993 | public function getTextIndexes() |
||
| 997 | |||
| 998 | /** |
||
| 999 | * get textIndexes fields |
||
| 1000 | * |
||
| 1001 | * @param array $textIndexes Data array of special text search values |
||
| 1002 | * |
||
| 1003 | * @return void |
||
| 1004 | */ |
||
| 1005 | public function setTextIndexes($textIndexes) |
||
| 1009 | } |
||
| 1010 |
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.