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_DETAILS = 1; |
||
| 25 | |||
| 26 | const COLUMN_SUMMARY = 2; |
||
| 27 | |||
| 28 | const COLUMN_REFS = 3; |
||
| 29 | |||
| 30 | const COLUMN_MERGE_ORACLE = 4; |
||
| 31 | |||
| 32 | const COLUMN_MERGE_STATUS = 5; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Date helper. |
||
| 36 | * |
||
| 37 | * @var DateHelper |
||
| 38 | */ |
||
| 39 | private $_dateHelper; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Output helper. |
||
| 43 | * |
||
| 44 | * @var OutputHelper |
||
| 45 | */ |
||
| 46 | private $_outputHelper; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Columns. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $_columns = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Merge conflict regexps. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $_mergeConflictRegExps = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Log message limit. |
||
| 64 | * |
||
| 65 | * @var integer |
||
| 66 | */ |
||
| 67 | private $_logMessageLimit = 68; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Current revision (e.g. in a working copy). |
||
| 71 | * |
||
| 72 | * @var integer|null |
||
| 73 | */ |
||
| 74 | private $_currentRevision; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Creates instance of revision printer. |
||
| 78 | * |
||
| 79 | * @param DateHelper $date_helper Date helper. |
||
| 80 | * @param OutputHelper $output_helper Output helper. |
||
| 81 | */ |
||
| 82 | 1 | public function __construct(DateHelper $date_helper, OutputHelper $output_helper) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Resets state. |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 1 | private function _resetState() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Adds column to the output. |
||
| 104 | * |
||
| 105 | * @param integer $column Column. |
||
| 106 | * |
||
| 107 | * @return self |
||
| 108 | */ |
||
| 109 | public function withColumn($column) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Sets merge conflict regexps. |
||
| 118 | * |
||
| 119 | * @param array $merge_conflict_regexps Merge conflict regexps. |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public function setMergeConflictRegExps(array $merge_conflict_regexps) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Sets log message limit. |
||
| 130 | * |
||
| 131 | * @param integer $log_message_limit Log message limit. |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function setLogMessageLimit($log_message_limit) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Sets current revision. |
||
| 142 | * |
||
| 143 | * @param integer $revision Revision. |
||
| 144 | * |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function setCurrentRevision($revision) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Prints revisions. |
||
| 154 | * |
||
| 155 | * @param RevisionLog $revision_log Revision log. |
||
| 156 | * @param array $revisions Revisions. |
||
| 157 | * @param OutputInterface $output Output. |
||
| 158 | * |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function printRevisions(RevisionLog $revision_log, array $revisions, OutputInterface $output) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns log message. |
||
| 302 | * |
||
| 303 | * @param boolean $with_details With details. |
||
| 304 | * @param array $revision_data Revision data. |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | private function _generateLogMessageColumn($with_details, array $revision_data) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Generates change summary for a revision. |
||
| 336 | * |
||
| 337 | * @param array $revision_paths Revision paths. |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function _generateSummaryColumn(array $revision_paths) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns merge conflict path predictions. |
||
| 372 | * |
||
| 373 | * @param array $revision_paths Revision paths. |
||
| 374 | * |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | private function _getMergeConflictPrediction(array $revision_paths) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Generates content for "Merged Via" cell content. |
||
| 398 | * |
||
| 399 | * @param array $merged_via Merged Via. |
||
| 400 | * @param array $revisions_merged_via_refs Merged Via Refs. |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | private function _generateMergedViaColumn(array $merged_via, array $revisions_merged_via_refs) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Generates details row content. |
||
| 428 | * |
||
| 429 | * @param integer $revision Revision. |
||
| 430 | * @param array $revisions_refs Refs. |
||
| 431 | * @param array $revision_paths Revision paths. |
||
| 432 | * @param array $merge_conflict_prediction Merge conflict prediction. |
||
| 433 | * @param string $project_path Project path. |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | private function _generateDetailsRowContent( |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns path cut off regexp. |
||
| 487 | * |
||
| 488 | * @param string $project_path Project path. |
||
| 489 | * @param array $refs Refs. |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | protected function getPathCutOffRegExp($project_path, array $refs) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Returns relative path to "svn log" returned path. |
||
| 510 | * |
||
| 511 | * @param array $path_data Path data. |
||
| 512 | * @param string $path_key Path key. |
||
| 513 | * @param string $path_cut_off_regexp Path cut off regexp. |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | private function _getRelativeLogPath(array $path_data, $path_key, $path_cut_off_regexp) |
||
| 527 | |||
| 528 | } |
||
| 529 |
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: