Complex classes like Relation 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 Relation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class Relation extends Type implements TypeInterface |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * The name of the business object class which this class is related to. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | * |
||
| 60 | * @since 1.0 |
||
| 61 | */ |
||
| 62 | private $relatedClass; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The name of the fields of the business object class by which this class is related by. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | * |
||
| 69 | * @since 1.0 |
||
| 70 | */ |
||
| 71 | private $relatedClassField; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The name of the field from the related business object class which is displayed by the selection widget. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | */ |
||
| 80 | private $relatedClassDisplayField; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * An array of fields to use the values of while rendering related display values via the selection widget. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | * |
||
| 87 | * @since 1.0 |
||
| 88 | */ |
||
| 89 | private $relatedClassHeaderFields = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The name of the business object class on the left of a MANY-TO-MANY relation. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | * |
||
| 96 | * @since 1.0 |
||
| 97 | */ |
||
| 98 | private $relatedClassLeft; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The name of the field from the related business object class on the left of a |
||
| 102 | * MANY-TO-MANY relation which is displayed by the selection widget. |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | * |
||
| 106 | * @since 1.0 |
||
| 107 | */ |
||
| 108 | private $relatedClassLeftDisplayField; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The name of the business object class on the right of a MANY-TO-MANY relation. |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | * |
||
| 115 | * @since 1.0 |
||
| 116 | */ |
||
| 117 | private $relatedClassRight; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The name of the field from the related business object class on the right of a |
||
| 121 | * MANY-TO-MANY relation which is displayed by the selection widget. |
||
| 122 | * |
||
| 123 | * @var string |
||
| 124 | * |
||
| 125 | * @since 1.0 |
||
| 126 | */ |
||
| 127 | private $relatedClassRightDisplayField; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The type of relation ('MANY-TO-ONE' or 'ONE-TO-MANY' or 'ONE-TO-ONE' or 'MANY-TO-MANY'). |
||
| 131 | * |
||
| 132 | * @var string |
||
| 133 | * |
||
| 134 | * @since 1.0 |
||
| 135 | */ |
||
| 136 | private $relationType; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * In the case of MANY-TO-MANY relationship, a lookup object will be required. |
||
| 140 | * |
||
| 141 | * @var \Alpha\Model\Type\RelationLookup |
||
| 142 | * |
||
| 143 | * @since 1.0 |
||
| 144 | */ |
||
| 145 | private $lookup; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * In the case of MANY-TO-MANY relationship, this transient array can be used to hold the IDs of related records. |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | * |
||
| 152 | * @since 2.0 |
||
| 153 | */ |
||
| 154 | private $IDs = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * When building a relation with the TagObject record, set this to the name of the tagged class. |
||
| 158 | * |
||
| 159 | * @var string |
||
| 160 | * |
||
| 161 | * @since 1.0 |
||
| 162 | */ |
||
| 163 | private $taggedClass; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * An array of the allowable relationship types ('MANY-TO-ONE' or 'ONE-TO-MANY' or 'ONE-TO-ONE' or 'MANY-TO-MANY'). |
||
| 167 | * |
||
| 168 | * @var array |
||
| 169 | * |
||
| 170 | * @since 1.0 |
||
| 171 | */ |
||
| 172 | private $allowableRelationTypes = array('MANY-TO-ONE', 'ONE-TO-MANY', 'ONE-TO-ONE', 'MANY-TO-MANY'); |
||
| 173 | |||
| 174 | /** |
||
| 175 | * The ID value of the related object. In the special case of a MANY-TO-MANY |
||
| 176 | * relation, contains the ID of the object on the current, accessing side. Can contain NULL. |
||
| 177 | * |
||
| 178 | * @var mixed |
||
| 179 | * |
||
| 180 | * @since 1.0 |
||
| 181 | */ |
||
| 182 | private $value = null; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * The validation rule for the Relation type. |
||
| 186 | * |
||
| 187 | * @var string |
||
| 188 | * |
||
| 189 | * @since 1.0 |
||
| 190 | */ |
||
| 191 | private $validationRule; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The error message for the Relation type when validation fails. |
||
| 195 | * |
||
| 196 | * @var string |
||
| 197 | * |
||
| 198 | * @since 1.0 |
||
| 199 | */ |
||
| 200 | protected $helper; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * The size of the value for the this Relation. |
||
| 204 | * |
||
| 205 | * @var int |
||
| 206 | * |
||
| 207 | * @since 1.0 |
||
| 208 | */ |
||
| 209 | private $size = 11; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * The absolute maximum size of the value for the this Relation. |
||
| 213 | * |
||
| 214 | * @var int |
||
| 215 | * |
||
| 216 | * @since 1.0 |
||
| 217 | */ |
||
| 218 | const MAX_SIZE = 11; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Constructor. |
||
| 222 | * |
||
| 223 | * @since 1.0 |
||
| 224 | */ |
||
| 225 | public function __construct() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set the name of the business object class that this class is related to. |
||
| 233 | * |
||
| 234 | * @param string $RC |
||
| 235 | * @param string $side Only required for MANY-TO-MANY relations |
||
| 236 | * |
||
| 237 | * @since 1.0 |
||
| 238 | * |
||
| 239 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 240 | */ |
||
| 241 | public function setRelatedClass($RC, $side = '') |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the name of the business object class that this class is related to. |
||
| 264 | * |
||
| 265 | * @param string $side |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | * |
||
| 269 | * @since 1.0 |
||
| 270 | * |
||
| 271 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 272 | */ |
||
| 273 | public function getRelatedClass($side = '') |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Setter for the field of the related class. |
||
| 292 | * |
||
| 293 | * @param string $RCF |
||
| 294 | * |
||
| 295 | * @since 1.0 |
||
| 296 | * |
||
| 297 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 298 | */ |
||
| 299 | public function setRelatedClassField($RCF) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Getter for the field of the related class. |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | * |
||
| 325 | * @since 1.0 |
||
| 326 | */ |
||
| 327 | public function getRelatedClassField() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Setter for ONE-TO-MANY relations, which sets the header fields to |
||
| 334 | * render from the related class. |
||
| 335 | * |
||
| 336 | * @param array $fieldNames |
||
| 337 | * |
||
| 338 | * @since 1.0 |
||
| 339 | */ |
||
| 340 | public function setRelatedClassHeaderFields($fieldNames) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Getter for the selection widget field headings of the related class. |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | * |
||
| 350 | * @since 1.0 |
||
| 351 | */ |
||
| 352 | public function getRelatedClassHeaderFields() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Setter for the display field from the related class. |
||
| 359 | * |
||
| 360 | * @param string $RCDF |
||
| 361 | * @param string $side Only required for MANY-TO-MANY relations |
||
| 362 | * |
||
| 363 | * @since 1.0 |
||
| 364 | * |
||
| 365 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 366 | */ |
||
| 367 | public function setRelatedClassDisplayField($RCDF, $side = '') |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Getter for the display field from the related class. |
||
| 386 | * |
||
| 387 | * @param string $side Only required for MANY-TO-MANY relations |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | * |
||
| 391 | * @since 1.0 |
||
| 392 | * |
||
| 393 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 394 | */ |
||
| 395 | public function getRelatedClassDisplayField($side = '') |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Setter for the relation type. |
||
| 414 | * |
||
| 415 | * @param string $RT |
||
| 416 | * |
||
| 417 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 418 | * @throws \Alpha\Exception\FailedLookupCreateException |
||
| 419 | * |
||
| 420 | * @since 1.0 |
||
| 421 | */ |
||
| 422 | public function setRelationType($RT) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Getter for the relation type. |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | * |
||
| 445 | * @since 1.0 |
||
| 446 | */ |
||
| 447 | public function getRelationType() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Setter for the value ID of this relation. |
||
| 454 | * |
||
| 455 | * @param int $val |
||
| 456 | * |
||
| 457 | * @since 1.0 |
||
| 458 | * |
||
| 459 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 460 | */ |
||
| 461 | public function setValue($val) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Getter for the array of IDs used by MANY-TO-MANY instances. |
||
| 480 | * |
||
| 481 | * @return array |
||
| 482 | * |
||
| 483 | * @since 2.0 |
||
| 484 | */ |
||
| 485 | public function getRelatedIDs() |
||
| 486 | { |
||
| 487 | return $this->IDs; |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Setter for the array of IDs used by MANY-TO-MANY instances. |
||
| 492 | * |
||
| 493 | * @param array $IDs |
||
| 494 | * |
||
| 495 | * @since 2.0 |
||
| 496 | * |
||
| 497 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 498 | */ |
||
| 499 | public function setRelatedIDs($IDs) |
||
| 500 | { |
||
| 501 | if (is_array($IDs)) { |
||
| 502 | $this->IDs = $IDs; |
||
| 503 | } else { |
||
| 504 | throw new IllegalArguementException('An array must be provided to setRelatedIDs()!'); |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Getter for the Relation value. |
||
| 510 | * |
||
| 511 | * @return mixed |
||
| 512 | * |
||
| 513 | * @since 1.0 |
||
| 514 | */ |
||
| 515 | public function getValue() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get the validation rule. |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | * |
||
| 525 | * @since 1.0 |
||
| 526 | */ |
||
| 527 | public function getRule() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Setter to override the default validation rule. |
||
| 534 | * |
||
| 535 | * @param string $rule |
||
| 536 | * |
||
| 537 | * @since 1.0 |
||
| 538 | */ |
||
| 539 | public function setRule($rule) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Getter for the display value of the related class field. In the case of a |
||
| 546 | * MANY-TO-MANY Relation, a comma-seperated sorted list of values is returned. |
||
| 547 | * |
||
| 548 | * @param string $accessingClassName Used to indicate the reading side when accessing from MANY-TO-MANY relation (leave blank for other relation types) |
||
| 549 | * |
||
| 550 | * @return string |
||
| 551 | * |
||
| 552 | * @since 1.0 |
||
| 553 | * |
||
| 554 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 555 | */ |
||
| 556 | public function getRelatedClassDisplayFieldValue($accessingClassName = '') |
||
| 617 | |||
| 618 | /** |
||
| 619 | * For one-to-many and many-to-many relations, get the objects on the other side. |
||
| 620 | * |
||
| 621 | * string $accessingClassName Used to indicate the reading side when accessing from MANY-TO-MANY relation (leave blank for other relation types) |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | * |
||
| 625 | * @since 1.0 |
||
| 626 | * |
||
| 627 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 628 | */ |
||
| 629 | public function getRelatedObjects($accessingClassName = '') |
||
| 679 | |||
| 680 | /** |
||
| 681 | * For one-to-one relations, get the object on the other side. |
||
| 682 | * |
||
| 683 | * @return array |
||
| 684 | * |
||
| 685 | * @since 1.0 |
||
| 686 | * |
||
| 687 | * @throws \Alpha\Model\Type\IllegalArguementException |
||
| 688 | */ |
||
| 689 | public function getRelatedObject() |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Get the allowable size of the Relation in the database field. |
||
| 703 | * |
||
| 704 | * @return int |
||
| 705 | * |
||
| 706 | * @since 1.0 |
||
| 707 | */ |
||
| 708 | public function getSize() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Get the lookup object if available (only on MANY-TO-MANY relations, null otherwise). |
||
| 715 | * |
||
| 716 | * @return RelationLookup |
||
| 717 | * |
||
| 718 | * @since 1.0 |
||
| 719 | */ |
||
| 720 | public function getLookup() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Gets the side ('left' or 'right') of the passed classname on the current Relation object. |
||
| 727 | * |
||
| 728 | * @param string $RecordClassname |
||
| 729 | * |
||
| 730 | * @return string |
||
| 731 | * |
||
| 732 | * @since 1.0 |
||
| 733 | * |
||
| 734 | * @throws \Alpha\Model\Type\IllegalArguementException |
||
| 735 | */ |
||
| 736 | public function getSide($RecordClassname) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Set the taggedClass property to the name of the tagged class when building relations |
||
| 749 | * to the TagObject record. |
||
| 750 | * |
||
| 751 | * @param $taggedClass |
||
| 752 | * |
||
| 753 | * @since 1.0 |
||
| 754 | */ |
||
| 755 | public function setTaggedClass($taggedClass) |
||
| 759 | } |
||
| 760 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.