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 | * these are the BSON primitive types. |
||
| 148 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 149 | * every type set *not* in this set will be carried over to 'format' |
||
| 150 | * |
||
| 151 | * @var string[] |
||
| 152 | */ |
||
| 153 | protected $primitiveTypes = [ |
||
| 154 | 'array', |
||
| 155 | 'boolean', |
||
| 156 | 'integer', |
||
| 157 | 'number', |
||
| 158 | 'null', |
||
| 159 | 'object', |
||
| 160 | 'string' |
||
| 161 | ]; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * those are types that when they are required, a minimal length |
||
| 165 | * shall be specified in schema (or allow null if not required; that will lead |
||
| 166 | * to the inclusion of "null" in the "type" property array) |
||
| 167 | * |
||
| 168 | * @var array |
||
| 169 | */ |
||
| 170 | protected $minLengthTypes = [ |
||
| 171 | 'integer', |
||
| 172 | 'number', |
||
| 173 | 'float', |
||
| 174 | 'double', |
||
| 175 | 'decimal', |
||
| 176 | 'string', |
||
| 177 | 'date', |
||
| 178 | 'extref' |
||
| 179 | ]; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * known non-primitive types we map to primitives here. |
||
| 183 | * the type itself is set to the format. |
||
| 184 | * |
||
| 185 | * @var string[] |
||
| 186 | */ |
||
| 187 | protected $specialTypeMapping = [ |
||
| 188 | 'extref' => 'string', |
||
| 189 | 'translatable' => 'object', |
||
| 190 | 'date' => 'string', |
||
| 191 | 'float' => 'number', |
||
| 192 | 'double' => 'number', |
||
| 193 | 'decimal' => 'number' |
||
| 194 | ]; |
||
| 195 | |||
| 196 | protected $formatOverrides = [ |
||
| 197 | 'date' => 'date-time' |
||
| 198 | ]; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Build properties |
||
| 202 | */ |
||
| 203 | 4 | public function __construct() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * set title |
||
| 210 | * |
||
| 211 | * @param string $title title |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | 4 | public function setTitle($title) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * get title |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | 4 | public function getTitle() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * set description |
||
| 232 | * |
||
| 233 | * @param string $description description |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | 2 | public function setDescription($description) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * get description |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | 2 | public function getDescription() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * set type |
||
| 254 | * |
||
| 255 | * @param string|array $types types |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | 2 | public function setType($types) |
|
| 260 | { |
||
| 261 | 2 | if (!is_array($types)) { |
|
| 262 | 2 | $types = [$types]; |
|
| 263 | 1 | } |
|
| 264 | |||
| 265 | 2 | $typesToSet = []; |
|
| 266 | 2 | foreach ($types as $type) { |
|
| 267 | 2 | if ($type === 'int') { |
|
| 268 | $type = 'integer'; |
||
| 269 | } |
||
| 270 | 2 | if ($type === 'hash') { |
|
| 271 | $type = 'object'; |
||
| 272 | } |
||
| 273 | |||
| 274 | // handle non-primitive types |
||
| 275 | 2 | if (!in_array($type, $this->primitiveTypes)) { |
|
| 276 | 2 | $setType = 'string'; |
|
| 277 | 2 | if (isset($this->specialTypeMapping[$type])) { |
|
| 278 | $setType = $this->specialTypeMapping[$type]; |
||
| 279 | } |
||
| 280 | 2 | $typesToSet[] = $setType; |
|
| 281 | |||
| 282 | 2 | if (isset($this->formatOverrides[$type])) { |
|
| 283 | $type = $this->formatOverrides[$type]; |
||
| 284 | } |
||
| 285 | |||
| 286 | 2 | $this->setFormat($type); |
|
| 287 | 1 | } else { |
|
| 288 | 2 | $typesToSet[] = $type; |
|
| 289 | } |
||
| 290 | 1 | } |
|
| 291 | |||
| 292 | 2 | $this->type = new SchemaType($typesToSet); |
|
| 293 | 2 | } |
|
| 294 | |||
| 295 | /** |
||
| 296 | * get type |
||
| 297 | * |
||
| 298 | * @return SchemaType type |
||
| 299 | */ |
||
| 300 | 2 | public function getType() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * get MinLengthTypes |
||
| 307 | * |
||
| 308 | * @return array MinLengthTypes |
||
| 309 | */ |
||
| 310 | 2 | public function getMinLengthTypes() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * get format |
||
| 317 | * |
||
| 318 | * @return string format |
||
| 319 | */ |
||
| 320 | 2 | public function getFormat() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * sets format |
||
| 327 | * |
||
| 328 | * @param string $format format |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | 2 | public function setFormat($format) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * get numeric minimum |
||
| 339 | * |
||
| 340 | * @return float numeric minimum |
||
| 341 | */ |
||
| 342 | 2 | public function getNumericMinimum() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * set numeric minimum |
||
| 349 | * |
||
| 350 | * @param float $numericMinimum numeric mimimum |
||
| 351 | * |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | public function setNumericMinimum($numericMinimum) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * get numeric maximum |
||
| 361 | * |
||
| 362 | * @return float numeric maximum |
||
| 363 | */ |
||
| 364 | 2 | public function getNumericMaximum() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * set numeric maximum |
||
| 371 | * |
||
| 372 | * @param float $numericMaximum maximum |
||
| 373 | * |
||
| 374 | * @return void |
||
| 375 | */ |
||
| 376 | public function setNumericMaximum($numericMaximum) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * set min length |
||
| 383 | * |
||
| 384 | * @return int length |
||
| 385 | */ |
||
| 386 | 2 | public function getMinLength() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * get min length |
||
| 393 | * |
||
| 394 | * @param int $minLength length |
||
| 395 | * |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | public function setMinLength($minLength) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * gets maxlength |
||
| 405 | * |
||
| 406 | * @return int length |
||
| 407 | */ |
||
| 408 | 2 | public function getMaxLength() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * set maxlength |
||
| 415 | * |
||
| 416 | * @param int $maxLength length |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | public function setMaxLength($maxLength) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * set min Items |
||
| 427 | * |
||
| 428 | * @return int Items |
||
| 429 | */ |
||
| 430 | 2 | public function getMinItems() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * get min Items |
||
| 437 | * |
||
| 438 | * @param int $minItems length |
||
| 439 | * |
||
| 440 | * @return void |
||
| 441 | */ |
||
| 442 | public function setMinItems($minItems) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * gets maxItems |
||
| 449 | * |
||
| 450 | * @return int Items |
||
| 451 | */ |
||
| 452 | 2 | public function getMaxItems() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * set maxItems |
||
| 459 | * |
||
| 460 | * @param int $maxItems Items |
||
| 461 | * |
||
| 462 | * @return void |
||
| 463 | */ |
||
| 464 | public function setMaxItems($maxItems) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * get Enum |
||
| 471 | * |
||
| 472 | * @return array Enum |
||
|
|
|||
| 473 | */ |
||
| 474 | 2 | public function getEnum() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * set Enum |
||
| 481 | * |
||
| 482 | * @param array $enum enum |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | public function setEnum(array $enum) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * get regex pattern |
||
| 493 | * |
||
| 494 | * @return string pattern |
||
| 495 | */ |
||
| 496 | 2 | public function getRegexPattern() |
|
| 500 | |||
| 501 | /** |
||
| 502 | * set regex pattern |
||
| 503 | * |
||
| 504 | * @param string $regexPattern regex pattern |
||
| 505 | * |
||
| 506 | * @return void |
||
| 507 | */ |
||
| 508 | public function setRegexPattern($regexPattern) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * get DocumentClass |
||
| 515 | * |
||
| 516 | * @return string DocumentClass |
||
| 517 | */ |
||
| 518 | 2 | public function getDocumentClass() |
|
| 522 | |||
| 523 | /** |
||
| 524 | * set DocumentClass |
||
| 525 | * |
||
| 526 | * @param string $documentClass documentClass |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | */ |
||
| 530 | 2 | public function setDocumentClass($documentClass) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * get Constraints |
||
| 537 | * |
||
| 538 | * @return mixed Constraints |
||
| 539 | */ |
||
| 540 | 2 | public function getConstraints() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * set Constraints |
||
| 547 | * |
||
| 548 | * @param mixed $constraints constraints |
||
| 549 | * |
||
| 550 | * @return void |
||
| 551 | */ |
||
| 552 | public function setConstraints($constraints) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * add a constraint |
||
| 559 | * |
||
| 560 | * @param string $name constraint name |
||
| 561 | * |
||
| 562 | * @return void |
||
| 563 | */ |
||
| 564 | public function addConstraint($name) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * set items |
||
| 571 | * |
||
| 572 | * @param Schema $items items schema |
||
| 573 | * |
||
| 574 | * @return void |
||
| 575 | */ |
||
| 576 | 2 | public function setItems($items) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * get items |
||
| 583 | * |
||
| 584 | * @return Schema |
||
| 585 | */ |
||
| 586 | 2 | public function getItems() |
|
| 590 | |||
| 591 | /** |
||
| 592 | * add a property |
||
| 593 | * |
||
| 594 | * @param string $name property name |
||
| 595 | * @param Schema $property property |
||
| 596 | * |
||
| 597 | * @return void |
||
| 598 | */ |
||
| 599 | 2 | public function addProperty($name, $property) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * removes a property |
||
| 606 | * |
||
| 607 | * @param string $name property name |
||
| 608 | * |
||
| 609 | * @return void |
||
| 610 | */ |
||
| 611 | 2 | public function removeProperty($name) |
|
| 617 | |||
| 618 | /** |
||
| 619 | * returns a property |
||
| 620 | * |
||
| 621 | * @param string $name property name |
||
| 622 | * |
||
| 623 | * @return void|Schema property |
||
| 624 | */ |
||
| 625 | public function getProperty($name) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * get properties |
||
| 632 | * |
||
| 633 | * @return ArrayCollection|null |
||
| 634 | */ |
||
| 635 | 2 | public function getProperties() |
|
| 643 | |||
| 644 | /** |
||
| 645 | * set additionalProperties on schema |
||
| 646 | * |
||
| 647 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 648 | * |
||
| 649 | * @return void |
||
| 650 | */ |
||
| 651 | 2 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * get addtionalProperties for schema |
||
| 658 | * |
||
| 659 | * @return SchemaAdditionalProperties |
||
| 660 | */ |
||
| 661 | 2 | public function getAdditionalProperties() |
|
| 665 | |||
| 666 | /** |
||
| 667 | * set required variables |
||
| 668 | * |
||
| 669 | * @param string[] $required array of required fields |
||
| 670 | * |
||
| 671 | * @return void |
||
| 672 | */ |
||
| 673 | 2 | public function setRequired(array $required) |
|
| 678 | |||
| 679 | /** |
||
| 680 | * get required fields |
||
| 681 | * |
||
| 682 | * @return string[]|null |
||
| 683 | */ |
||
| 684 | 2 | public function getRequired() |
|
| 693 | |||
| 694 | /** |
||
| 695 | * set translatable flag |
||
| 696 | * |
||
| 697 | * This flag is a local extension to json schema. |
||
| 698 | * |
||
| 699 | * @param boolean $translatable translatable flag |
||
| 700 | * |
||
| 701 | * @return void |
||
| 702 | */ |
||
| 703 | public function setTranslatable($translatable) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * get translatable flag |
||
| 714 | * |
||
| 715 | * @return boolean |
||
| 716 | */ |
||
| 717 | public function isTranslatable() |
||
| 726 | |||
| 727 | /** |
||
| 728 | * set a array of urls that can extref refer to |
||
| 729 | * |
||
| 730 | * @param array $refCollection urls |
||
| 731 | * |
||
| 732 | * @return void |
||
| 733 | */ |
||
| 734 | public function setRefCollection(array $refCollection) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * get a collection of urls that can extref refer to |
||
| 741 | * |
||
| 742 | * @return array |
||
| 743 | */ |
||
| 744 | 2 | public function getRefCollection() |
|
| 753 | |||
| 754 | /** |
||
| 755 | * set an array of possible event names |
||
| 756 | * |
||
| 757 | * @param array $eventNames event names |
||
| 758 | * |
||
| 759 | * @return void |
||
| 760 | */ |
||
| 761 | 2 | public function setEventNames(array $eventNames) |
|
| 765 | |||
| 766 | /** |
||
| 767 | * get a collection of possible event names |
||
| 768 | * |
||
| 769 | * @return array |
||
| 770 | */ |
||
| 771 | 2 | public function getEventNames() |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Set the readOnly flag |
||
| 783 | * |
||
| 784 | * @param bool $readOnly ReadOnly flag |
||
| 785 | * |
||
| 786 | * @return void |
||
| 787 | */ |
||
| 788 | 2 | public function setReadOnly($readOnly) |
|
| 792 | |||
| 793 | /** |
||
| 794 | * Get the readOnly flag. |
||
| 795 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 796 | * |
||
| 797 | * @return bool|null true if readOnly isset to true or null if not |
||
| 798 | */ |
||
| 799 | 2 | public function getReadOnly() |
|
| 803 | |||
| 804 | /** |
||
| 805 | * get RecordOriginModifiable |
||
| 806 | * |
||
| 807 | * @return boolean RecordOriginModifiable |
||
| 808 | */ |
||
| 809 | 2 | public function isRecordOriginModifiable() |
|
| 813 | |||
| 814 | /** |
||
| 815 | * set RecordOriginModifiable |
||
| 816 | * |
||
| 817 | * @param boolean $recordOriginModifiable recordOriginModifiable |
||
| 818 | * |
||
| 819 | * @return void |
||
| 820 | */ |
||
| 821 | 2 | public function setRecordOriginModifiable($recordOriginModifiable) |
|
| 825 | |||
| 826 | /** |
||
| 827 | * get RecordOriginException |
||
| 828 | * |
||
| 829 | * @return boolean RecordOriginException |
||
| 830 | */ |
||
| 831 | 2 | public function isRecordOriginException() |
|
| 835 | |||
| 836 | /** |
||
| 837 | * set RecordOriginException |
||
| 838 | * |
||
| 839 | * @param boolean $recordOriginException recordOriginException |
||
| 840 | * |
||
| 841 | * @return void |
||
| 842 | */ |
||
| 843 | public function setRecordOriginException($recordOriginException) |
||
| 847 | |||
| 848 | /** |
||
| 849 | * set searchable variables |
||
| 850 | * |
||
| 851 | * @param string[] $searchable array of searchable fields |
||
| 852 | * |
||
| 853 | * @return void |
||
| 854 | */ |
||
| 855 | 2 | public function setSearchable($searchable) |
|
| 859 | |||
| 860 | /** |
||
| 861 | * get searchable fields |
||
| 862 | * |
||
| 863 | * @return string[]|null |
||
| 864 | */ |
||
| 865 | 2 | public function getSearchable() |
|
| 872 | } |
||
| 873 |
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.