Conditions | 44 |
Paths | 14689 |
Total Lines | 101 |
Code Lines | 75 |
Lines | 30 |
Ratio | 29.7 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
146 | function parseContextDiff(&$diff) |
||
147 | { |
||
148 | $edits = array(); |
||
149 | $i = $max_i = $j = $max_j = 0; |
||
150 | $end = count($diff) - 1; |
||
151 | while ($i < $end && $j < $end) { |
||
152 | while ($i >= $max_i && $j >= $max_j) { |
||
153 | // Find the boundaries of the diff output of the two files |
||
154 | for ($i = $j; |
||
155 | $i < $end && substr($diff[$i], 0, 3) == '***'; |
||
156 | $i++); |
||
157 | for ($max_i = $i; |
||
158 | $max_i < $end && substr($diff[$max_i], 0, 3) != '---'; |
||
159 | $max_i++); |
||
160 | for ($j = $max_i; |
||
161 | $j < $end && substr($diff[$j], 0, 3) == '---'; |
||
162 | $j++); |
||
163 | for ($max_j = $j; |
||
164 | $max_j < $end && substr($diff[$max_j], 0, 3) != '***'; |
||
165 | $max_j++); |
||
166 | } |
||
167 | |||
168 | // find what hasn't been changed |
||
169 | $array = array(); |
||
170 | while ($i < $max_i && |
||
171 | $j < $max_j && |
||
172 | strcmp($diff[$i], $diff[$j]) == 0) { |
||
173 | $array[] = substr($diff[$i], 2); |
||
174 | $i++; |
||
175 | $j++; |
||
176 | } |
||
177 | |||
178 | View Code Duplication | while ($i < $max_i && ($max_j-$j) <= 1) { |
|
179 | if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') { |
||
180 | break; |
||
181 | } |
||
182 | $array[] = substr($diff[$i++], 2); |
||
183 | } |
||
184 | |||
185 | View Code Duplication | while ($j < $max_j && ($max_i-$i) <= 1) { |
|
186 | if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') { |
||
187 | break; |
||
188 | } |
||
189 | $array[] = substr($diff[$j++], 2); |
||
190 | } |
||
191 | if (count($array) > 0) { |
||
192 | $edits[] = new Text_Diff_Op_copy($array); |
||
193 | } |
||
194 | |||
195 | if ($i < $max_i) { |
||
196 | $diff1 = array(); |
||
197 | switch (substr($diff[$i], 0, 1)) { |
||
198 | case '!': |
||
199 | $diff2 = array(); |
||
200 | do { |
||
201 | $diff1[] = substr($diff[$i], 2); |
||
202 | if ($j < $max_j && substr($diff[$j], 0, 1) == '!') { |
||
203 | $diff2[] = substr($diff[$j++], 2); |
||
204 | } |
||
205 | } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!'); |
||
206 | $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
||
207 | break; |
||
208 | |||
209 | View Code Duplication | case '+': |
|
210 | do { |
||
211 | $diff1[] = substr($diff[$i], 2); |
||
212 | } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+'); |
||
213 | $edits[] = new Text_Diff_Op_add($diff1); |
||
214 | break; |
||
215 | |||
216 | case '-': |
||
217 | do { |
||
218 | $diff1[] = substr($diff[$i], 2); |
||
219 | } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-'); |
||
220 | $edits[] = new Text_Diff_Op_delete($diff1); |
||
221 | break; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | if ($j < $max_j) { |
||
226 | $diff2 = array(); |
||
227 | switch (substr($diff[$j], 0, 1)) { |
||
228 | View Code Duplication | case '+': |
|
229 | do { |
||
230 | $diff2[] = substr($diff[$j++], 2); |
||
231 | } while ($j < $max_j && substr($diff[$j], 0, 1) == '+'); |
||
232 | $edits[] = new Text_Diff_Op_add($diff2); |
||
233 | break; |
||
234 | |||
235 | View Code Duplication | case '-': |
|
236 | do { |
||
237 | $diff2[] = substr($diff[$j++], 2); |
||
238 | } while ($j < $max_j && substr($diff[$j], 0, 1) == '-'); |
||
239 | $edits[] = new Text_Diff_Op_delete($diff2); |
||
240 | break; |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | return $edits; |
||
246 | } |
||
247 | |||
249 |