Complex classes like RevisionPrinter 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 RevisionPrinter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class RevisionPrinter |
||
| 22 | { |
||
| 23 | |||
| 24 | const COLUMN_FULL_MESSAGE = 1; |
||
| 25 | |||
| 26 | const COLUMN_DETAILS = 2; |
||
| 27 | |||
| 28 | const COLUMN_SUMMARY = 3; |
||
| 29 | |||
| 30 | const COLUMN_REFS = 4; |
||
| 31 | |||
| 32 | const COLUMN_MERGE_ORACLE = 5; |
||
| 33 | |||
| 34 | const COLUMN_MERGE_STATUS = 6; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Date helper. |
||
| 38 | * |
||
| 39 | * @var DateHelper |
||
| 40 | */ |
||
| 41 | private $_dateHelper; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Output helper. |
||
| 45 | * |
||
| 46 | * @var OutputHelper |
||
| 47 | */ |
||
| 48 | private $_outputHelper; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Columns. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $_columns = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Merge conflict regexps. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $_mergeConflictRegExps = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Log message limit. |
||
| 66 | * |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | private $_logMessageLimit = 68; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Current revision (e.g. in a working copy). |
||
| 73 | * |
||
| 74 | * @var integer|null |
||
| 75 | */ |
||
| 76 | private $_currentRevision; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Creates instance of revision printer. |
||
| 80 | * |
||
| 81 | * @param DateHelper $date_helper Date helper. |
||
| 82 | * @param OutputHelper $output_helper Output helper. |
||
| 83 | */ |
||
| 84 | 1 | public function __construct(DateHelper $date_helper, OutputHelper $output_helper) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Resets state. |
||
| 94 | * |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | 1 | private function _resetState() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Adds column to the output. |
||
| 106 | * |
||
| 107 | * @param integer $column Column. |
||
| 108 | * |
||
| 109 | * @return self |
||
| 110 | */ |
||
| 111 | public function withColumn($column) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets merge conflict regexps. |
||
| 120 | * |
||
| 121 | * @param array $merge_conflict_regexps Merge conflict regexps. |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function setMergeConflictRegExps(array $merge_conflict_regexps) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Sets log message limit. |
||
| 132 | * |
||
| 133 | * @param integer $log_message_limit Log message limit. |
||
| 134 | * |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | public function setLogMessageLimit($log_message_limit) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Sets current revision. |
||
| 144 | * |
||
| 145 | * @param integer $revision Revision. |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | public function setCurrentRevision($revision) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Prints revisions. |
||
| 156 | * |
||
| 157 | * @param RevisionLog $revision_log Revision log. |
||
| 158 | * @param array $revisions Revisions. |
||
| 159 | * @param OutputInterface $output Output. |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function printRevisions(RevisionLog $revision_log, array $revisions, OutputInterface $output) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Applies a style to the text. |
||
| 305 | * |
||
| 306 | * @param string $text Text. |
||
| 307 | * @param string $style Style. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | protected function applyStyle($text, $style) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns log message. |
||
| 324 | * |
||
| 325 | * @param boolean $with_full_message Show commit message without truncation. |
||
| 326 | * @param array $revision_data Revision data. |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | private function _generateLogMessageColumn($with_full_message, array $revision_data) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Generates change summary for a revision. |
||
| 360 | * |
||
| 361 | * @param array $revision_paths Revision paths. |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | private function _generateSummaryColumn(array $revision_paths) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Returns merge conflict path predictions. |
||
| 396 | * |
||
| 397 | * @param array $revision_paths Revision paths. |
||
| 398 | * |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | private function _getMergeConflictPrediction(array $revision_paths) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Generates content for "Merged Via" cell content. |
||
| 422 | * |
||
| 423 | * @param array $merged_via Merged Via. |
||
| 424 | * @param array $revisions_merged_via_refs Merged Via Refs. |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | private function _generateMergedViaColumn(array $merged_via, array $revisions_merged_via_refs) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Generates details row content. |
||
| 452 | * |
||
| 453 | * @param integer $revision Revision. |
||
| 454 | * @param array $revisions_refs Refs. |
||
| 455 | * @param array $revision_paths Revision paths. |
||
| 456 | * @param array $merge_conflict_prediction Merge conflict prediction. |
||
| 457 | * @param string $project_path Project path. |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | private function _generateDetailsRowContent( |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns path cut off regexp. |
||
| 511 | * |
||
| 512 | * @param string $project_path Project path. |
||
| 513 | * @param array $refs Refs. |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | protected function getPathCutOffRegExp($project_path, array $refs) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Returns relative path to "svn log" returned path. |
||
| 534 | * |
||
| 535 | * @param array $path_data Path data. |
||
| 536 | * @param string $path_key Path key. |
||
| 537 | * @param string $path_cut_off_regexp Path cut off regexp. |
||
| 538 | * |
||
| 539 | * @return string |
||
| 540 | */ |
||
| 541 | private function _getRelativeLogPath(array $path_data, $path_key, $path_cut_off_regexp) |
||
| 551 | |||
| 552 | } |
||
| 553 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: