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 string[] |
||
| 83 | */ |
||
| 84 | protected $searchable = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | protected $minLength; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var int |
||
| 93 | */ |
||
| 94 | protected $maxLength; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $minItems; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $maxItems; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var float |
||
| 108 | */ |
||
| 109 | protected $numericMinimum; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var float |
||
| 113 | */ |
||
| 114 | protected $numericMaximum; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var SchemaEnum |
||
| 118 | */ |
||
| 119 | protected $enum; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $regexPattern; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | protected $documentClass; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array<string> |
||
| 133 | */ |
||
| 134 | protected $constraints; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * these are the BSON primitive types. |
||
| 138 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 139 | * every type set *not* in this set will be carried over to 'format' |
||
| 140 | * |
||
| 141 | * @var string[] |
||
| 142 | */ |
||
| 143 | protected $primitiveTypes = [ |
||
| 144 | 'array', |
||
| 145 | 'boolean', |
||
| 146 | 'integer', |
||
| 147 | 'number', |
||
| 148 | 'null', |
||
| 149 | 'object', |
||
| 150 | 'string' |
||
| 151 | ]; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * those are types that when they are required, a minimal length |
||
| 155 | * shall be specified in schema (or allow null if not required; that will lead |
||
| 156 | * to the inclusion of "null" in the "type" property array) |
||
| 157 | * |
||
| 158 | * @var array |
||
| 159 | */ |
||
| 160 | protected $minLengthTypes = [ |
||
| 161 | 'integer', |
||
| 162 | 'number', |
||
| 163 | 'float', |
||
| 164 | 'double', |
||
| 165 | 'decimal', |
||
| 166 | 'string', |
||
| 167 | 'date', |
||
| 168 | 'extref' |
||
| 169 | ]; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * known non-primitive types we map to primitives here. |
||
| 173 | * the type itself is set to the format. |
||
| 174 | * |
||
| 175 | * @var string[] |
||
| 176 | */ |
||
| 177 | protected $specialTypeMapping = [ |
||
| 178 | 'extref' => 'string', |
||
| 179 | 'translatable' => 'object', |
||
| 180 | 'date' => 'string', |
||
| 181 | 'float' => 'number', |
||
| 182 | 'double' => 'number', |
||
| 183 | 'decimal' => 'number' |
||
| 184 | ]; |
||
| 185 | |||
| 186 | protected $formatOverrides = [ |
||
| 187 | 'date' => 'date-time' |
||
| 188 | ]; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Build properties |
||
| 192 | */ |
||
| 193 | 4 | public function __construct() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * set title |
||
| 200 | * |
||
| 201 | * @param string $title title |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | 4 | public function setTitle($title) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * get title |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | 4 | public function getTitle() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * set description |
||
| 222 | * |
||
| 223 | * @param string $description description |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | 2 | public function setDescription($description) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * get description |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 2 | public function getDescription() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * set type |
||
| 244 | * |
||
| 245 | * @param string|array $types types |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | 2 | public function setType($types) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * get type |
||
| 287 | * |
||
| 288 | * @return SchemaType type |
||
| 289 | */ |
||
| 290 | 2 | public function getType() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * get MinLengthTypes |
||
| 297 | * |
||
| 298 | * @return array MinLengthTypes |
||
| 299 | */ |
||
| 300 | 2 | public function getMinLengthTypes() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * get format |
||
| 307 | * |
||
| 308 | * @return string format |
||
| 309 | */ |
||
| 310 | 2 | public function getFormat() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * sets format |
||
| 317 | * |
||
| 318 | * @param string $format format |
||
| 319 | * |
||
| 320 | * @return void |
||
| 321 | */ |
||
| 322 | 2 | public function setFormat($format) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * get numeric minimum |
||
| 329 | * |
||
| 330 | * @return float numeric minimum |
||
| 331 | */ |
||
| 332 | 2 | public function getNumericMinimum() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * set numeric minimum |
||
| 339 | * |
||
| 340 | * @param float $numericMinimum numeric mimimum |
||
| 341 | * |
||
| 342 | * @return void |
||
| 343 | */ |
||
| 344 | public function setNumericMinimum($numericMinimum) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * get numeric maximum |
||
| 351 | * |
||
| 352 | * @return float numeric maximum |
||
| 353 | */ |
||
| 354 | 2 | public function getNumericMaximum() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * set numeric maximum |
||
| 361 | * |
||
| 362 | * @param float $numericMaximum maximum |
||
| 363 | * |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | public function setNumericMaximum($numericMaximum) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * set min length |
||
| 373 | * |
||
| 374 | * @return int length |
||
| 375 | */ |
||
| 376 | 2 | public function getMinLength() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * get min length |
||
| 383 | * |
||
| 384 | * @param int $minLength length |
||
| 385 | * |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | public function setMinLength($minLength) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * gets maxlength |
||
| 395 | * |
||
| 396 | * @return int length |
||
| 397 | */ |
||
| 398 | 2 | public function getMaxLength() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * set maxlength |
||
| 405 | * |
||
| 406 | * @param int $maxLength length |
||
| 407 | * |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | public function setMaxLength($maxLength) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * set min Items |
||
| 417 | * |
||
| 418 | * @return int Items |
||
| 419 | */ |
||
| 420 | 2 | public function getMinItems() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * get min Items |
||
| 427 | * |
||
| 428 | * @param int $minItems length |
||
| 429 | * |
||
| 430 | * @return void |
||
| 431 | */ |
||
| 432 | public function setMinItems($minItems) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * gets maxItems |
||
| 439 | * |
||
| 440 | * @return int Items |
||
| 441 | */ |
||
| 442 | 2 | public function getMaxItems() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * set maxItems |
||
| 449 | * |
||
| 450 | * @param int $maxItems Items |
||
| 451 | * |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | public function setMaxItems($maxItems) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * get Enum |
||
| 461 | * |
||
| 462 | * @return array Enum |
||
|
|
|||
| 463 | */ |
||
| 464 | 2 | public function getEnum() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * set Enum |
||
| 471 | * |
||
| 472 | * @param array $enum enum |
||
| 473 | * |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | public function setEnum(array $enum) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * get regex pattern |
||
| 483 | * |
||
| 484 | * @return string pattern |
||
| 485 | */ |
||
| 486 | 2 | public function getRegexPattern() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * set regex pattern |
||
| 493 | * |
||
| 494 | * @param string $regexPattern regex pattern |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | public function setRegexPattern($regexPattern) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * get DocumentClass |
||
| 505 | * |
||
| 506 | * @return string DocumentClass |
||
| 507 | */ |
||
| 508 | 2 | public function getDocumentClass() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * set DocumentClass |
||
| 515 | * |
||
| 516 | * @param string $documentClass documentClass |
||
| 517 | * |
||
| 518 | * @return void |
||
| 519 | */ |
||
| 520 | 2 | public function setDocumentClass($documentClass) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * get Constraints |
||
| 527 | * |
||
| 528 | * @return mixed Constraints |
||
| 529 | */ |
||
| 530 | 2 | public function getConstraints() |
|
| 534 | |||
| 535 | /** |
||
| 536 | * set Constraints |
||
| 537 | * |
||
| 538 | * @param mixed $constraints constraints |
||
| 539 | * |
||
| 540 | * @return void |
||
| 541 | */ |
||
| 542 | public function setConstraints($constraints) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * add a constraint |
||
| 549 | * |
||
| 550 | * @param string $name constraint name |
||
| 551 | * |
||
| 552 | * @return void |
||
| 553 | */ |
||
| 554 | public function addConstraint($name) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * set items |
||
| 561 | * |
||
| 562 | * @param Schema $items items schema |
||
| 563 | * |
||
| 564 | * @return void |
||
| 565 | */ |
||
| 566 | 2 | public function setItems($items) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * get items |
||
| 573 | * |
||
| 574 | * @return Schema |
||
| 575 | */ |
||
| 576 | 2 | public function getItems() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * add a property |
||
| 583 | * |
||
| 584 | * @param string $name property name |
||
| 585 | * @param Schema $property property |
||
| 586 | * |
||
| 587 | * @return void |
||
| 588 | */ |
||
| 589 | 2 | public function addProperty($name, $property) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * removes a property |
||
| 596 | * |
||
| 597 | * @param string $name property name |
||
| 598 | * |
||
| 599 | * @return void |
||
| 600 | */ |
||
| 601 | 2 | public function removeProperty($name) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * returns a property |
||
| 610 | * |
||
| 611 | * @param string $name property name |
||
| 612 | * |
||
| 613 | * @return void|Schema property |
||
| 614 | */ |
||
| 615 | public function getProperty($name) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * get properties |
||
| 622 | * |
||
| 623 | * @return ArrayCollection|null |
||
| 624 | */ |
||
| 625 | 2 | public function getProperties() |
|
| 633 | |||
| 634 | /** |
||
| 635 | * set additionalProperties on schema |
||
| 636 | * |
||
| 637 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 638 | * |
||
| 639 | * @return void |
||
| 640 | */ |
||
| 641 | 2 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * get addtionalProperties for schema |
||
| 648 | * |
||
| 649 | * @return SchemaAdditionalProperties |
||
| 650 | */ |
||
| 651 | 2 | public function getAdditionalProperties() |
|
| 655 | |||
| 656 | /** |
||
| 657 | * set required variables |
||
| 658 | * |
||
| 659 | * @param string[] $required array of required fields |
||
| 660 | * |
||
| 661 | * @return void |
||
| 662 | */ |
||
| 663 | 2 | public function setRequired(array $required) |
|
| 668 | |||
| 669 | /** |
||
| 670 | * get required fields |
||
| 671 | * |
||
| 672 | * @return string[]|null |
||
| 673 | */ |
||
| 674 | 2 | public function getRequired() |
|
| 683 | |||
| 684 | /** |
||
| 685 | * set translatable flag |
||
| 686 | * |
||
| 687 | * This flag is a local extension to json schema. |
||
| 688 | * |
||
| 689 | * @param boolean $translatable translatable flag |
||
| 690 | * |
||
| 691 | * @return void |
||
| 692 | */ |
||
| 693 | public function setTranslatable($translatable) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * get translatable flag |
||
| 704 | * |
||
| 705 | * @return boolean |
||
| 706 | */ |
||
| 707 | public function isTranslatable() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * set a array of urls that can extref refer to |
||
| 719 | * |
||
| 720 | * @param array $refCollection urls |
||
| 721 | * |
||
| 722 | * @return void |
||
| 723 | */ |
||
| 724 | public function setRefCollection(array $refCollection) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * get a collection of urls that can extref refer to |
||
| 731 | * |
||
| 732 | * @return array |
||
| 733 | */ |
||
| 734 | 2 | public function getRefCollection() |
|
| 743 | |||
| 744 | /** |
||
| 745 | * set an array of possible event names |
||
| 746 | * |
||
| 747 | * @param array $eventNames event names |
||
| 748 | * |
||
| 749 | * @return void |
||
| 750 | */ |
||
| 751 | 2 | public function setEventNames(array $eventNames) |
|
| 755 | |||
| 756 | /** |
||
| 757 | * get a collection of possible event names |
||
| 758 | * |
||
| 759 | * @return array |
||
| 760 | */ |
||
| 761 | 2 | public function getEventNames() |
|
| 770 | |||
| 771 | /** |
||
| 772 | * Set the readOnly flag |
||
| 773 | * |
||
| 774 | * @param bool $readOnly ReadOnly flag |
||
| 775 | * |
||
| 776 | * @return void |
||
| 777 | */ |
||
| 778 | 2 | public function setReadOnly($readOnly) |
|
| 782 | |||
| 783 | /** |
||
| 784 | * Get the readOnly flag. |
||
| 785 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 786 | * |
||
| 787 | * @return bool|null true if readOnly isset to true or null if not |
||
| 788 | */ |
||
| 789 | 2 | public function getReadOnly() |
|
| 793 | |||
| 794 | /** |
||
| 795 | * set searchable variables |
||
| 796 | * |
||
| 797 | * @param string[] $searchable array of searchable fields |
||
| 798 | * |
||
| 799 | * @return void |
||
| 800 | */ |
||
| 801 | 2 | public function setSearchable($searchable) |
|
| 805 | |||
| 806 | /** |
||
| 807 | * get searchable fields |
||
| 808 | * |
||
| 809 | * @return string[]|null |
||
| 810 | */ |
||
| 811 | 2 | public function getSearchable() |
|
| 818 | } |
||
| 819 |
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.