| Total Complexity | 41 |
| Total Lines | 348 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RangeDifferencer 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.
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 RangeDifferencer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | final class RangeDifferencer |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Prevent class instantiation. |
||
| 37 | */ |
||
| 38 | private function __construct() |
||
| 39 | { |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Finds the differences between two RangeComparatorInterfaces. |
||
| 44 | * The differences are returned as an array of RangeDifferences. |
||
| 45 | * If no differences are detected an empty array is returned. |
||
| 46 | * |
||
| 47 | * @param RangeComparatorInterface $left |
||
| 48 | * @param RangeComparatorInterface $right |
||
| 49 | * @return RangeDifference[] |
||
| 50 | */ |
||
| 51 | public static function findDifferences(RangeComparatorInterface $left, RangeComparatorInterface $right): array { |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param RangeComparatorInterface $ancestor |
||
| 57 | * @param RangeComparatorInterface $left |
||
| 58 | * @param RangeComparatorInterface $right |
||
| 59 | * @return RangeDifference[] |
||
| 60 | */ |
||
| 61 | public static function findDifferences3( |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Finds the differences among two RangeComparatorInterfaces. In contrast |
||
| 159 | * to findDifferences, the result contains RangeDifference elements for |
||
| 160 | * non-differing ranges too. |
||
| 161 | * |
||
| 162 | * @param RangeComparatorInterface $left |
||
| 163 | * @param RangeComparatorInterface $right |
||
| 164 | * @return RangeDifference[] |
||
| 165 | */ |
||
| 166 | public static function findRanges(RangeComparatorInterface $left, RangeComparatorInterface $right): array { |
||
| 167 | $in = static::findDifferences($left, $right); |
||
| 168 | $out = []; |
||
| 169 | |||
| 170 | $mStart = 0; |
||
| 171 | $yStart = 0; |
||
| 172 | |||
| 173 | for ($i = 0; $i < count($in); $i++) { |
||
| 174 | $es = $in[$i]; |
||
| 175 | $rd = new RangeDifference( |
||
| 176 | RangeDifference::NOCHANGE, |
||
| 177 | $mStart, $es->getRightStart() - $mStart, |
||
| 178 | $yStart, $es->getLeftStart() - $yStart); |
||
| 179 | |||
| 180 | if (0 !== $rd->getMaxLength()) { |
||
| 181 | $out[] = $rd; |
||
| 182 | } |
||
| 183 | |||
| 184 | $out[] = $es; |
||
| 185 | |||
| 186 | $mStart = $es->getRightEnd(); |
||
| 187 | $yStart = $es->getLeftEnd(); |
||
| 188 | } |
||
| 189 | |||
| 190 | $rd = new RangeDifference(RangeDifference::NOCHANGE, |
||
| 191 | $mStart, $right->getRangeCount() - $mStart, |
||
| 192 | $yStart, $left->getRangeCount() - $yStart); |
||
| 193 | |||
| 194 | if ($rd->getMaxLength() > 0) { |
||
| 195 | $out[] = $rd; |
||
| 196 | } |
||
| 197 | |||
| 198 | return $out; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Finds the differences among three RangeComparatorInterfaces. In contrast |
||
| 203 | * to findDifferences, the result contains RangeDifference elements for |
||
| 204 | * non-differing ranges too. If the ancestor range comparator is null, a |
||
| 205 | * two-way comparison is performed. |
||
| 206 | * |
||
| 207 | * @param RangeComparatorInterface $ancestor |
||
| 208 | * @param RangeComparatorInterface $left |
||
| 209 | * @param RangeComparatorInterface $right |
||
| 210 | * @return RangeDifference[] |
||
| 211 | */ |
||
| 212 | public static function findRanges3( |
||
| 213 | RangeComparatorInterface $ancestor, |
||
| 214 | RangeComparatorInterface $left, |
||
| 215 | RangeComparatorInterface $right): array |
||
| 216 | { |
||
| 217 | if (null === $ancestor) { |
||
| 218 | return static::findRanges($left, $right); |
||
| 219 | } |
||
| 220 | |||
| 221 | $in = static::findDifferences3($ancestor, $left, $right); |
||
| 222 | $out = []; |
||
| 223 | |||
| 224 | $mStart = 0; |
||
| 225 | $yStart = 0; |
||
| 226 | $aStart = 0; |
||
| 227 | |||
| 228 | for ($i = 0; $i < \count($in); $i++) { |
||
| 229 | $es = $in[$i]; |
||
| 230 | $rd = new RangeDifference(RangeDifference::NOCHANGE, |
||
| 231 | $mStart, $es->getRightStart() - $mStart, |
||
| 232 | $yStart, $es->getLeftStart() - $yStart, |
||
| 233 | $aStart, $es->getAncestorStart() - $aStart); |
||
| 234 | |||
| 235 | if ($rd->getMaxLength() > 0) { |
||
| 236 | $out[] = $rd; |
||
| 237 | } |
||
| 238 | |||
| 239 | $out[] = $es; |
||
| 240 | |||
| 241 | $mStart = $es->getRightEnd(); |
||
| 242 | $yStart = $es->getLeftEnd(); |
||
| 243 | $aStart = $es->getAncestorEnd(); |
||
| 244 | } |
||
| 245 | |||
| 246 | $rd = new RangeDifference(RangeDifference::NOCHANGE, |
||
| 247 | $mStart, $right->getRangeCount() - $mStart, |
||
| 248 | $yStart, $left->getRangeCount() - $yStart, |
||
| 249 | $aStart, $ancestor->getRangeCount() - $aStart); |
||
| 250 | |||
| 251 | if ($rd->getMaxLength() > 0) { |
||
| 252 | $out[] = $rd; |
||
| 253 | } |
||
| 254 | |||
| 255 | return $out; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param DifferencesIterator $myIterator |
||
| 260 | * @param DifferencesIterator $yourIterator |
||
| 261 | * @param array $diff3 |
||
| 262 | * @param RangeComparatorInterface $right |
||
| 263 | * @param RangeComparatorInterface $left |
||
| 264 | * @param int $changeRightStart |
||
| 265 | * @param int $changeRightEnd |
||
| 266 | * @return RangeDifference |
||
| 267 | */ |
||
| 268 | private static function createRangeDifference3( |
||
| 269 | DifferencesIterator $myIterator, |
||
| 270 | DifferencesIterator $yourIterator, |
||
| 271 | array &$diff3, |
||
| 272 | RangeComparatorInterface $right, |
||
| 273 | RangeComparatorInterface $left, |
||
| 274 | int $changeRightStart, |
||
| 275 | int $changeRightEnd |
||
| 276 | ): RangeDifference { |
||
| 277 | $kind = RangeDifference::ERROR; |
||
| 278 | /** @var RangeDifference $last */ |
||
| 279 | $last = $diff3[\count($diff3) - 1]; |
||
| 280 | |||
| 281 | // At least one range array must be non-empty. |
||
| 282 | assert(true === ($myIterator->getCount() !== 0 || $yourIterator->getCount() !== 0)); |
||
| 283 | |||
| 284 | // Find corresponding lines to changeRangeStart/End in right and left. |
||
| 285 | if ($myIterator->getCount() === 0) { |
||
| 286 | // Only left changed. |
||
| 287 | $rightStart = $changeRightStart - $last->getAncestorEnd() + $last->getRightEnd(); |
||
| 288 | $rightEnd = $changeRightEnd - $last->getAncestorEnd() + $last->getRightEnd(); |
||
| 289 | $kind = RangeDifference::LEFT; |
||
| 290 | } else { |
||
| 291 | $myRange = $myIterator->getRange(); |
||
| 292 | $f = $myRange[0]; |
||
| 293 | $l = $myRange[\count($myRange) - 1]; |
||
| 294 | $rightStart = $changeRightStart - $f->getLeftStart() + $f->getRightStart(); |
||
| 295 | $rightEnd = $changeRightEnd - $l->getLeftEnd() + $l->getRightEnd(); |
||
| 296 | } |
||
| 297 | |||
| 298 | if ($yourIterator->getCount() === 0) { |
||
| 299 | // Only right changed. |
||
| 300 | $leftStart = $changeRightStart - $last->getAncestorEnd() + $last->getLeftEnd(); |
||
| 301 | $leftEnd = $changeRightEnd - $last->getAncestorEnd() + $last->getLeftEnd(); |
||
| 302 | $kind = RangeDifference::RIGHT; |
||
| 303 | } else { |
||
| 304 | $yourRange = $yourIterator->getRange(); |
||
| 305 | $f = $yourRange[0]; |
||
| 306 | $l = $yourRange[\count($yourRange) - 1]; |
||
| 307 | $leftStart = $changeRightStart - $f->getLeftStart() + $f->getRightStart(); |
||
| 308 | $leftEnd = $changeRightEnd - $l->getLeftEnd() + $l->getRightEnd(); |
||
| 309 | } |
||
| 310 | |||
| 311 | if ($kind === RangeDifference::ERROR) { |
||
| 312 | // Overlapping change (conflict) -> compare the changed ranges. |
||
| 313 | if (static::rangeSpansEqual( |
||
| 314 | $right, $rightStart, $rightEnd - $rightStart, |
||
| 315 | $left, $leftStart, $leftEnd - $leftStart)) { |
||
| 316 | $kind = RangeDifference::ANCESTOR; |
||
| 317 | } else { |
||
| 318 | $kind = RangeDifference::CONFLICT; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | return new RangeDifference( |
||
| 323 | $kind, |
||
| 324 | $rightStart, $rightEnd - $rightStart, |
||
| 325 | $leftStart, $leftEnd - $leftStart, |
||
| 326 | $changeRightStart, $changeRightEnd - $changeRightStart); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Tests whether right and left changed in the same way. |
||
| 331 | * |
||
| 332 | * @param RangeComparatorInterface $right |
||
| 333 | * @param int $rightStart |
||
| 334 | * @param int $rightLength |
||
| 335 | * @param RangeComparatorInterface $left |
||
| 336 | * @param int $leftStart |
||
| 337 | * @param int $leftLength |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | private static function rangeSpansEqual( |
||
| 341 | RangeComparatorInterface $right, |
||
| 342 | int $rightStart, |
||
| 343 | int $rightLength, |
||
| 344 | RangeComparatorInterface $left, |
||
| 345 | int $leftStart, |
||
| 346 | int $leftLength |
||
| 347 | ): bool { |
||
| 348 | if ($rightLength === $leftLength) { |
||
| 349 | for ($i = 0; $i < $rightLength; $i++) { |
||
| 350 | if (!static::rangesEqual( |
||
| 351 | $right, $rightStart + $i, |
||
| 352 | $left, $leftStart + $i)) { |
||
| 353 | break; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | if ($i === $rightLength) { |
||
| 358 | return true; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | return false; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Tests if two ranges are equal. |
||
| 367 | * |
||
| 368 | * @param RangeComparatorInterface $a |
||
| 369 | * @param int $ai |
||
| 370 | * @param RangeComparatorInterface $b |
||
| 371 | * @param int $bi |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | private static function rangesEqual( |
||
| 381 | } |
||
| 382 | } |
||
| 383 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.