| Conditions | 11 |
| Paths | 58 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 70 | public function render($diff) |
||
| 71 | { |
||
| 72 | $xi = $yi = 1; |
||
| 73 | $block = false; |
||
| 74 | $context = array(); |
||
| 75 | |||
| 76 | $nlead = $this->_leading_context_lines; |
||
| 77 | $ntrail = $this->_trailing_context_lines; |
||
| 78 | |||
| 79 | $output = $this->_startDiff(); |
||
| 80 | |||
| 81 | foreach ($diff->getDiff() as $edit) { |
||
| 82 | if (is_a($edit, 'Text_Diff_Op_copy')) { |
||
| 83 | if (is_array($block)) { |
||
| 84 | if (count($edit->orig) <= $nlead + $ntrail) { |
||
| 85 | $block[] = $edit; |
||
| 86 | } else { |
||
| 87 | if ($ntrail) { |
||
| 88 | $context = array_slice($edit->orig, 0, $ntrail); |
||
| 89 | $block[] = new Text_Diff_Op_copy($context); |
||
| 90 | } |
||
| 91 | $output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block); |
||
| 92 | $block = false; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | $context = $edit->orig; |
||
| 96 | } else { |
||
| 97 | if (!is_array($block)) { |
||
| 98 | $context = array_slice($context, count($context) - $nlead); |
||
| 99 | $x0 = $xi - count($context); |
||
| 100 | $y0 = $yi - count($context); |
||
| 101 | $block = array(); |
||
| 102 | if ($context) { |
||
| 103 | $block[] = new Text_Diff_Op_copy($context); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | $block[] = $edit; |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($edit->orig) { |
||
| 110 | $xi += count($edit->orig); |
||
| 111 | } |
||
| 112 | if ($edit->final) { |
||
| 113 | $yi += count($edit->final); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | if (is_array($block)) { |
||
| 118 | $output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $output . $this->_endDiff(); |
||
| 122 | } |
||
| 123 | |||
| 258 |
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: