Complex classes like BuildBase 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 BuildBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class BuildBase extends Model |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | public static $sleepable = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $tableName = 'build'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $modelName = 'Build'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $data = array( |
||
| 36 | 'id' => null, |
||
| 37 | 'project_id' => null, |
||
| 38 | 'commit_id' => null, |
||
| 39 | 'status' => null, |
||
| 40 | 'log' => null, |
||
| 41 | 'branch' => null, |
||
| 42 | 'created' => null, |
||
| 43 | 'started' => null, |
||
| 44 | 'finished' => null, |
||
| 45 | 'committer_email' => null, |
||
| 46 | 'commit_message' => null, |
||
| 47 | 'extra' => null, |
||
| 48 | ); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $getters = array( |
||
| 54 | // Direct property getters: |
||
| 55 | 'id' => 'getId', |
||
| 56 | 'project_id' => 'getProjectId', |
||
| 57 | 'commit_id' => 'getCommitId', |
||
| 58 | 'status' => 'getStatus', |
||
| 59 | 'log' => 'getLog', |
||
| 60 | 'branch' => 'getBranch', |
||
| 61 | 'created' => 'getCreated', |
||
| 62 | 'started' => 'getStarted', |
||
| 63 | 'finished' => 'getFinished', |
||
| 64 | 'committer_email' => 'getCommitterEmail', |
||
| 65 | 'commit_message' => 'getCommitMessage', |
||
| 66 | 'extra' => 'getExtra', |
||
| 67 | |||
| 68 | // Foreign key getters: |
||
| 69 | 'Project' => 'getProject', |
||
| 70 | ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $setters = array( |
||
| 76 | // Direct property setters: |
||
| 77 | 'id' => 'setId', |
||
| 78 | 'project_id' => 'setProjectId', |
||
| 79 | 'commit_id' => 'setCommitId', |
||
| 80 | 'status' => 'setStatus', |
||
| 81 | 'log' => 'setLog', |
||
| 82 | 'branch' => 'setBranch', |
||
| 83 | 'created' => 'setCreated', |
||
| 84 | 'started' => 'setStarted', |
||
| 85 | 'finished' => 'setFinished', |
||
| 86 | 'committer_email' => 'setCommitterEmail', |
||
| 87 | 'commit_message' => 'setCommitMessage', |
||
| 88 | 'extra' => 'setExtra', |
||
| 89 | |||
| 90 | // Foreign key setters: |
||
| 91 | 'Project' => 'setProject', |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | public $columns = array( |
||
| 98 | 'id' => array( |
||
| 99 | 'type' => 'int', |
||
| 100 | 'length' => 11, |
||
| 101 | 'primary_key' => true, |
||
| 102 | 'auto_increment' => true, |
||
| 103 | 'default' => null, |
||
| 104 | ), |
||
| 105 | 'project_id' => array( |
||
| 106 | 'type' => 'int', |
||
| 107 | 'length' => 11, |
||
| 108 | 'default' => null, |
||
| 109 | ), |
||
| 110 | 'commit_id' => array( |
||
| 111 | 'type' => 'varchar', |
||
| 112 | 'length' => 50, |
||
| 113 | 'default' => null, |
||
| 114 | ), |
||
| 115 | 'status' => array( |
||
| 116 | 'type' => 'int', |
||
| 117 | 'length' => 11, |
||
| 118 | 'default' => null, |
||
| 119 | ), |
||
| 120 | 'log' => array( |
||
| 121 | 'type' => 'mediumtext', |
||
| 122 | 'nullable' => true, |
||
| 123 | 'default' => null, |
||
| 124 | ), |
||
| 125 | 'branch' => array( |
||
| 126 | 'type' => 'varchar', |
||
| 127 | 'length' => 50, |
||
| 128 | 'default' => 'master', |
||
| 129 | ), |
||
| 130 | 'created' => array( |
||
| 131 | 'type' => 'datetime', |
||
| 132 | 'nullable' => true, |
||
| 133 | 'default' => null, |
||
| 134 | ), |
||
| 135 | 'started' => array( |
||
| 136 | 'type' => 'datetime', |
||
| 137 | 'nullable' => true, |
||
| 138 | 'default' => null, |
||
| 139 | ), |
||
| 140 | 'finished' => array( |
||
| 141 | 'type' => 'datetime', |
||
| 142 | 'nullable' => true, |
||
| 143 | 'default' => null, |
||
| 144 | ), |
||
| 145 | 'committer_email' => array( |
||
| 146 | 'type' => 'varchar', |
||
| 147 | 'length' => 512, |
||
| 148 | 'nullable' => true, |
||
| 149 | 'default' => null, |
||
| 150 | ), |
||
| 151 | 'commit_message' => array( |
||
| 152 | 'type' => 'text', |
||
| 153 | 'nullable' => true, |
||
| 154 | 'default' => null, |
||
| 155 | ), |
||
| 156 | 'extra' => array( |
||
| 157 | 'type' => 'text', |
||
| 158 | 'nullable' => true, |
||
| 159 | 'default' => null, |
||
| 160 | ), |
||
| 161 | ); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | public $indexes = array( |
||
| 167 | 'PRIMARY' => array('unique' => true, 'columns' => 'id'), |
||
| 168 | 'project_id' => array('columns' => 'project_id'), |
||
| 169 | 'idx_status' => array('columns' => 'status'), |
||
| 170 | ); |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | public $foreignKeys = array( |
||
| 176 | 'build_ibfk_1' => array( |
||
| 177 | 'local_col' => 'project_id', |
||
| 178 | 'update' => 'CASCADE', |
||
| 179 | 'delete' => 'CASCADE', |
||
| 180 | 'table' => 'project', |
||
| 181 | 'col' => 'id', |
||
| 182 | ), |
||
| 183 | ); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the value of Id / id. |
||
| 187 | * |
||
| 188 | * @return int |
||
| 189 | */ |
||
| 190 | 23 | public function getId() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Get the value of ProjectId / project_id. |
||
| 199 | * |
||
| 200 | * @return int |
||
| 201 | */ |
||
| 202 | 3 | public function getProjectId() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get the value of CommitId / commit_id. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 15 | public function getCommitId() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get the value of Status / status. |
||
| 223 | * |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | 8 | public function getStatus() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get the value of Log / log. |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 2 | public function getLog() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get the value of Branch / branch. |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | 4 | public function getBranch() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get the value of Created / created. |
||
| 259 | * |
||
| 260 | * @return \DateTime |
||
| 261 | */ |
||
| 262 | 2 | public function getCreated() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Get the value of Started / started. |
||
| 275 | * |
||
| 276 | * @return \DateTime |
||
| 277 | */ |
||
| 278 | 2 | public function getStarted() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Get the value of Finished / finished. |
||
| 291 | * |
||
| 292 | * @return \DateTime |
||
| 293 | */ |
||
| 294 | 6 | public function getFinished() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Get the value of CommitterEmail / committer_email. |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | 3 | public function getCommitterEmail() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get the value of CommitMessage / commit_message. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getCommitMessage() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the value of Extra / extra. |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getExtra() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set the value of Id / id. |
||
| 343 | * |
||
| 344 | * Must not be null. |
||
| 345 | * |
||
| 346 | * @param $value int |
||
| 347 | */ |
||
| 348 | 6 | public function setId($value) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Set the value of ProjectId / project_id. |
||
| 364 | * |
||
| 365 | * Must not be null. |
||
| 366 | * |
||
| 367 | * @param $value int |
||
| 368 | */ |
||
| 369 | 9 | public function setProjectId($value) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Set the value of CommitId / commit_id. |
||
| 385 | * |
||
| 386 | * Must not be null. |
||
| 387 | * |
||
| 388 | * @param $value string |
||
| 389 | */ |
||
| 390 | 4 | public function setCommitId($value) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Set the value of Status / status. |
||
| 406 | * |
||
| 407 | * Must not be null. |
||
| 408 | * |
||
| 409 | * @param $value int |
||
| 410 | */ |
||
| 411 | 10 | public function setStatus($value) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Set the value of Log / log. |
||
| 427 | * |
||
| 428 | * @param $value string |
||
| 429 | */ |
||
| 430 | 1 | public function setLog($value) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Set the value of Branch / branch. |
||
| 445 | * |
||
| 446 | * Must not be null. |
||
| 447 | * |
||
| 448 | * @param $value string |
||
| 449 | */ |
||
| 450 | 9 | public function setBranch($value) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Set the value of Created / created. |
||
| 466 | * |
||
| 467 | * @param $value \DateTime |
||
| 468 | */ |
||
| 469 | 4 | public function setCreated($value) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Set the value of Started / started. |
||
| 484 | * |
||
| 485 | * @param $value \DateTime |
||
| 486 | */ |
||
| 487 | 5 | public function setStarted($value) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Set the value of Finished / finished. |
||
| 502 | * |
||
| 503 | * @param $value \DateTime |
||
| 504 | */ |
||
| 505 | 5 | public function setFinished($value) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Set the value of CommitterEmail / committer_email. |
||
| 520 | * |
||
| 521 | * @param $value string |
||
| 522 | */ |
||
| 523 | 2 | public function setCommitterEmail($value) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Set the value of CommitMessage / commit_message. |
||
| 538 | * |
||
| 539 | * @param $value string |
||
| 540 | */ |
||
| 541 | 4 | public function setCommitMessage($value) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Set the value of Extra / extra. |
||
| 556 | * |
||
| 557 | * @param $value string |
||
| 558 | */ |
||
| 559 | 3 | public function setExtra($value) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Get the Project model for this Build by Id. |
||
| 574 | * |
||
| 575 | * @uses \PHPCI\Store\ProjectStore::getById() |
||
| 576 | * @uses \PHPCI\Model\Project |
||
| 577 | * |
||
| 578 | * @return \PHPCI\Model\Project |
||
| 579 | */ |
||
| 580 | public function getProject() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Set Project - Accepts an ID, an array representing a Project or a Project model. |
||
| 601 | * |
||
| 602 | * @param $value mixed |
||
| 603 | */ |
||
| 604 | 4 | public function setProject($value) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Set Project - Accepts a Project model. |
||
| 622 | * |
||
| 623 | * @param $value \PHPCI\Model\Project |
||
| 624 | */ |
||
| 625 | 8 | public function setProjectObject(\PHPCI\Model\Project $value) |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Get BuildError models by BuildId for this Build. |
||
| 632 | * |
||
| 633 | * @uses \PHPCI\Store\BuildErrorStore::getByBuildId() |
||
| 634 | * @uses \PHPCI\Model\BuildError |
||
| 635 | * |
||
| 636 | * @return \PHPCI\Model\BuildError[] |
||
| 637 | */ |
||
| 638 | public function getBuildBuildErrors() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get BuildMeta models by BuildId for this Build. |
||
| 645 | * |
||
| 646 | * @uses \PHPCI\Store\BuildMetaStore::getByBuildId() |
||
| 647 | * @uses \PHPCI\Model\BuildMeta |
||
| 648 | * |
||
| 649 | * @return \PHPCI\Model\BuildMeta[] |
||
| 650 | */ |
||
| 651 | public function getBuildBuildMetas() |
||
| 655 | } |
||
| 656 |
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: