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 | * Creates instance of revision printer. |
||
| 71 | * |
||
| 72 | * @param DateHelper $date_helper Date helper. |
||
| 73 | * @param OutputHelper $output_helper Output helper. |
||
| 74 | */ |
||
| 75 | 1 | public function __construct(DateHelper $date_helper, OutputHelper $output_helper) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Resets state. |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | 1 | private function _resetState() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Adds column to the output. |
||
| 97 | * |
||
| 98 | * @param integer $column Column. |
||
| 99 | * |
||
| 100 | * @return self |
||
| 101 | */ |
||
| 102 | public function withColumn($column) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Sets merge conflict regexps. |
||
| 111 | * |
||
| 112 | * @param array $merge_conflict_regexps Merge conflict regexps. |
||
| 113 | * |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | public function setMergeConflictRegExps(array $merge_conflict_regexps) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Sets log message limit. |
||
| 123 | * |
||
| 124 | * @param integer $log_message_limit Log message limit. |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | public function setLogMessageLimit($log_message_limit) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Prints revisions. |
||
| 135 | * |
||
| 136 | * @param RevisionLog $revision_log Revision log. |
||
| 137 | * @param array $revisions Revisions. |
||
| 138 | * @param OutputInterface $output Output. |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function printRevisions(RevisionLog $revision_log, array $revisions, OutputInterface $output) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns log message. |
||
| 277 | * |
||
| 278 | * @param boolean $with_details With details. |
||
| 279 | * @param array $revision_data Revision data. |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | private function _generateLogMessageColumn($with_details, array $revision_data) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Generates change summary for a revision. |
||
| 311 | * |
||
| 312 | * @param array $revision_paths Revision paths. |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | private function _generateSummaryColumn(array $revision_paths) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns merge conflict path predictions. |
||
| 347 | * |
||
| 348 | * @param array $revision_paths Revision paths. |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | private function _getMergeConflictPrediction(array $revision_paths) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Generates content for "Merged Via" cell content. |
||
| 373 | * |
||
| 374 | * @param array $merged_via Merged Via. |
||
| 375 | * @param array $revisions_merged_via_refs Merged Via Refs. |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | private function _generateMergedViaColumn(array $merged_via, array $revisions_merged_via_refs) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Generates details row content. |
||
| 403 | * |
||
| 404 | * @param integer $revision Revision. |
||
| 405 | * @param array $revisions_refs Refs. |
||
| 406 | * @param array $revision_paths Revision paths. |
||
| 407 | * @param array $merge_conflict_prediction Merge conflict prediction. |
||
| 408 | * @param string $project_path Project path. |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | private function _generateDetailsRowContent( |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Returns path cut off regexp. |
||
| 462 | * |
||
| 463 | * @param string $project_path Project path. |
||
| 464 | * @param array $refs Refs. |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | protected function getPathCutOffRegExp($project_path, array $refs) |
||
| 469 | { |
||
| 470 | $ret = array(); |
||
| 471 | |||
| 472 | // Remove ref from path only for single-ref revision. |
||
| 473 | /*if ( count($refs) === 1 ) { |
||
| 474 | $ret[] = $project_path . reset($refs) . '/'; |
||
| 475 | }*/ |
||
| 476 | |||
| 477 | // Always remove project path. |
||
| 478 | $ret[] = $project_path; |
||
| 479 | |||
| 480 | return '#^(' . implode('|', array_map('preg_quote', $ret)) . ')#'; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Returns relative path to "svn log" returned path. |
||
| 485 | * |
||
| 486 | * @param array $path_data Path data. |
||
| 487 | * @param string $path_key Path key. |
||
| 488 | * @param string $path_cut_off_regexp Path cut off regexp. |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | private function _getRelativeLogPath(array $path_data, $path_key, $path_cut_off_regexp) |
||
| 502 | |||
| 503 | } |
||
| 504 |
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: