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 Schema | ||
| 51 | */ | ||
| 52 | protected $additionalProperties; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * @var string[] | ||
| 56 | */ | ||
| 57 | protected $required = array(); | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @var string[] | ||
| 61 | */ | ||
| 62 | protected $searchable = array(); | ||
| 63 | |||
| 64 | /** | ||
| 65 | * @var boolean | ||
| 66 | */ | ||
| 67 | protected $translatable; | ||
| 68 | |||
| 69 | /** | ||
| 70 | * @var array | ||
| 71 | */ | ||
| 72 | protected $refCollection = array(); | ||
| 73 | |||
| 74 | /** | ||
| 75 | * possible event names this collection implements (queue events) | ||
| 76 | * | ||
| 77 | * @var array | ||
| 78 | */ | ||
| 79 | protected $eventNames = array(); | ||
| 80 | |||
| 81 | /** | ||
| 82 | * @var bool | ||
| 83 | */ | ||
| 84 | protected $readOnly = false; | ||
| 85 | |||
| 86 | /** | ||
| 87 | * these are the BSON primitive types. | ||
| 88 | * http://json-schema.org/latest/json-schema-core.html#anchor8 | ||
| 89 | * every type set *not* in this set will be carried over to 'format' | ||
| 90 | * | ||
| 91 | * @var string[] | ||
| 92 | */ | ||
| 93 | protected $primitiveTypes = array( | ||
| 94 | 'array', | ||
| 95 | 'boolean', | ||
| 96 | 'integer', | ||
| 97 | 'number', | ||
| 98 | 'null', | ||
| 99 | 'object', | ||
| 100 | 'string' | ||
| 101 | ); | ||
| 102 | |||
| 103 | /** | ||
| 104 | * known non-primitive types we map to primitives here. | ||
| 105 | * the type itself is set to the format. | ||
| 106 | * | ||
| 107 | * @var string[] | ||
| 108 | */ | ||
| 109 | protected $specialTypeMapping = array( | ||
| 110 | 'extref' => 'string', | ||
| 111 | 'translatable' => 'object' | ||
| 112 | ); | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Build properties | ||
| 116 | */ | ||
| 117 | public function __construct() | ||
| 121 | |||
| 122 | /** | ||
| 123 | * set title | ||
| 124 | * | ||
| 125 | * @param string $title title | ||
| 126 | * | ||
| 127 | * @return void | ||
| 128 | */ | ||
| 129 | public function setTitle($title) | ||
| 133 | |||
| 134 | /** | ||
| 135 | * get title | ||
| 136 | * | ||
| 137 | * @return string | ||
| 138 | */ | ||
| 139 | public function getTitle() | ||
| 143 | |||
| 144 | /** | ||
| 145 | * set description | ||
| 146 | * | ||
| 147 | * @param string $description description | ||
| 148 | * | ||
| 149 | * @return void | ||
| 150 | */ | ||
| 151 | public function setDescription($description) | ||
| 155 | |||
| 156 | /** | ||
| 157 | * get description | ||
| 158 | * | ||
| 159 | * @return string | ||
| 160 | */ | ||
| 161 | public function getDescription() | ||
| 165 | |||
| 166 | /** | ||
| 167 | * set type | ||
| 168 | * | ||
| 169 | * @param string $type type | ||
| 170 | * | ||
| 171 | * @return void | ||
| 172 | */ | ||
| 173 | public function setType($type) | ||
| 194 | |||
| 195 | /** | ||
| 196 | * get type | ||
| 197 | * | ||
| 198 | * @return string type | ||
| 199 | */ | ||
| 200 | public function getType() | ||
| 204 | |||
| 205 | /** | ||
| 206 | * get format | ||
| 207 | * | ||
| 208 | * @return string format | ||
| 209 | */ | ||
| 210 | public function getFormat() | ||
| 214 | |||
| 215 | /** | ||
| 216 | * sets format | ||
| 217 | * | ||
| 218 | * @param string $format format | ||
| 219 | * | ||
| 220 | * @return void | ||
| 221 | */ | ||
| 222 | public function setFormat($format) | ||
| 226 | |||
| 227 | /** | ||
| 228 | * set items | ||
| 229 | * | ||
| 230 | * @param Schema $items items schema | ||
| 231 | * | ||
| 232 | * @return void | ||
| 233 | */ | ||
| 234 | public function setItems($items) | ||
| 238 | |||
| 239 | /** | ||
| 240 | * get items | ||
| 241 | * | ||
| 242 | * @return Schema | ||
| 243 | */ | ||
| 244 | public function getItems() | ||
| 248 | |||
| 249 | /** | ||
| 250 | * add a property | ||
| 251 | * | ||
| 252 | * @param string $name property name | ||
| 253 | * @param Schema $property property | ||
| 254 | * | ||
| 255 | * @return void | ||
| 256 | */ | ||
| 257 | public function addProperty($name, $property) | ||
| 261 | |||
| 262 | /** | ||
| 263 | * removes a property | ||
| 264 | * | ||
| 265 | * @param string $name property name | ||
| 266 | * | ||
| 267 | * @return void | ||
| 268 | */ | ||
| 269 | public function removeProperty($name) | ||
| 275 | |||
| 276 | /** | ||
| 277 | * returns a property | ||
| 278 | * | ||
| 279 | * @param string $name property name | ||
| 280 | * | ||
| 281 | * @return void|Schema property | ||
| 282 | */ | ||
| 283 | public function getProperty($name) | ||
| 287 | |||
| 288 | /** | ||
| 289 | * get properties | ||
| 290 | * | ||
| 291 | * @return ArrayCollection|null | ||
| 292 | */ | ||
| 293 | public function getProperties() | ||
| 301 | |||
| 302 | /** | ||
| 303 | * set additionalProperties on schema | ||
| 304 | * | ||
| 305 | * @param Schema $schema schema to use for additionalProperties type | ||
| 306 | * | ||
| 307 | * @return void | ||
| 308 | */ | ||
| 309 | public function setAdditionalProperties(Schema $schema) | ||
| 313 | |||
| 314 | /** | ||
| 315 | * get addtionalProperties for schema | ||
| 316 | * | ||
| 317 | * @return Schema | ||
| 318 | */ | ||
| 319 | public function getAdditionalProperties() | ||
| 323 | |||
| 324 | /** | ||
| 325 | * set required variables | ||
| 326 | * | ||
| 327 | * @param string[] $required arary of required fields | ||
| 328 | * | ||
| 329 | * @return void | ||
| 330 | */ | ||
| 331 | public function setRequired($required) | ||
| 335 | |||
| 336 | /** | ||
| 337 | * get required fields | ||
| 338 | * | ||
| 339 | * @return string[]|null | ||
| 340 | */ | ||
| 341 | public function getRequired() | ||
| 350 | |||
| 351 | /** | ||
| 352 | * set searchable variables | ||
| 353 | * | ||
| 354 | * @param string[] $searchable arary of searchable fields | ||
| 355 | * | ||
| 356 | * @return void | ||
| 357 | */ | ||
| 358 | public function setSearchable($searchable) | ||
| 362 | |||
| 363 | /** | ||
| 364 | * get searchable fields | ||
| 365 | * | ||
| 366 | * @return string[]|null | ||
| 367 | */ | ||
| 368 | public function getSearchable() | ||
| 377 | |||
| 378 | /** | ||
| 379 | * set translatable flag | ||
| 380 | * | ||
| 381 | * This flag is a local extension to json schema. | ||
| 382 | * | ||
| 383 | * @param boolean $translatable translatable flag | ||
| 384 | * | ||
| 385 | * @return void | ||
| 386 | */ | ||
| 387 | public function setTranslatable($translatable) | ||
| 395 | |||
| 396 | /** | ||
| 397 | * get translatable flag | ||
| 398 | * | ||
| 399 | * @return boolean | ||
| 400 | */ | ||
| 401 | public function isTranslatable() | ||
| 410 | |||
| 411 | /** | ||
| 412 | * set a array of urls that can extref refer to | ||
| 413 | * | ||
| 414 | * @param array $refCollection urls | ||
| 415 | * | ||
| 416 | * @return void | ||
| 417 | */ | ||
| 418 | public function setRefCollection(array $refCollection) | ||
| 422 | |||
| 423 | /** | ||
| 424 | * get a collection of urls that can extref refer to | ||
| 425 | * | ||
| 426 | * @return array | ||
| 427 | */ | ||
| 428 | public function getRefCollection() | ||
| 437 | |||
| 438 | /** | ||
| 439 | * set an array of possible event names | ||
| 440 | * | ||
| 441 | * @param array $eventNames event names | ||
| 442 | * | ||
| 443 | * @return void | ||
| 444 | */ | ||
| 445 | public function setEventNames(array $eventNames) | ||
| 449 | |||
| 450 | /** | ||
| 451 | * get a collection of possible event names | ||
| 452 | * | ||
| 453 | * @return array | ||
| 454 | */ | ||
| 455 | public function getEventNames() | ||
| 464 | |||
| 465 | /** | ||
| 466 | * Set the readOnly flag | ||
| 467 | * | ||
| 468 | * @param bool $readOnly ReadOnly flag | ||
| 469 | * | ||
| 470 | * @return void | ||
| 471 | */ | ||
| 472 | public function setReadOnly($readOnly) | ||
| 476 | |||
| 477 | /** | ||
| 478 | * Get the readOnly flag. | ||
| 479 | * Returns null if the flag is set to false so the serializer will ignore it. | ||
| 480 | * | ||
| 481 | * @return bool|null true if readOnly isset to true or null if not | ||
| 482 | */ | ||
| 483 | public function getReadOnly() | ||
| 487 | } | ||
| 488 |