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