Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Text_Diff_Engine_string 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 Text_Diff_Engine_string, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Text_Diff_Engine_string { |
||
24 | |||
25 | /** |
||
26 | * Parses a unified or context diff. |
||
27 | * |
||
28 | * First param contains the whole diff and the second can be used to force |
||
29 | * a specific diff type. If the second parameter is 'autodetect', the |
||
30 | * diff will be examined to find out which type of diff this is. |
||
31 | * |
||
32 | * @param string $diff The diff content. |
||
33 | * @param string $mode The diff mode of the content in $diff. One of |
||
34 | * 'context', 'unified', or 'autodetect'. |
||
35 | * |
||
36 | * @return array List of all diff operations. |
||
37 | */ |
||
38 | function diff($diff, $mode = 'autodetect') |
||
83 | |||
84 | /** |
||
85 | * Parses an array containing the unified diff. |
||
86 | * |
||
87 | * @param array $diff Array of lines. |
||
88 | * |
||
89 | * @return array List of all diff operations. |
||
90 | */ |
||
91 | function parseUnifiedDiff($diff) |
||
92 | { |
||
93 | $edits = array(); |
||
94 | $end = count($diff) - 1; |
||
95 | for ($i = 0; $i < $end;) { |
||
96 | $diff1 = array(); |
||
97 | switch (substr($diff[$i], 0, 1)) { |
||
98 | case ' ': |
||
99 | do { |
||
100 | $diff1[] = substr($diff[$i], 1); |
||
101 | } while (++$i < $end && substr($diff[$i], 0, 1) == ' '); |
||
102 | $edits[] = new Text_Diff_Op_copy($diff1); |
||
103 | break; |
||
104 | |||
105 | View Code Duplication | case '+': |
|
106 | // get all new lines |
||
107 | do { |
||
108 | $diff1[] = substr($diff[$i], 1); |
||
109 | } while (++$i < $end && substr($diff[$i], 0, 1) == '+'); |
||
110 | $edits[] = new Text_Diff_Op_add($diff1); |
||
111 | break; |
||
112 | |||
113 | case '-': |
||
114 | // get changed or removed lines |
||
115 | $diff2 = array(); |
||
116 | do { |
||
117 | $diff1[] = substr($diff[$i], 1); |
||
118 | } while (++$i < $end && substr($diff[$i], 0, 1) == '-'); |
||
119 | |||
120 | while ($i < $end && substr($diff[$i], 0, 1) == '+') { |
||
121 | $diff2[] = substr($diff[$i++], 1); |
||
122 | } |
||
123 | if (count($diff2) == 0) { |
||
124 | $edits[] = new Text_Diff_Op_delete($diff1); |
||
125 | } else { |
||
126 | $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
||
127 | } |
||
128 | break; |
||
129 | |||
130 | default: |
||
131 | $i++; |
||
132 | break; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return $edits; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Parses an array containing the context diff. |
||
141 | * |
||
142 | * @param array $diff Array of lines. |
||
143 | * |
||
144 | * @return array List of all diff operations. |
||
145 | */ |
||
146 | function parseContextDiff(&$diff) |
||
247 | |||
248 | } |
||
249 |