| Conditions | 20 |
| Paths | 66 |
| Total Lines | 82 |
| Code Lines | 49 |
| 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 |
||
| 99 | public function getDifferences(): array |
||
| 100 | { |
||
| 101 | $differences = []; |
||
| 102 | $length = $this->getLength(); |
||
| 103 | |||
| 104 | if (0 === $length) { |
||
| 105 | $differences[] = new RangeDifference( |
||
| 106 | RangeDifference::CHANGE, |
||
| 107 | 0, $this->comparator2->getRangeCount(), |
||
| 108 | 0 , $this->comparator1->getRangeCount()); |
||
| 109 | } else { |
||
| 110 | $index1 = 0; |
||
| 111 | $index2 = 0; |
||
| 112 | $s1 = -1; |
||
| 113 | $s2 = -1; |
||
| 114 | |||
| 115 | while ($index1 < \count($this->lcs[0]) && $index2 < \count($this->lcs[1])) { |
||
| 116 | // Move both LCS lists to the next occupied slot. |
||
| 117 | while (0 === $l1 = $this->lcs[0][$index1]) { |
||
| 118 | $index1++; |
||
| 119 | |||
| 120 | if ($index1 >= \count($this->lcs[0])) { |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($index1 >= \count($this->lcs[0])) { |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | |||
| 129 | while (0 === $l2 = $this->lcs[1][$index2]) { |
||
| 130 | $index2++; |
||
| 131 | |||
| 132 | if ($index2 >= \count($this->lcs[1])) { |
||
| 133 | break; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($index2 >= \count($this->lcs[1])) { |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | |||
| 141 | // Convert the entry to an array index (see setLcs(int, int)). |
||
| 142 | $end1 = $l1 - 1; |
||
| 143 | $end2 = $l2 - 1; |
||
| 144 | |||
| 145 | if (-1 === $s1 && (0 !== $end1 || 0 !== $end2)) { |
||
| 146 | // There is a diff at the beginning. |
||
| 147 | // TODO: We need to confirm that this is the proper order. |
||
| 148 | $differences[] = new RangeDifference(RangeDifference::CHANGE, 0, $end2, 0, $end1); |
||
| 149 | } elseif ($end1 !== $s1 + 1 || $end2 !== $s2 + 1) { |
||
| 150 | // A diff was found on one of the sides. |
||
| 151 | $leftStart = $s1 + 1; |
||
| 152 | $leftLength = $end1 - $leftStart; |
||
| 153 | $rightStart = $s2 + 1; |
||
| 154 | $rightLength = $end2 - $rightStart; |
||
| 155 | |||
| 156 | // TODO: We need to confirm that this is the proper order. |
||
| 157 | $differences[] = new RangeDifference( |
||
| 158 | RangeDifference::CHANGE, $rightStart, $rightLength, $leftStart, $leftLength); |
||
| 159 | } |
||
| 160 | |||
| 161 | $s1 = $end1; |
||
| 162 | $s2 = $end2; |
||
| 163 | $index1++; |
||
| 164 | $index2++; |
||
| 165 | } |
||
| 166 | |||
| 167 | if (-1 !== $s1 && ($s1 + 1 < $this->comparator1->getRangeCount() || |
||
| 168 | $s2 + 1 < $this->comparator2->getRangeCount())) { |
||
| 169 | $leftStart = $s1 < $this->comparator1->getRangeCount() ? $s1 + 1 : $s1; |
||
| 170 | $rightStart = $s2 < $this->comparator2->getRangeCount() ? $s2 + 1 : $s2; |
||
| 171 | |||
| 172 | // TODO: We need to confirm that this is the proper order. |
||
| 173 | $differences[] = new RangeDifference( |
||
| 174 | RangeDifference::CHANGE, |
||
| 175 | $rightStart, $this->comparator2->getRangeCount() - $s2 + 1, |
||
| 176 | $leftStart, $this->comparator1->getRangeCount() - $s1 + 1); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return $differences; |
||
| 181 | } |
||
| 183 |