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 | * Returns log message. |
||
305 | * |
||
306 | * @param boolean $with_full_message Show commit message without truncation. |
||
307 | * @param array $revision_data Revision data. |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | private function _generateLogMessageColumn($with_full_message, array $revision_data) |
||
338 | |||
339 | /** |
||
340 | * Generates change summary for a revision. |
||
341 | * |
||
342 | * @param array $revision_paths Revision paths. |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | private function _generateSummaryColumn(array $revision_paths) |
||
374 | |||
375 | /** |
||
376 | * Returns merge conflict path predictions. |
||
377 | * |
||
378 | * @param array $revision_paths Revision paths. |
||
379 | * |
||
380 | * @return array |
||
381 | */ |
||
382 | private function _getMergeConflictPrediction(array $revision_paths) |
||
400 | |||
401 | /** |
||
402 | * Generates content for "Merged Via" cell content. |
||
403 | * |
||
404 | * @param array $merged_via Merged Via. |
||
405 | * @param array $revisions_merged_via_refs Merged Via Refs. |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | private function _generateMergedViaColumn(array $merged_via, array $revisions_merged_via_refs) |
||
430 | |||
431 | /** |
||
432 | * Generates details row content. |
||
433 | * |
||
434 | * @param integer $revision Revision. |
||
435 | * @param array $revisions_refs Refs. |
||
436 | * @param array $revision_paths Revision paths. |
||
437 | * @param array $merge_conflict_prediction Merge conflict prediction. |
||
438 | * @param string $project_path Project path. |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | private function _generateDetailsRowContent( |
||
489 | |||
490 | /** |
||
491 | * Returns path cut off regexp. |
||
492 | * |
||
493 | * @param string $project_path Project path. |
||
494 | * @param array $refs Refs. |
||
495 | * |
||
496 | * @return string |
||
497 | */ |
||
498 | protected function getPathCutOffRegExp($project_path, array $refs) |
||
512 | |||
513 | /** |
||
514 | * Returns relative path to "svn log" returned path. |
||
515 | * |
||
516 | * @param array $path_data Path data. |
||
517 | * @param string $path_key Path key. |
||
518 | * @param string $path_cut_off_regexp Path cut off regexp. |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | private function _getRelativeLogPath(array $path_data, $path_key, $path_cut_off_regexp) |
||
532 | |||
533 | } |
||
534 |
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: