| Conditions | 27 |
| Paths | 528 |
| Total Lines | 121 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 22 | protected function diffLists(DiffList $oldList, DiffList $newList) |
||
| 23 | { |
||
| 24 | $oldMatchData = array(); |
||
|
|
|||
| 25 | $newMatchData = array(); |
||
| 26 | $oldListIndices = array(); |
||
| 27 | $newListIndices = array(); |
||
| 28 | $oldListItems = array(); |
||
| 29 | $newListItems = array(); |
||
| 30 | |||
| 31 | foreach ($oldList->getListItems() as $oldIndex => $oldListItem) { |
||
| 32 | if ($oldListItem instanceof DiffListItem) { |
||
| 33 | $oldListItems[$oldIndex] = $oldListItem; |
||
| 34 | |||
| 35 | $oldListIndices[] = $oldIndex; |
||
| 36 | $oldMatchData[$oldIndex] = array(); |
||
| 37 | |||
| 38 | // Get match percentages |
||
| 39 | foreach ($newList->getListItems() as $newIndex => $newListItem) { |
||
| 40 | if ($newListItem instanceof DiffListItem) { |
||
| 41 | if (!in_array($newListItem, $newListItems)) { |
||
| 42 | $newListItems[$newIndex] = $newListItem; |
||
| 43 | } |
||
| 44 | if (!in_array($newIndex, $newListIndices)) { |
||
| 45 | $newListIndices[] = $newIndex; |
||
| 46 | } |
||
| 47 | if (!array_key_exists($newIndex, $newMatchData)) { |
||
| 48 | $newMatchData[$newIndex] = array(); |
||
| 49 | } |
||
| 50 | |||
| 51 | $oldText = implode('', $oldListItem->getText()); |
||
| 52 | $newText = implode('', $newListItem->getText()); |
||
| 53 | |||
| 54 | // similar_text |
||
| 55 | $percentage = null; |
||
| 56 | similar_text($oldText, $newText, $percentage); |
||
| 57 | |||
| 58 | $oldMatchData[$oldIndex][$newIndex] = $percentage; |
||
| 59 | $newMatchData[$newIndex][$oldIndex] = $percentage; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $currentIndexInOld = 0; |
||
| 66 | $currentIndexInNew = 0; |
||
| 67 | $oldCount = count($oldListIndices); |
||
| 68 | $newCount = count($newListIndices); |
||
| 69 | $difference = max($oldCount, $newCount) - min($oldCount, $newCount); |
||
| 70 | |||
| 71 | $diffOutput = ''; |
||
| 72 | |||
| 73 | foreach ($newList->getListItems() as $newIndex => $newListItem) { |
||
| 74 | if ($newListItem instanceof DiffListItem) { |
||
| 75 | $operation = null; |
||
| 76 | |||
| 77 | $oldListIndex = array_key_exists($currentIndexInOld, $oldListIndices) ? $oldListIndices[$currentIndexInOld] : null; |
||
| 78 | $class = 'normal'; |
||
| 79 | |||
| 80 | if (null !== $oldListIndex && array_key_exists($oldListIndex, $oldMatchData)) { |
||
| 81 | // Check percentage matches of upcoming list items in old. |
||
| 82 | $matchPercentage = $oldMatchData[$oldListIndex][$newIndex]; |
||
| 83 | |||
| 84 | // does the old list item match better? |
||
| 85 | $otherMatchBetter = false; |
||
| 86 | foreach ($oldMatchData[$oldListIndex] as $index => $percentage) { |
||
| 87 | if ($index > $newIndex && $percentage > $matchPercentage) { |
||
| 88 | $otherMatchBetter = $index; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | if (false !== $otherMatchBetter && $newCount > $oldCount && $difference > 0) { |
||
| 93 | $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins')); |
||
| 94 | $currentIndexInNew++; |
||
| 95 | $difference--; |
||
| 96 | |||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | $nextOldListIndex = array_key_exists($currentIndexInOld + 1, $oldListIndices) ? $oldListIndices[$currentIndexInOld + 1] : null; |
||
| 101 | |||
| 102 | $replacement = false; |
||
| 103 | |||
| 104 | if ($nextOldListIndex !== null && $oldMatchData[$nextOldListIndex][$newIndex] > $matchPercentage && $oldMatchData[$nextOldListIndex][$newIndex] > $this->matchThreshold) { |
||
| 105 | // Following list item in old is better match, use that. |
||
| 106 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
||
| 107 | |||
| 108 | $currentIndexInOld++; |
||
| 109 | $oldListIndex = $nextOldListIndex; |
||
| 110 | $matchPercentage = $oldMatchData[$oldListIndex]; |
||
| 111 | $replacement = true; |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($matchPercentage > $this->matchThreshold || $currentIndexInNew === $currentIndexInOld) { |
||
| 115 | // Diff the two lists. |
||
| 116 | $htmlDiff = new HtmlDiff($oldListItems[$oldListIndex]->getInnerHtml(), $newListItem->getInnerHtml(), $this->encoding, $this->specialCaseTags, $this->groupDiffs); |
||
| 117 | $diffContent = $htmlDiff->build(); |
||
| 118 | |||
| 119 | $diffOutput .= sprintf('%s%s%s', $newListItem->getStartTagWithDiffClass($replacement ? 'replacement' : 'normal'), $diffContent, $newListItem->getEndTag()); |
||
| 120 | |||
| 121 | } else { |
||
| 122 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
||
| 123 | $diffOutput .= sprintf('%s', $newListItem->getHtml('replacement', 'ins')); |
||
| 124 | } |
||
| 125 | $currentIndexInOld++; |
||
| 126 | } else { |
||
| 127 | $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins')); |
||
| 128 | } |
||
| 129 | |||
| 130 | $currentIndexInNew++; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // Output any additional list items |
||
| 135 | while (array_key_exists($currentIndexInOld, $oldListIndices)) { |
||
| 136 | $oldListIndex = $oldListIndices[$currentIndexInOld]; |
||
| 137 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
||
| 138 | $currentIndexInOld++; |
||
| 139 | } |
||
| 140 | |||
| 141 | return sprintf('%s%s%s', $newList->getStartTagWithDiffClass(), $diffOutput, $newList->getEndTag()); |
||
| 142 | } |
||
| 143 | |||
| 247 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.