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) |
||
184 | { |
||
185 | $table = new Table($output); |
||
186 | $headers = array('Revision', 'Author', 'Date', 'Bug-ID', 'Log Message'); |
||
187 | |||
188 | $with_full_message = in_array(self::COLUMN_FULL_MESSAGE, $this->_columns); |
||
189 | $with_details = in_array(self::COLUMN_DETAILS, $this->_columns); |
||
190 | |||
191 | // Add "Summary" header. |
||
192 | $with_summary = in_array(self::COLUMN_SUMMARY, $this->_columns); |
||
193 | |||
194 | if ( $with_summary ) { |
||
195 | $headers[] = 'Summary'; |
||
196 | } |
||
197 | |||
198 | // Add "Refs" header. |
||
199 | $with_refs = in_array(self::COLUMN_REFS, $this->_columns); |
||
200 | |||
201 | if ( $with_refs ) { |
||
202 | $headers[] = 'Refs'; |
||
203 | } |
||
204 | |||
205 | $with_merge_oracle = in_array(self::COLUMN_MERGE_ORACLE, $this->_columns); |
||
206 | |||
207 | // Add "M.O." header. |
||
208 | if ( $with_merge_oracle ) { |
||
209 | $headers[] = 'M.O.'; |
||
210 | } |
||
211 | |||
212 | // Add "Merged Via" header. |
||
213 | $with_merge_status = in_array(self::COLUMN_MERGE_STATUS, $this->_columns); |
||
214 | |||
215 | if ( $with_merge_status ) { |
||
216 | $headers[] = 'Merged Via'; |
||
217 | } |
||
218 | |||
219 | $table->setHeaders($headers); |
||
220 | |||
221 | $prev_bugs = null; |
||
222 | $last_color = 'yellow'; |
||
223 | |||
224 | if ( $this->_aggregateByBug ) { |
||
225 | $aggregated_revisions = $this->aggregateRevisionsByBug($revisions, $revision_log); |
||
226 | $revisions = array_keys($aggregated_revisions); |
||
227 | } |
||
228 | |||
229 | $last_revision = end($revisions); |
||
230 | |||
231 | $project_path = $revision_log->getProjectPath(); |
||
232 | |||
233 | $bugs_per_row = $with_details ? 1 : 3; |
||
234 | |||
235 | $revisions_data = $revision_log->getRevisionsData('summary', $revisions); |
||
236 | $revisions_paths = $revision_log->getRevisionsData('paths', $revisions); |
||
237 | $revisions_bugs = $revision_log->getRevisionsData('bugs', $revisions); |
||
238 | $revisions_refs = $revision_log->getRevisionsData('refs', $revisions); |
||
239 | |||
240 | if ( $with_merge_status ) { |
||
241 | $revisions_merged_via = $revision_log->getRevisionsData('merges', $revisions); |
||
242 | $revisions_merged_via_refs = $revision_log->getRevisionsData( |
||
243 | 'refs', |
||
244 | call_user_func_array('array_merge', $revisions_merged_via) |
||
245 | ); |
||
246 | } |
||
247 | |||
248 | $first_revision = reset($revisions); |
||
249 | |||
250 | foreach ( $revisions as $revision ) { |
||
251 | $revision_data = $revisions_data[$revision]; |
||
252 | |||
253 | $new_bugs = $revisions_bugs[$revision]; |
||
254 | |||
255 | if ( isset($prev_bugs) && $new_bugs !== $prev_bugs ) { |
||
256 | $last_color = $last_color === 'yellow' ? 'magenta' : 'yellow'; |
||
257 | } |
||
258 | |||
259 | $row = array( |
||
260 | $this->_aggregateByBug ? $aggregated_revisions[$revision] . ' cmts' : $revision, |
||
|
|||
261 | $revision_data['author'], |
||
262 | $this->_dateHelper->getAgoTime($revision_data['date']), |
||
263 | $this->_outputHelper->formatArray($new_bugs, $bugs_per_row, $last_color), |
||
264 | $this->_generateLogMessageColumn($with_full_message || $with_details, $revision_data), |
||
265 | ); |
||
266 | |||
267 | $revision_paths = $revisions_paths[$revision]; |
||
268 | |||
269 | // Add "Summary" column. |
||
270 | if ( $with_summary ) { |
||
271 | $row[] = $this->_generateSummaryColumn($revision_paths); |
||
272 | } |
||
273 | |||
274 | // Add "Refs" column. |
||
275 | if ( $with_refs ) { |
||
276 | $row[] = $this->_outputHelper->formatArray( |
||
277 | $revisions_refs[$revision], |
||
278 | 1 |
||
279 | ); |
||
280 | } |
||
281 | |||
282 | // Add "M.O." column. |
||
283 | if ( $with_merge_oracle ) { |
||
284 | $merge_conflict_prediction = $this->_getMergeConflictPrediction($revision_paths); |
||
285 | $row[] = $merge_conflict_prediction ? '<error>' . count($merge_conflict_prediction) . '</error>' : ''; |
||
286 | } |
||
287 | else { |
||
288 | $merge_conflict_prediction = array(); |
||
289 | } |
||
290 | |||
291 | // Add "Merged Via" column. |
||
292 | if ( $with_merge_status ) { |
||
293 | $row[] = $this->_generateMergedViaColumn($revisions_merged_via[$revision], $revisions_merged_via_refs); |
||
294 | } |
||
295 | |||
296 | if ( $revision === $this->_currentRevision ) { |
||
297 | foreach ( $row as $index => $cell ) { |
||
298 | $row[$index] = $this->applyStyle($cell, 'fg=white;options=bold'); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | if ( $with_full_message && $revision !== $first_revision ) { |
||
303 | $table->addRow(new TableSeparator()); |
||
304 | } |
||
305 | |||
306 | $table->addRow($row); |
||
307 | |||
308 | if ( $with_details ) { |
||
309 | $details = $this->_generateDetailsRowContent( |
||
310 | $revision, |
||
311 | $revisions_refs, |
||
312 | $revision_paths, |
||
313 | $merge_conflict_prediction, |
||
314 | $project_path |
||
315 | ); |
||
316 | |||
317 | $table->addRow(new TableSeparator()); |
||
318 | $table->addRow(array( |
||
319 | new TableCell($details, array('colspan' => count($headers))), |
||
320 | )); |
||
321 | |||
322 | if ( $revision != $last_revision ) { |
||
323 | $table->addRow(new TableSeparator()); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | $prev_bugs = $new_bugs; |
||
328 | } |
||
329 | |||
330 | $table->render(); |
||
331 | |||
332 | $this->_resetState(); |
||
333 | } |
||
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: