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 string |
||
| 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 = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | protected $translatable; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $refCollection = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * possible event names this collection implements (queue events) |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $eventNames = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $readOnly = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string[] |
||
| 83 | */ |
||
| 84 | protected $searchable = array(); |
||
| 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 |
||
| 128 | */ |
||
| 129 | protected $documentClass; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * these are the BSON primitive types. |
||
| 133 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 134 | * every type set *not* in this set will be carried over to 'format' |
||
| 135 | * |
||
| 136 | * @var string[] |
||
| 137 | */ |
||
| 138 | protected $primitiveTypes = [ |
||
| 139 | 'array', |
||
| 140 | 'boolean', |
||
| 141 | 'integer', |
||
| 142 | 'number', |
||
| 143 | 'null', |
||
| 144 | 'object', |
||
| 145 | 'string' |
||
| 146 | ]; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * those are types that when they are required, a minimal length |
||
| 150 | * shall be specified in schema |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected $minLengthTypes = [ |
||
| 155 | 'integer', |
||
| 156 | 'number', |
||
| 157 | 'string', |
||
| 158 | 'date', |
||
| 159 | 'extref' |
||
| 160 | ]; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * known non-primitive types we map to primitives here. |
||
| 164 | * the type itself is set to the format. |
||
| 165 | * |
||
| 166 | * @var string[] |
||
| 167 | */ |
||
| 168 | protected $specialTypeMapping = [ |
||
| 169 | 'extref' => 'string', |
||
| 170 | 'translatable' => 'object', |
||
| 171 | 'date' => 'string', |
||
| 172 | 'float' => 'number', |
||
| 173 | 'double' => 'number', |
||
| 174 | 'decimal' => 'number' |
||
| 175 | ]; |
||
| 176 | |||
| 177 | protected $formatOverrides = [ |
||
| 178 | 'date' => 'date-time' |
||
| 179 | ]; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Build properties |
||
| 183 | */ |
||
| 184 | 4 | public function __construct() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * set title |
||
| 191 | * |
||
| 192 | * @param string $title title |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | 4 | public function setTitle($title) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * get title |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 6 | public function getTitle() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * set description |
||
| 213 | * |
||
| 214 | * @param string $description description |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | 2 | public function setDescription($description) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * get description |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 4 | public function getDescription() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * set type |
||
| 235 | * |
||
| 236 | * @param string|array $types types |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 2 | public function setType($types) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * get type |
||
| 278 | * |
||
| 279 | * @return SchemaType type |
||
| 280 | */ |
||
| 281 | 4 | public function getType() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * get MinLengthTypes |
||
| 288 | * |
||
| 289 | * @return array MinLengthTypes |
||
| 290 | */ |
||
| 291 | 4 | public function getMinLengthTypes() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * get format |
||
| 298 | * |
||
| 299 | * @return string format |
||
| 300 | */ |
||
| 301 | 4 | public function getFormat() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * sets format |
||
| 308 | * |
||
| 309 | * @param string $format format |
||
| 310 | * |
||
| 311 | * @return void |
||
| 312 | */ |
||
| 313 | 2 | public function setFormat($format) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * get numeric minimum |
||
| 320 | * |
||
| 321 | * @return float numeric minimum |
||
| 322 | */ |
||
| 323 | 4 | public function getNumericMinimum() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * set numeric minimum |
||
| 330 | * |
||
| 331 | * @param float $numericMinimum numeric mimimum |
||
| 332 | * |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | public function setNumericMinimum($numericMinimum) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * get numeric maximum |
||
| 342 | * |
||
| 343 | * @return float numeric maximum |
||
| 344 | */ |
||
| 345 | 4 | public function getNumericMaximum() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * set numeric maximum |
||
| 352 | * |
||
| 353 | * @param float $numericMaximum maximum |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function setNumericMaximum($numericMaximum) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * set min length |
||
| 364 | * |
||
| 365 | * @return int length |
||
| 366 | */ |
||
| 367 | 4 | public function getMinLength() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * get min length |
||
| 374 | * |
||
| 375 | * @param int $minLength length |
||
| 376 | * |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function setMinLength($minLength) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * gets maxlength |
||
| 386 | * |
||
| 387 | * @return int length |
||
| 388 | */ |
||
| 389 | 4 | public function getMaxLength() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * set maxlength |
||
| 396 | * |
||
| 397 | * @param int $maxLength length |
||
| 398 | * |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | public function setMaxLength($maxLength) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * set min Items |
||
| 408 | * |
||
| 409 | * @return int Items |
||
| 410 | */ |
||
| 411 | 4 | public function getMinItems() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * get min Items |
||
| 418 | * |
||
| 419 | * @param int $minItems length |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | public function setMinItems($minItems) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * gets maxItems |
||
| 430 | * |
||
| 431 | * @return int Items |
||
| 432 | */ |
||
| 433 | 4 | public function getMaxItems() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * set maxItems |
||
| 440 | * |
||
| 441 | * @param int $maxItems Items |
||
| 442 | * |
||
| 443 | * @return void |
||
| 444 | */ |
||
| 445 | public function setMaxItems($maxItems) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * get Enum |
||
| 452 | * |
||
| 453 | * @return array Enum |
||
| 454 | */ |
||
| 455 | 4 | public function getEnum() |
|
| 459 | |||
| 460 | /** |
||
| 461 | * set Enum |
||
| 462 | * |
||
| 463 | * @param array $enum enum |
||
| 464 | * |
||
| 465 | * @return void |
||
| 466 | */ |
||
| 467 | public function setEnum(array $enum) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * get regex pattern |
||
| 474 | * |
||
| 475 | * @return string pattern |
||
| 476 | */ |
||
| 477 | 4 | public function getRegexPattern() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * set regex pattern |
||
| 484 | * |
||
| 485 | * @param string $regexPattern regex pattern |
||
| 486 | * |
||
| 487 | * @return void |
||
| 488 | */ |
||
| 489 | public function setRegexPattern($regexPattern) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * get DocumentClass |
||
| 496 | * |
||
| 497 | * @return string DocumentClass |
||
| 498 | */ |
||
| 499 | 4 | public function getDocumentClass() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * set DocumentClass |
||
| 506 | * |
||
| 507 | * @param string $documentClass documentClass |
||
| 508 | * |
||
| 509 | * @return void |
||
| 510 | */ |
||
| 511 | 2 | public function setDocumentClass($documentClass) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * set items |
||
| 518 | * |
||
| 519 | * @param Schema $items items schema |
||
| 520 | * |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | 2 | public function setItems($items) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * get items |
||
| 530 | * |
||
| 531 | * @return Schema |
||
| 532 | */ |
||
| 533 | 4 | public function getItems() |
|
| 537 | |||
| 538 | /** |
||
| 539 | * add a property |
||
| 540 | * |
||
| 541 | * @param string $name property name |
||
| 542 | * @param Schema $property property |
||
| 543 | * |
||
| 544 | * @return void |
||
| 545 | */ |
||
| 546 | 2 | public function addProperty($name, $property) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * removes a property |
||
| 553 | * |
||
| 554 | * @param string $name property name |
||
| 555 | * |
||
| 556 | * @return void |
||
| 557 | */ |
||
| 558 | 2 | public function removeProperty($name) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * returns a property |
||
| 567 | * |
||
| 568 | * @param string $name property name |
||
| 569 | * |
||
| 570 | * @return void|Schema property |
||
| 571 | */ |
||
| 572 | public function getProperty($name) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * get properties |
||
| 579 | * |
||
| 580 | * @return ArrayCollection|null |
||
| 581 | */ |
||
| 582 | 4 | public function getProperties() |
|
| 590 | |||
| 591 | /** |
||
| 592 | * set additionalProperties on schema |
||
| 593 | * |
||
| 594 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 595 | * |
||
| 596 | * @return void |
||
| 597 | */ |
||
| 598 | 2 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * get addtionalProperties for schema |
||
| 605 | * |
||
| 606 | * @return SchemaAdditionalProperties |
||
| 607 | */ |
||
| 608 | 4 | public function getAdditionalProperties() |
|
| 612 | |||
| 613 | /** |
||
| 614 | * set required variables |
||
| 615 | * |
||
| 616 | * @param string[] $required array of required fields |
||
| 617 | * |
||
| 618 | * @return void |
||
| 619 | */ |
||
| 620 | 2 | public function setRequired(array $required) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * get required fields |
||
| 628 | * |
||
| 629 | * @return string[]|null |
||
| 630 | */ |
||
| 631 | 4 | public function getRequired() |
|
| 640 | |||
| 641 | /** |
||
| 642 | * set translatable flag |
||
| 643 | * |
||
| 644 | * This flag is a local extension to json schema. |
||
| 645 | * |
||
| 646 | * @param boolean $translatable translatable flag |
||
| 647 | * |
||
| 648 | * @return void |
||
| 649 | */ |
||
| 650 | public function setTranslatable($translatable) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * get translatable flag |
||
| 661 | * |
||
| 662 | * @return boolean |
||
| 663 | */ |
||
| 664 | public function isTranslatable() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * set a array of urls that can extref refer to |
||
| 676 | * |
||
| 677 | * @param array $refCollection urls |
||
| 678 | * |
||
| 679 | * @return void |
||
| 680 | */ |
||
| 681 | public function setRefCollection(array $refCollection) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * get a collection of urls that can extref refer to |
||
| 688 | * |
||
| 689 | * @return array |
||
| 690 | */ |
||
| 691 | 4 | public function getRefCollection() |
|
| 700 | |||
| 701 | /** |
||
| 702 | * set an array of possible event names |
||
| 703 | * |
||
| 704 | * @param array $eventNames event names |
||
| 705 | * |
||
| 706 | * @return void |
||
| 707 | */ |
||
| 708 | 2 | public function setEventNames(array $eventNames) |
|
| 712 | |||
| 713 | /** |
||
| 714 | * get a collection of possible event names |
||
| 715 | * |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | 4 | public function getEventNames() |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Set the readOnly flag |
||
| 730 | * |
||
| 731 | * @param bool $readOnly ReadOnly flag |
||
| 732 | * |
||
| 733 | * @return void |
||
| 734 | */ |
||
| 735 | 2 | public function setReadOnly($readOnly) |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Get the readOnly flag. |
||
| 742 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 743 | * |
||
| 744 | * @return bool|null true if readOnly isset to true or null if not |
||
| 745 | */ |
||
| 746 | 4 | public function getReadOnly() |
|
| 750 | |||
| 751 | /** |
||
| 752 | * set searchable variables |
||
| 753 | * |
||
| 754 | * @param string[] $searchable arary of searchable fields |
||
| 755 | * |
||
| 756 | * @return void |
||
| 757 | */ |
||
| 758 | 2 | public function setSearchable($searchable) |
|
| 762 | |||
| 763 | /** |
||
| 764 | * get searchable fields |
||
| 765 | * |
||
| 766 | * @return string[]|null |
||
| 767 | */ |
||
| 768 | 4 | public function getSearchable() |
|
| 777 | } |
||
| 778 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..