Complex classes like ProjectBase 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 ProjectBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ProjectBase extends Model |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | public static $sleepable = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $tableName = 'project'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $modelName = 'Project'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $data = array( |
||
| 36 | 'id' => null, |
||
| 37 | 'title' => null, |
||
| 38 | 'reference' => null, |
||
| 39 | 'branch' => null, |
||
| 40 | 'ssh_private_key' => null, |
||
| 41 | 'type' => null, |
||
| 42 | 'access_information' => null, |
||
| 43 | 'last_commit' => null, |
||
| 44 | 'build_config' => null, |
||
| 45 | 'ssh_public_key' => null, |
||
| 46 | 'allow_public_status' => null, |
||
| 47 | 'archived' => null, |
||
| 48 | 'group_id' => null, |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $getters = array( |
||
| 55 | // Direct property getters: |
||
| 56 | 'id' => 'getId', |
||
| 57 | 'title' => 'getTitle', |
||
| 58 | 'reference' => 'getReference', |
||
| 59 | 'branch' => 'getBranch', |
||
| 60 | 'ssh_private_key' => 'getSshPrivateKey', |
||
| 61 | 'type' => 'getType', |
||
| 62 | 'access_information' => 'getAccessInformation', |
||
| 63 | 'last_commit' => 'getLastCommit', |
||
| 64 | 'build_config' => 'getBuildConfig', |
||
| 65 | 'ssh_public_key' => 'getSshPublicKey', |
||
| 66 | 'allow_public_status' => 'getAllowPublicStatus', |
||
| 67 | 'archived' => 'getArchived', |
||
| 68 | 'group_id' => 'getGroupId', |
||
| 69 | |||
| 70 | // Foreign key getters: |
||
| 71 | 'Group' => 'getGroup', |
||
| 72 | ); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $setters = array( |
||
| 78 | // Direct property setters: |
||
| 79 | 'id' => 'setId', |
||
| 80 | 'title' => 'setTitle', |
||
| 81 | 'reference' => 'setReference', |
||
| 82 | 'branch' => 'setBranch', |
||
| 83 | 'ssh_private_key' => 'setSshPrivateKey', |
||
| 84 | 'type' => 'setType', |
||
| 85 | 'access_information' => 'setAccessInformation', |
||
| 86 | 'last_commit' => 'setLastCommit', |
||
| 87 | 'build_config' => 'setBuildConfig', |
||
| 88 | 'ssh_public_key' => 'setSshPublicKey', |
||
| 89 | 'allow_public_status' => 'setAllowPublicStatus', |
||
| 90 | 'archived' => 'setArchived', |
||
| 91 | 'group_id' => 'setGroupId', |
||
| 92 | |||
| 93 | // Foreign key setters: |
||
| 94 | 'Group' => 'setGroup', |
||
| 95 | ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | public $columns = array( |
||
| 101 | 'id' => array( |
||
| 102 | 'type' => 'int', |
||
| 103 | 'length' => 11, |
||
| 104 | 'primary_key' => true, |
||
| 105 | 'auto_increment' => true, |
||
| 106 | 'default' => null, |
||
| 107 | ), |
||
| 108 | 'title' => array( |
||
| 109 | 'type' => 'varchar', |
||
| 110 | 'length' => 250, |
||
| 111 | 'default' => null, |
||
| 112 | ), |
||
| 113 | 'reference' => array( |
||
| 114 | 'type' => 'varchar', |
||
| 115 | 'length' => 250, |
||
| 116 | 'default' => null, |
||
| 117 | ), |
||
| 118 | 'branch' => array( |
||
| 119 | 'type' => 'varchar', |
||
| 120 | 'length' => 50, |
||
| 121 | 'default' => 'master', |
||
| 122 | ), |
||
| 123 | 'ssh_private_key' => array( |
||
| 124 | 'type' => 'text', |
||
| 125 | 'nullable' => true, |
||
| 126 | 'default' => null, |
||
| 127 | ), |
||
| 128 | 'type' => array( |
||
| 129 | 'type' => 'varchar', |
||
| 130 | 'length' => 50, |
||
| 131 | 'default' => null, |
||
| 132 | ), |
||
| 133 | 'access_information' => array( |
||
| 134 | 'type' => 'varchar', |
||
| 135 | 'length' => 250, |
||
| 136 | 'nullable' => true, |
||
| 137 | 'default' => null, |
||
| 138 | ), |
||
| 139 | 'last_commit' => array( |
||
| 140 | 'type' => 'varchar', |
||
| 141 | 'length' => 250, |
||
| 142 | 'nullable' => true, |
||
| 143 | 'default' => null, |
||
| 144 | ), |
||
| 145 | 'build_config' => array( |
||
| 146 | 'type' => 'text', |
||
| 147 | 'nullable' => true, |
||
| 148 | 'default' => null, |
||
| 149 | ), |
||
| 150 | 'ssh_public_key' => array( |
||
| 151 | 'type' => 'text', |
||
| 152 | 'nullable' => true, |
||
| 153 | 'default' => null, |
||
| 154 | ), |
||
| 155 | 'allow_public_status' => array( |
||
| 156 | 'type' => 'int', |
||
| 157 | 'length' => 11, |
||
| 158 | ), |
||
| 159 | 'archived' => array( |
||
| 160 | 'type' => 'tinyint', |
||
| 161 | 'length' => 1, |
||
| 162 | 'default' => null, |
||
| 163 | ), |
||
| 164 | 'group_id' => array( |
||
| 165 | 'type' => 'int', |
||
| 166 | 'length' => 11, |
||
| 167 | 'default' => 1, |
||
| 168 | ), |
||
| 169 | ); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var array |
||
| 173 | */ |
||
| 174 | public $indexes = array( |
||
| 175 | 'PRIMARY' => array('unique' => true, 'columns' => 'id'), |
||
| 176 | 'idx_project_title' => array('columns' => 'title'), |
||
| 177 | 'group_id' => array('columns' => 'group_id'), |
||
| 178 | ); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | public $foreignKeys = array( |
||
| 184 | 'project_ibfk_1' => array( |
||
| 185 | 'local_col' => 'group_id', |
||
| 186 | 'update' => 'CASCADE', |
||
| 187 | 'delete' => '', |
||
| 188 | 'table' => 'project_group', |
||
| 189 | 'col' => 'id', |
||
| 190 | ), |
||
| 191 | ); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the value of Id / id. |
||
| 195 | * |
||
| 196 | * @return int |
||
| 197 | */ |
||
| 198 | 8 | public function getId() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get the value of Title / title. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | 7 | public function getTitle() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Get the value of Reference / reference. |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 5 | public function getReference() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Get the value of Branch / branch. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function getBranch() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the value of SshPrivateKey / ssh_private_key. |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | 1 | public function getSshPrivateKey() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Get the value of Type / type. |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | 12 | public function getType() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get the value of AccessInformation / access_information. |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getAccessInformation() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the value of LastCommit / last_commit. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getLastCommit() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the value of BuildConfig / build_config. |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 1 | public function getBuildConfig() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Get the value of SshPublicKey / ssh_public_key. |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | 1 | public function getSshPublicKey() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Get the value of AllowPublicStatus / allow_public_status. |
||
| 315 | * |
||
| 316 | * @return int |
||
| 317 | */ |
||
| 318 | 2 | public function getAllowPublicStatus() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Get the value of Archived / archived. |
||
| 327 | * |
||
| 328 | * @return int |
||
| 329 | */ |
||
| 330 | public function getArchived() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the value of GroupId / group_id. |
||
| 339 | * |
||
| 340 | * @return int |
||
| 341 | */ |
||
| 342 | public function getGroupId() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set the value of Id / id. |
||
| 351 | * |
||
| 352 | * Must not be null. |
||
| 353 | * |
||
| 354 | * @param $value int |
||
| 355 | */ |
||
| 356 | 8 | public function setId($value) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Set the value of Title / title. |
||
| 372 | * |
||
| 373 | * Must not be null. |
||
| 374 | * |
||
| 375 | * @param $value string |
||
| 376 | */ |
||
| 377 | 10 | public function setTitle($value) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Set the value of Reference / reference. |
||
| 393 | * |
||
| 394 | * Must not be null. |
||
| 395 | * |
||
| 396 | * @param $value string |
||
| 397 | */ |
||
| 398 | 5 | public function setReference($value) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Set the value of Branch / branch. |
||
| 414 | * |
||
| 415 | * Must not be null. |
||
| 416 | * |
||
| 417 | * @param $value string |
||
| 418 | */ |
||
| 419 | 6 | public function setBranch($value) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Set the value of SshPrivateKey / ssh_private_key. |
||
| 435 | * |
||
| 436 | * @param $value string |
||
| 437 | */ |
||
| 438 | 2 | public function setSshPrivateKey($value) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Set the value of Type / type. |
||
| 453 | * |
||
| 454 | * Must not be null. |
||
| 455 | * |
||
| 456 | * @param $value string |
||
| 457 | */ |
||
| 458 | 13 | public function setType($value) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Set the value of AccessInformation / access_information. |
||
| 474 | * |
||
| 475 | * @param $value string |
||
| 476 | */ |
||
| 477 | 2 | public function setAccessInformation($value) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Set the value of LastCommit / last_commit. |
||
| 492 | * |
||
| 493 | * @param $value string |
||
| 494 | */ |
||
| 495 | public function setLastCommit($value) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Set the value of BuildConfig / build_config. |
||
| 510 | * |
||
| 511 | * @param $value string |
||
| 512 | */ |
||
| 513 | 2 | public function setBuildConfig($value) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Set the value of SshPublicKey / ssh_public_key. |
||
| 528 | * |
||
| 529 | * @param $value string |
||
| 530 | */ |
||
| 531 | 2 | public function setSshPublicKey($value) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Set the value of AllowPublicStatus / allow_public_status. |
||
| 546 | * |
||
| 547 | * Must not be null. |
||
| 548 | * |
||
| 549 | * @param $value int |
||
| 550 | */ |
||
| 551 | 5 | public function setAllowPublicStatus($value) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Set the value of Archived / archived. |
||
| 567 | * |
||
| 568 | * Must not be null. |
||
| 569 | * |
||
| 570 | * @param $value int |
||
| 571 | */ |
||
| 572 | public function setArchived($value) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Set the value of GroupId / group_id. |
||
| 588 | * |
||
| 589 | * Must not be null. |
||
| 590 | * |
||
| 591 | * @param $value int |
||
| 592 | */ |
||
| 593 | public function setGroupId($value) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Get the ProjectGroup model for this Project by Id. |
||
| 609 | * |
||
| 610 | * @uses \PHPCI\Store\ProjectGroupStore::getById() |
||
| 611 | * @uses \PHPCI\Model\ProjectGroup |
||
| 612 | * |
||
| 613 | * @return \PHPCI\Model\ProjectGroup |
||
| 614 | */ |
||
| 615 | public function getGroup() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Set Group - Accepts an ID, an array representing a ProjectGroup or a ProjectGroup model. |
||
| 636 | * |
||
| 637 | * @param $value mixed |
||
| 638 | */ |
||
| 639 | public function setGroup($value) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Set Group - Accepts a ProjectGroup model. |
||
| 657 | * |
||
| 658 | * @param $value \PHPCI\Model\ProjectGroup |
||
| 659 | */ |
||
| 660 | public function setGroupObject(\PHPCI\Model\ProjectGroup $value) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Get Build models by ProjectId for this Project. |
||
| 667 | * |
||
| 668 | * @uses \PHPCI\Store\BuildStore::getByProjectId() |
||
| 669 | * @uses \PHPCI\Model\Build |
||
| 670 | * |
||
| 671 | * @return \PHPCI\Model\Build[] |
||
| 672 | */ |
||
| 673 | public function getProjectBuilds() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Get BuildMeta models by ProjectId for this Project. |
||
| 680 | * |
||
| 681 | * @uses \PHPCI\Store\BuildMetaStore::getByProjectId() |
||
| 682 | * @uses \PHPCI\Model\BuildMeta |
||
| 683 | * |
||
| 684 | * @return \PHPCI\Model\BuildMeta[] |
||
| 685 | */ |
||
| 686 | public function getProjectBuildMetas() |
||
| 690 | } |
||
| 691 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: