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 SchemaEnum |
||
| 98 | */ |
||
| 99 | protected $enum; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * these are the BSON primitive types. |
||
| 103 | * http://json-schema.org/latest/json-schema-core.html#anchor8 |
||
| 104 | * every type set *not* in this set will be carried over to 'format' |
||
| 105 | * |
||
| 106 | * @var string[] |
||
| 107 | */ |
||
| 108 | protected $primitiveTypes = array( |
||
| 109 | 'array', |
||
| 110 | 'boolean', |
||
| 111 | 'integer', |
||
| 112 | 'number', |
||
| 113 | 'null', |
||
| 114 | 'object', |
||
| 115 | 'string' |
||
| 116 | ); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * known non-primitive types we map to primitives here. |
||
| 120 | * the type itself is set to the format. |
||
| 121 | * |
||
| 122 | * @var string[] |
||
| 123 | */ |
||
| 124 | protected $specialTypeMapping = [ |
||
| 125 | 'extref' => 'string', |
||
| 126 | 'translatable' => 'object', |
||
| 127 | 'date' => 'string', |
||
| 128 | 'float' => 'number', |
||
| 129 | 'double' => 'number', |
||
| 130 | 'decimal' => 'number' |
||
| 131 | ]; |
||
| 132 | |||
| 133 | protected $formatOverrides = [ |
||
| 134 | 'date' => 'date-time' |
||
| 135 | ]; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Build properties |
||
| 139 | */ |
||
| 140 | public function __construct() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * set title |
||
| 147 | * |
||
| 148 | * @param string $title title |
||
| 149 | * |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function setTitle($title) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * get title |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function getTitle() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * set description |
||
| 169 | * |
||
| 170 | * @param string $description description |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function setDescription($description) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * get description |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getDescription() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * set type |
||
| 191 | * |
||
| 192 | * @param string $type type |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | public function setType($type) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * get type |
||
| 225 | * |
||
| 226 | * @return string type |
||
| 227 | */ |
||
| 228 | public function getType() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * get format |
||
| 235 | * |
||
| 236 | * @return string format |
||
| 237 | */ |
||
| 238 | public function getFormat() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * sets format |
||
| 245 | * |
||
| 246 | * @param string $format format |
||
| 247 | * |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | public function setFormat($format) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * set min length |
||
| 257 | * |
||
| 258 | * @return int length |
||
| 259 | */ |
||
| 260 | public function getMinLength() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * get min length |
||
| 267 | * |
||
| 268 | * @param int $minLength length |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function setMinLength($minLength) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * gets maxlength |
||
| 279 | * |
||
| 280 | * @return int length |
||
| 281 | */ |
||
| 282 | public function getMaxLength() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * set maxlength |
||
| 289 | * |
||
| 290 | * @param int $maxLength length |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | public function setMaxLength($maxLength) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * get Enum |
||
| 301 | * |
||
| 302 | * @return array Enum |
||
|
|
|||
| 303 | */ |
||
| 304 | public function getEnum() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * set Enum |
||
| 311 | * |
||
| 312 | * @param array $enum enum |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | public function setEnum(array $enum) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * set items |
||
| 323 | * |
||
| 324 | * @param Schema $items items schema |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public function setItems($items) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * get items |
||
| 335 | * |
||
| 336 | * @return Schema |
||
| 337 | */ |
||
| 338 | public function getItems() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * add a property |
||
| 345 | * |
||
| 346 | * @param string $name property name |
||
| 347 | * @param Schema $property property |
||
| 348 | * |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | public function addProperty($name, $property) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * removes a property |
||
| 358 | * |
||
| 359 | * @param string $name property name |
||
| 360 | * |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | public function removeProperty($name) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * returns a property |
||
| 372 | * |
||
| 373 | * @param string $name property name |
||
| 374 | * |
||
| 375 | * @return void|Schema property |
||
| 376 | */ |
||
| 377 | public function getProperty($name) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * get properties |
||
| 384 | * |
||
| 385 | * @return ArrayCollection|null |
||
| 386 | */ |
||
| 387 | public function getProperties() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * set additionalProperties on schema |
||
| 398 | * |
||
| 399 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * get addtionalProperties for schema |
||
| 410 | * |
||
| 411 | * @return SchemaAdditionalProperties |
||
| 412 | */ |
||
| 413 | public function getAdditionalProperties() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * set required variables |
||
| 420 | * |
||
| 421 | * @param string[] $required arary of required fields |
||
| 422 | * |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | public function setRequired($required) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * get required fields |
||
| 432 | * |
||
| 433 | * @return string[]|null |
||
| 434 | */ |
||
| 435 | public function getRequired() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * set translatable flag |
||
| 447 | * |
||
| 448 | * This flag is a local extension to json schema. |
||
| 449 | * |
||
| 450 | * @param boolean $translatable translatable flag |
||
| 451 | * |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | public function setTranslatable($translatable) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * get translatable flag |
||
| 465 | * |
||
| 466 | * @return boolean |
||
| 467 | */ |
||
| 468 | public function isTranslatable() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * set a array of urls that can extref refer to |
||
| 480 | * |
||
| 481 | * @param array $refCollection urls |
||
| 482 | * |
||
| 483 | * @return void |
||
| 484 | */ |
||
| 485 | public function setRefCollection(array $refCollection) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * get a collection of urls that can extref refer to |
||
| 492 | * |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | public function getRefCollection() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * set an array of possible event names |
||
| 507 | * |
||
| 508 | * @param array $eventNames event names |
||
| 509 | * |
||
| 510 | * @return void |
||
| 511 | */ |
||
| 512 | public function setEventNames(array $eventNames) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * get a collection of possible event names |
||
| 519 | * |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | public function getEventNames() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Set the readOnly flag |
||
| 534 | * |
||
| 535 | * @param bool $readOnly ReadOnly flag |
||
| 536 | * |
||
| 537 | * @return void |
||
| 538 | */ |
||
| 539 | public function setReadOnly($readOnly) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get the readOnly flag. |
||
| 546 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
| 547 | * |
||
| 548 | * @return bool|null true if readOnly isset to true or null if not |
||
| 549 | */ |
||
| 550 | public function getReadOnly() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * set searchable variables |
||
| 557 | * |
||
| 558 | * @param string[] $searchable arary of searchable fields |
||
| 559 | * |
||
| 560 | * @return void |
||
| 561 | */ |
||
| 562 | public function setSearchable($searchable) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * get searchable fields |
||
| 569 | * |
||
| 570 | * @return string[]|null |
||
| 571 | */ |
||
| 572 | public function getSearchable() |
||
| 581 | } |
||
| 582 |
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.