Complex classes like FunctionDefinition 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 FunctionDefinition, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class FunctionDefinition extends AbstractDefinition |
||
| 41 | { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string $docBlock DocBlock comment of the function |
||
| 45 | */ |
||
| 46 | protected $docBlock; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var boolean $isFinal Is the function final? |
||
| 50 | */ |
||
| 51 | protected $isFinal; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var boolean $isAbstract Is the function abstract? |
||
| 55 | */ |
||
| 56 | protected $isAbstract; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string $visibility Visibility of the method |
||
| 60 | */ |
||
| 61 | protected $visibility; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var boolean $isStatic Is the method static? |
||
| 65 | */ |
||
| 66 | protected $isStatic; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string $name Name of the function |
||
| 70 | */ |
||
| 71 | protected $name; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var \AppserverIo\Doppelgaenger\Entities\Lists\ParameterDefinitionList $parameterDefinitions List of parameter definitions |
||
| 75 | */ |
||
| 76 | protected $parameterDefinitions; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Lists of pointcuts |
||
| 80 | * |
||
| 81 | * @var \AppserverIo\Doppelgaenger\Entities\Lists\PointcutExpressionList $pointcuts |
||
| 82 | */ |
||
| 83 | protected $pointcutExpressions; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $preconditions Preconditions of this function |
||
| 87 | */ |
||
| 88 | protected $preconditions; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var \AppserverIo\Doppelgaenger\Entities\Lists\TypedListList $ancestralPreconditions Preconditions of any parent functions |
||
| 92 | */ |
||
| 93 | protected $ancestralPreconditions; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var boolean $usesOld Does this function use the dgOld keyword? |
||
| 97 | */ |
||
| 98 | protected $usesOld; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var string $body Body of the function |
||
| 102 | */ |
||
| 103 | protected $body; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $postconditions Postconditions of this function |
||
| 107 | */ |
||
| 108 | protected $postconditions; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var \AppserverIo\Doppelgaenger\Entities\Lists\TypedListList $ancestralPostconditions |
||
| 112 | * Postconditions of any parent functions |
||
| 113 | */ |
||
| 114 | protected $ancestralPostconditions; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Name of the structure containing that function |
||
| 118 | * |
||
| 119 | * @var string $structureName |
||
| 120 | */ |
||
| 121 | protected $structureName; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Default constructor |
||
| 125 | */ |
||
| 126 | public function __construct() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Getter method for attribute $docBlock |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getDocBlock() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Getter method for attribute $isFinal |
||
| 157 | * |
||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | public function isFinal() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Getter method for attribute $isAbstract |
||
| 167 | * |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | public function isAbstract() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Getter method for attribute $visibility |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getVisibility() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Getter method for attribute $isStatic |
||
| 187 | * |
||
| 188 | * @return boolean |
||
| 189 | */ |
||
| 190 | public function isStatic() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Getter method for attribute $name |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getName() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Getter method for attribute $parameterDefinitions |
||
| 207 | * |
||
| 208 | * @return ParameterDefinitionList |
||
| 209 | */ |
||
| 210 | public function getParameterDefinitions() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Getter method for attribute $preconditions |
||
| 217 | * |
||
| 218 | * @return AssertionList |
||
| 219 | */ |
||
| 220 | public function getPreconditions() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Getter method for attribute $ancestralPreconditions |
||
| 227 | * |
||
| 228 | * @return null|TypedListList |
||
| 229 | */ |
||
| 230 | public function getAncestralPreconditions() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Getter method for attribute $body |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getBody() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Getter method for attribute $pointcutExpressions |
||
| 247 | * |
||
| 248 | * @return \AppserverIo\Doppelgaenger\Entities\Lists\PointcutExpressionList |
||
| 249 | */ |
||
| 250 | public function getPointcutExpressions() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Getter method for attribute $postconditions |
||
| 257 | * |
||
| 258 | * @return AssertionList |
||
| 259 | */ |
||
| 260 | public function getPostconditions() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Getter method for attribute $ancestralPostconditions |
||
| 267 | * |
||
| 268 | * @return null|TypedListList |
||
| 269 | */ |
||
| 270 | public function getAncestralPostconditions() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Getter method for attribute $structureName |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getStructureName() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Will return all preconditions. Direct as well as ancestral. |
||
| 287 | * |
||
| 288 | * @param boolean $nonPrivateOnly Make this true if you only want conditions which do not have a private context |
||
| 289 | * @param boolean $filterMismatches Do we have to filter condition mismatches due to signature changes |
||
| 290 | * |
||
| 291 | * @return \AppserverIo\Doppelgaenger\Entities\Lists\TypedListList |
||
| 292 | */ |
||
| 293 | public function getAllPreconditions($nonPrivateOnly = false, $filterMismatches = true) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Will return all postconditions. Direct as well as ancestral. |
||
| 328 | * |
||
| 329 | * @param boolean $nonPrivateOnly Make this true if you only want conditions which do not have a private context |
||
| 330 | * |
||
| 331 | * @return \AppserverIo\Doppelgaenger\Entities\Lists\TypedListList |
||
| 332 | */ |
||
| 333 | public function getAllPostconditions($nonPrivateOnly = false) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Will return the header of this function either in calling or in defining manner. |
||
| 357 | * String will stop after the closing ")" bracket, so the string can be used for interfaces as well. |
||
| 358 | * |
||
| 359 | * @param string $type Can be either "call" or "definition" |
||
| 360 | * @param string $suffix Suffix for the function name |
||
| 361 | * @param boolean $showMe Will mark a method as original by extending it with a suffix |
||
| 362 | * @param boolean $forceStatic Will force static call for call type headers |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public function getHeader($type, $suffix = '', $showMe = false, $forceStatic = false) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * This method will check if a certain assertion mismatches the scope of this function. |
||
| 441 | * |
||
| 442 | * @param \AppserverIo\Psr\MetaobjectProtocol\Dbc\Assertions\AssertionInterface $assertion The assertion to check for a possible mismatch |
||
| 443 | * within this function context |
||
| 444 | * |
||
| 445 | * @return boolean |
||
| 446 | */ |
||
| 447 | protected function conditionIsMismatch(AssertionInterface $assertion) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Setter method for attribute $docBlock |
||
| 471 | * |
||
| 472 | * @param string $docBlock Doc block of the structure |
||
| 473 | * |
||
| 474 | * @return null |
||
| 475 | */ |
||
| 476 | public function setDocBlock($docBlock) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Setter method for the $isFinal property |
||
| 483 | * |
||
| 484 | * @param boolean $isFinal If the class is defined final |
||
| 485 | * |
||
| 486 | * @return null |
||
| 487 | */ |
||
| 488 | public function setIsFinal($isFinal) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Setter method for the $isAbstract property |
||
| 495 | * |
||
| 496 | * @param boolean $isAbstract If the class is abstract |
||
| 497 | * |
||
| 498 | * @return null |
||
| 499 | */ |
||
| 500 | public function setIsAbstract($isAbstract) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Setter method for the $isStatic property |
||
| 507 | * |
||
| 508 | * @param boolean $isStatic If the attribute is declared static |
||
| 509 | * |
||
| 510 | * @return null |
||
| 511 | */ |
||
| 512 | public function setIsStatic($isStatic) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Setter method for attribute $name |
||
| 519 | * |
||
| 520 | * @param string $name Name of the structure |
||
| 521 | * |
||
| 522 | * @return null |
||
| 523 | */ |
||
| 524 | public function setName($name) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Setter method for attribute $parameterDefinitions |
||
| 531 | * |
||
| 532 | * @param \AppserverIo\Doppelgaenger\Entities\Lists\ParameterDefinitionList $parameterDefinitions List of parameters |
||
| 533 | * |
||
| 534 | * @return null |
||
| 535 | */ |
||
| 536 | public function setParameterDefinitions(ParameterDefinitionList $parameterDefinitions) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Setter method for attribute $preconditions |
||
| 543 | * |
||
| 544 | * @param \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $preconditions List of preconditions |
||
| 545 | * |
||
| 546 | * @return null |
||
| 547 | */ |
||
| 548 | public function setPreconditions(AssertionList $preconditions) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Setter method for attribute $ancestralPreconditions |
||
| 555 | * |
||
| 556 | * @param \AppserverIo\Doppelgaenger\Entities\Lists\TypedListList $ancestralPreconditions Inherited preconditions |
||
| 557 | * |
||
| 558 | * @return null |
||
| 559 | */ |
||
| 560 | public function setAncestralPreconditions(TypedListList $ancestralPreconditions) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Setter method for attribute $pointcutExpressions |
||
| 567 | * |
||
| 568 | * @param \AppserverIo\Doppelgaenger\Entities\Lists\PointcutExpressionList $pointcutExpressions List of pointcut expressions |
||
| 569 | * |
||
| 570 | * @return null |
||
| 571 | */ |
||
| 572 | public function setPointcutExpressions(PointcutExpressionList $pointcutExpressions) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Getter method for attribute $postconditions |
||
| 579 | * |
||
| 580 | * @param \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $postconditions List of postconditions |
||
| 581 | * |
||
| 582 | * @return null |
||
| 583 | */ |
||
| 584 | public function setPostconditions(AssertionList $postconditions) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Setter method for attribute $ancestralPostconditions |
||
| 591 | * |
||
| 592 | * @param \AppserverIo\Doppelgaenger\Entities\Lists\TypedListList $ancestralPreconditions Inherited preconditions |
||
| 593 | * |
||
| 594 | * @return null |
||
| 595 | */ |
||
| 596 | public function setAncestralPostconditions(TypedListList $ancestralPreconditions) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Stter method for attribute $usesOld |
||
| 603 | * |
||
| 604 | * @param boolean $usesOld Does the function use the "old" keyword |
||
| 605 | * |
||
| 606 | * @return null |
||
| 607 | */ |
||
| 608 | public function setUsesOld($usesOld) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Getter method for attribute $body |
||
| 615 | * |
||
| 616 | * @param string $body Body of the function |
||
| 617 | * |
||
| 618 | * @return null |
||
| 619 | */ |
||
| 620 | public function setBody($body) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Setter method for attribute $structureName |
||
| 627 | * |
||
| 628 | * @param string $structureName Name of the structure containing that function |
||
| 629 | * |
||
| 630 | * @return null |
||
| 631 | */ |
||
| 632 | public function setStructureName($structureName) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Setter method for the $visibility property |
||
| 639 | * |
||
| 640 | * @param string $visibility Visibility of the attribute |
||
| 641 | * |
||
| 642 | * @return null |
||
| 643 | */ |
||
| 644 | public function setVisibility($visibility) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Getter method for attribute $usesOld |
||
| 651 | * |
||
| 652 | * @return boolean |
||
| 653 | */ |
||
| 654 | public function usesOld() |
||
| 658 | } |
||
| 659 |