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 | * these are the BSON primitive types. |
||
| 128 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 129 | * every type set *not* in this set will be carried over to 'format' |
||
| 130 | * |
||
| 131 | * @var string[] |
||
| 132 | */ |
||
| 133 | protected $primitiveTypes = array( |
||
| 134 | 'array', |
||
| 135 | 'boolean', |
||
| 136 | 'integer', |
||
| 137 | 'number', |
||
| 138 | 'null', |
||
| 139 | 'object', |
||
| 140 | 'string' |
||
| 141 | ); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * known non-primitive types we map to primitives here. |
||
| 145 | * the type itself is set to the format. |
||
| 146 | * |
||
| 147 | * @var string[] |
||
| 148 | */ |
||
| 149 | protected $specialTypeMapping = [ |
||
| 150 | 'extref' => 'string', |
||
| 151 | 'translatable' => 'object', |
||
| 152 | 'date' => 'string', |
||
| 153 | 'float' => 'number', |
||
| 154 | 'double' => 'number', |
||
| 155 | 'decimal' => 'number' |
||
| 156 | ]; |
||
| 157 | |||
| 158 | protected $formatOverrides = [ |
||
| 159 | 'date' => 'date-time' |
||
| 160 | ]; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Build properties |
||
| 164 | */ |
||
| 165 | public function __construct() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * set title |
||
| 172 | * |
||
| 173 | * @param string $title title |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function setTitle($title) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * get title |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getTitle() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * set description |
||
| 194 | * |
||
| 195 | * @param string $description description |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | public function setDescription($description) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * get description |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getDescription() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * set type |
||
| 216 | * |
||
| 217 | * @param string $type type |
||
| 218 | * |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | public function setType($type) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * get type |
||
| 250 | * |
||
| 251 | * @return string type |
||
| 252 | */ |
||
| 253 | public function getType() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * get format |
||
| 260 | * |
||
| 261 | * @return string format |
||
| 262 | */ |
||
| 263 | public function getFormat() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * sets format |
||
| 270 | * |
||
| 271 | * @param string $format format |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | public function setFormat($format) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * get numeric minimum |
||
| 282 | * |
||
| 283 | * @return float numeric minimum |
||
| 284 | */ |
||
| 285 | public function getNumericMinimum() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * set numeric minimum |
||
| 292 | * |
||
| 293 | * @param float $numericMinimum numeric mimimum |
||
| 294 | * |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | public function setNumericMinimum($numericMinimum) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * get numeric maximum |
||
| 304 | * |
||
| 305 | * @return float numeric maximum |
||
| 306 | */ |
||
| 307 | public function getNumericMaximum() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * set numeric maximum |
||
| 314 | * |
||
| 315 | * @param float $numericMaximum maximum |
||
| 316 | * |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function setNumericMaximum($numericMaximum) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * set min length |
||
| 326 | * |
||
| 327 | * @return int length |
||
| 328 | */ |
||
| 329 | public function getMinLength() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * get min length |
||
| 336 | * |
||
| 337 | * @param int $minLength length |
||
| 338 | * |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | public function setMinLength($minLength) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * gets maxlength |
||
| 348 | * |
||
| 349 | * @return int length |
||
| 350 | */ |
||
| 351 | public function getMaxLength() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * set maxlength |
||
| 358 | * |
||
| 359 | * @param int $maxLength length |
||
| 360 | * |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | public function setMaxLength($maxLength) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * set min Items |
||
| 370 | * |
||
| 371 | * @return int Items |
||
| 372 | */ |
||
| 373 | public function getMinItems() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * get min Items |
||
| 380 | * |
||
| 381 | * @param int $minItems length |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | */ |
||
| 385 | public function setMinItems($minItems) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * gets maxItems |
||
| 392 | * |
||
| 393 | * @return int Items |
||
| 394 | */ |
||
| 395 | public function getMaxItems() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * set maxItems |
||
| 402 | * |
||
| 403 | * @param int $maxItems Items |
||
| 404 | * |
||
| 405 | * @return void |
||
| 406 | */ |
||
| 407 | public function setMaxItems($maxItems) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * get Enum |
||
| 414 | * |
||
| 415 | * @return array Enum |
||
|
|
|||
| 416 | */ |
||
| 417 | public function getEnum() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * set Enum |
||
| 424 | * |
||
| 425 | * @param array $enum enum |
||
| 426 | * |
||
| 427 | * @return void |
||
| 428 | */ |
||
| 429 | public function setEnum(array $enum) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * get regex pattern |
||
| 436 | * |
||
| 437 | * @return string pattern |
||
| 438 | */ |
||
| 439 | public function getRegexPattern() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * set regex pattern |
||
| 446 | * |
||
| 447 | * @param string $regexPattern regex pattern |
||
| 448 | * |
||
| 449 | * @return void |
||
| 450 | */ |
||
| 451 | public function setRegexPattern($regexPattern) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * set items |
||
| 458 | * |
||
| 459 | * @param Schema $items items schema |
||
| 460 | * |
||
| 461 | * @return void |
||
| 462 | */ |
||
| 463 | public function setItems($items) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * get items |
||
| 470 | * |
||
| 471 | * @return Schema |
||
| 472 | */ |
||
| 473 | public function getItems() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * add a property |
||
| 480 | * |
||
| 481 | * @param string $name property name |
||
| 482 | * @param Schema $property property |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | public function addProperty($name, $property) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * removes a property |
||
| 493 | * |
||
| 494 | * @param string $name property name |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | public function removeProperty($name) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * returns a property |
||
| 507 | * |
||
| 508 | * @param string $name property name |
||
| 509 | * |
||
| 510 | * @return void|Schema property |
||
| 511 | */ |
||
| 512 | public function getProperty($name) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * get properties |
||
| 519 | * |
||
| 520 | * @return ArrayCollection|null |
||
| 521 | */ |
||
| 522 | public function getProperties() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * set additionalProperties on schema |
||
| 533 | * |
||
| 534 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 535 | * |
||
| 536 | * @return void |
||
| 537 | */ |
||
| 538 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * get addtionalProperties for schema |
||
| 545 | * |
||
| 546 | * @return SchemaAdditionalProperties |
||
| 547 | */ |
||
| 548 | public function getAdditionalProperties() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * set required variables |
||
| 555 | * |
||
| 556 | * @param string[] $required arary of required fields |
||
| 557 | * |
||
| 558 | * @return void |
||
| 559 | */ |
||
| 560 | public function setRequired($required) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * get required fields |
||
| 567 | * |
||
| 568 | * @return string[]|null |
||
| 569 | */ |
||
| 570 | public function getRequired() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * set translatable flag |
||
| 582 | * |
||
| 583 | * This flag is a local extension to json schema. |
||
| 584 | * |
||
| 585 | * @param boolean $translatable translatable flag |
||
| 586 | * |
||
| 587 | * @return void |
||
| 588 | */ |
||
| 589 | public function setTranslatable($translatable) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * get translatable flag |
||
| 600 | * |
||
| 601 | * @return boolean |
||
| 602 | */ |
||
| 603 | public function isTranslatable() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * set a array of urls that can extref refer to |
||
| 615 | * |
||
| 616 | * @param array $refCollection urls |
||
| 617 | * |
||
| 618 | * @return void |
||
| 619 | */ |
||
| 620 | public function setRefCollection(array $refCollection) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * get a collection of urls that can extref refer to |
||
| 627 | * |
||
| 628 | * @return array |
||
| 629 | */ |
||
| 630 | public function getRefCollection() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * set an array of possible event names |
||
| 642 | * |
||
| 643 | * @param array $eventNames event names |
||
| 644 | * |
||
| 645 | * @return void |
||
| 646 | */ |
||
| 647 | public function setEventNames(array $eventNames) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * get a collection of possible event names |
||
| 654 | * |
||
| 655 | * @return array |
||
| 656 | */ |
||
| 657 | public function getEventNames() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Set the readOnly flag |
||
| 669 | * |
||
| 670 | * @param bool $readOnly ReadOnly flag |
||
| 671 | * |
||
| 672 | * @return void |
||
| 673 | */ |
||
| 674 | public function setReadOnly($readOnly) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Get the readOnly flag. |
||
| 681 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 682 | * |
||
| 683 | * @return bool|null true if readOnly isset to true or null if not |
||
| 684 | */ |
||
| 685 | public function getReadOnly() |
||
| 689 | |||
| 690 | /** |
||
| 691 | * set searchable variables |
||
| 692 | * |
||
| 693 | * @param string[] $searchable arary of searchable fields |
||
| 694 | * |
||
| 695 | * @return void |
||
| 696 | */ |
||
| 697 | public function setSearchable($searchable) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * get searchable fields |
||
| 704 | * |
||
| 705 | * @return string[]|null |
||
| 706 | */ |
||
| 707 | public function getSearchable() |
||
| 716 | } |
||
| 717 |
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.