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 | * Aggregate by bug. |
||
59 | * |
||
60 | * @var boolean |
||
61 | */ |
||
62 | private $_aggregateByBug = false; |
||
63 | |||
64 | /** |
||
65 | * Merge conflict regexps. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | private $_mergeConflictRegExps = array(); |
||
70 | |||
71 | /** |
||
72 | * Log message limit. |
||
73 | * |
||
74 | * @var integer |
||
75 | */ |
||
76 | private $_logMessageLimit = 68; |
||
77 | |||
78 | /** |
||
79 | * Current revision (e.g. in a working copy). |
||
80 | * |
||
81 | * @var integer|null |
||
82 | */ |
||
83 | private $_currentRevision; |
||
84 | |||
85 | /** |
||
86 | * Creates instance of revision printer. |
||
87 | * |
||
88 | * @param DateHelper $date_helper Date helper. |
||
89 | * @param OutputHelper $output_helper Output helper. |
||
90 | */ |
||
91 | 1 | public function __construct(DateHelper $date_helper, OutputHelper $output_helper) |
|
98 | |||
99 | /** |
||
100 | * Resets state. |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | 1 | private function _resetState() |
|
111 | |||
112 | /** |
||
113 | * Adds column to the output. |
||
114 | * |
||
115 | * @param integer $column Column. |
||
116 | * |
||
117 | * @return self |
||
118 | */ |
||
119 | public function withColumn($column) |
||
125 | |||
126 | /** |
||
127 | * Sets aggregate by bug. |
||
128 | * |
||
129 | * @param boolean $aggregate_by_bug Aggregate by bug. |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | public function setAggregateByBug($aggregate_by_bug) |
||
137 | |||
138 | /** |
||
139 | * Sets merge conflict regexps. |
||
140 | * |
||
141 | * @param array $merge_conflict_regexps Merge conflict regexps. |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public function setMergeConflictRegExps(array $merge_conflict_regexps) |
||
149 | |||
150 | /** |
||
151 | * Sets log message limit. |
||
152 | * |
||
153 | * @param integer $log_message_limit Log message limit. |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function setLogMessageLimit($log_message_limit) |
||
161 | |||
162 | /** |
||
163 | * Sets current revision. |
||
164 | * |
||
165 | * @param integer $revision Revision. |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | public function setCurrentRevision($revision) |
||
173 | |||
174 | /** |
||
175 | * Prints revisions. |
||
176 | * |
||
177 | * @param RevisionLog $revision_log Revision log. |
||
178 | * @param array $revisions Revisions. |
||
179 | * @param OutputInterface $output Output. |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | public function printRevisions(RevisionLog $revision_log, array $revisions, OutputInterface $output) |
||
334 | |||
335 | /** |
||
336 | * Aggregates revisions by bugs. |
||
337 | * |
||
338 | * @param array $revisions Revisions. |
||
339 | * @param RevisionLog $revision_log Revision Log. |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | protected function aggregateRevisionsByBug(array $revisions, RevisionLog $revision_log) |
||
378 | |||
379 | /** |
||
380 | * Applies a style to the text. |
||
381 | * |
||
382 | * @param string $text Text. |
||
383 | * @param string $style Style. |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | protected function applyStyle($text, $style) |
||
397 | |||
398 | /** |
||
399 | * Returns log message. |
||
400 | * |
||
401 | * @param boolean $with_full_message Show commit message without truncation. |
||
402 | * @param array $revision_data Revision data. |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | private function _generateLogMessageColumn($with_full_message, array $revision_data) |
||
433 | |||
434 | /** |
||
435 | * Generates change summary for a revision. |
||
436 | * |
||
437 | * @param array $revision_paths Revision paths. |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | private function _generateSummaryColumn(array $revision_paths) |
||
469 | |||
470 | /** |
||
471 | * Returns merge conflict path predictions. |
||
472 | * |
||
473 | * @param array $revision_paths Revision paths. |
||
474 | * |
||
475 | * @return array |
||
476 | */ |
||
477 | private function _getMergeConflictPrediction(array $revision_paths) |
||
495 | |||
496 | /** |
||
497 | * Generates content for "Merged Via" cell content. |
||
498 | * |
||
499 | * @param array $merged_via Merged Via. |
||
500 | * @param array $revisions_merged_via_refs Merged Via Refs. |
||
501 | * |
||
502 | * @return string |
||
503 | */ |
||
504 | private function _generateMergedViaColumn(array $merged_via, array $revisions_merged_via_refs) |
||
525 | |||
526 | /** |
||
527 | * Generates details row content. |
||
528 | * |
||
529 | * @param integer $revision Revision. |
||
530 | * @param array $revisions_refs Refs. |
||
531 | * @param array $revision_paths Revision paths. |
||
532 | * @param array $merge_conflict_prediction Merge conflict prediction. |
||
533 | * @param string $project_path Project path. |
||
534 | * |
||
535 | * @return string |
||
536 | */ |
||
537 | private function _generateDetailsRowContent( |
||
584 | |||
585 | /** |
||
586 | * Returns path cut off regexp. |
||
587 | * |
||
588 | * @param string $project_path Project path. |
||
589 | * @param array $refs Refs. |
||
590 | * |
||
591 | * @return string |
||
592 | */ |
||
593 | protected function getPathCutOffRegExp($project_path, array $refs) |
||
607 | |||
608 | /** |
||
609 | * Returns relative path to "svn log" returned path. |
||
610 | * |
||
611 | * @param array $path_data Path data. |
||
612 | * @param string $path_key Path key. |
||
613 | * @param string $path_cut_off_regexp Path cut off regexp. |
||
614 | * |
||
615 | * @return string |
||
616 | */ |
||
617 | private function _getRelativeLogPath(array $path_data, $path_key, $path_cut_off_regexp) |
||
627 | |||
628 | } |
||
629 |
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: