Passed
Pull Request — master (#48)
by Josh
03:24
created

ListDiff::buildDiffList()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 75
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 55
CRAP Score 13.0073

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 75
ccs 55
cts 57
cp 0.9649
rs 5.3314
cc 13
eloc 56
nc 13
nop 1
crap 13.0073

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Caxy\HtmlDiff;
4
5
use Caxy\HtmlDiff\ListDiff\DiffList;
6
use Caxy\HtmlDiff\ListDiff\DiffListItem;
7
8
class ListDiff extends AbstractDiff
9
{
10
    protected static $listTypes = array('ul', 'ol', 'dl');
11
12
    /**
13
     * @param string              $oldText
14
     * @param string              $newText
15
     * @param HtmlDiffConfig|null $config
16
     *
17
     * @return ListDiff
18
     */
19 7 View Code Duplication
    public static function create($oldText, $newText, HtmlDiffConfig $config = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21 7
        $diff = new self($oldText, $newText);
22
23 7
        if (null !== $config) {
24 7
            $diff->setConfig($config);
25 7
        }
26
27 7
        return $diff;
28
    }
29
30 7 View Code Duplication
    public function build()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32 7
        if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
33
            $this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);
34
35
            return $this->content;
36
        }
37
38 7
        $this->splitInputsToWords();
39
40 7
        $this->content = $this->diffLists(
41 7
            $this->buildDiffList($this->oldWords),
42 7
            $this->buildDiffList($this->newWords)
43 7
        );
44
45 7
        if ($this->hasDiffCache()) {
46
            $this->getDiffCache()->save($this->oldText, $this->newText, $this->content);
47
        }
48
49 7
        return $this->content;
50
    }
51
52 7
    protected function diffLists(DiffList $oldList, DiffList $newList)
53
    {
54 7
        $oldMatchData = array();
55 7
        $newMatchData = array();
56 7
        $oldListIndices = array();
57 7
        $newListIndices = array();
58 7
        $oldListItems = array();
59 7
        $newListItems = array();
60
61 7
        foreach ($oldList->getListItems() as $oldIndex => $oldListItem) {
62 7
            if ($oldListItem instanceof DiffListItem) {
63 7
                $oldListItems[$oldIndex] = $oldListItem;
64
65 7
                $oldListIndices[] = $oldIndex;
66 7
                $oldMatchData[$oldIndex] = array();
67
68
                // Get match percentages
69 7
                foreach ($newList->getListItems() as $newIndex => $newListItem) {
70 7
                    if ($newListItem instanceof DiffListItem) {
71 7
                        if (!in_array($newListItem, $newListItems)) {
72 7
                            $newListItems[$newIndex] = $newListItem;
73 7
                        }
74 7
                        if (!in_array($newIndex, $newListIndices)) {
75 7
                            $newListIndices[] = $newIndex;
76 7
                        }
77 7
                        if (!array_key_exists($newIndex, $newMatchData)) {
78 7
                            $newMatchData[$newIndex] = array();
79 7
                        }
80
81 7
                        $oldText = implode('', $oldListItem->getText());
82 7
                        $newText = implode('', $newListItem->getText());
83
84
                        // similar_text
85 7
                        $percentage = null;
86 7
                        similar_text($oldText, $newText, $percentage);
87
88 7
                        $oldMatchData[$oldIndex][$newIndex] = $percentage;
89 7
                        $newMatchData[$newIndex][$oldIndex] = $percentage;
90 7
                    }
91 7
                }
92 7
            }
93 7
        }
94
95 7
        $currentIndexInOld = 0;
96 7
        $currentIndexInNew = 0;
97 7
        $oldCount = count($oldListIndices);
98 7
        $newCount = count($newListIndices);
99 7
        $difference = max($oldCount, $newCount) - min($oldCount, $newCount);
100
101 7
        $diffOutput = '';
102
103 7
        foreach ($newList->getListItems() as $newIndex => $newListItem) {
104 7
            if ($newListItem instanceof DiffListItem) {
105 7
                $operation = null;
0 ignored issues
show
Unused Code introduced by
$operation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
107 7
                $oldListIndex = array_key_exists($currentIndexInOld, $oldListIndices) ? $oldListIndices[$currentIndexInOld] : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108 7
                $class = 'normal';
0 ignored issues
show
Unused Code introduced by
$class is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
110 7
                if (null !== $oldListIndex && array_key_exists($oldListIndex, $oldMatchData)) {
111
                    // Check percentage matches of upcoming list items in old.
112 7
                    $matchPercentage = $oldMatchData[$oldListIndex][$newIndex];
113
114
                    // does the old list item match better?
115 7
                    $otherMatchBetter = false;
116 7
                    foreach ($oldMatchData[$oldListIndex] as $index => $percentage) {
117 7
                        if ($index > $newIndex && $percentage > $matchPercentage) {
118 3
                            $otherMatchBetter = $index;
119 3
                        }
120 7
                    }
121
122 7
                    if (false !== $otherMatchBetter && $newCount > $oldCount && $difference > 0) {
123 2
                        $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins'));
124 2
                        ++$currentIndexInNew;
125 2
                        --$difference;
126
127 2
                        continue;
128
                    }
129
130 7
                    $replacement = false;
131
132
                    // is there a better old list item match coming up?
133 7
                    if ($oldCount > $newCount) {
134 3
                        while ($difference > 0 && $this->hasBetterMatch($newMatchData[$newIndex], $oldListIndex)) {
135 2
                            $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del'));
136
137 2
                            ++$currentIndexInOld;
138 2
                            --$difference;
139 2
                            $oldListIndex = array_key_exists($currentIndexInOld, $oldListIndices) ? $oldListIndices[$currentIndexInOld] : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
140 2
                            $matchPercentage = $oldMatchData[$oldListIndex][$newIndex];
141 2
                            $replacement = true;
142 2
                        }
143 3
                    }
144
145 7
                    $nextOldListIndex = array_key_exists($currentIndexInOld + 1, $oldListIndices) ? $oldListIndices[$currentIndexInOld + 1] : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 147 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
146
147 7
                    if ($nextOldListIndex !== null && $oldMatchData[$nextOldListIndex][$newIndex] > $matchPercentage && $oldMatchData[$nextOldListIndex][$newIndex] > $this->config->getMatchThreshold()) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 203 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
148
                        // Following list item in old is better match, use that.
149
                        $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del'));
150
151
                        ++$currentIndexInOld;
152
                        $oldListIndex = $nextOldListIndex;
153
                        $matchPercentage = $oldMatchData[$oldListIndex][$newIndex];
154
                        $replacement = true;
155
                    }
156
157 7
                    if ($matchPercentage > $this->config->getMatchThreshold() || $currentIndexInNew === $currentIndexInOld) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
158
                        // Diff the two lists.
159 6
                        $htmlDiff = HtmlDiff::create(
160 6
                            $oldListItems[$oldListIndex]->getInnerHtml(),
161 6
                            $newListItem->getInnerHtml(),
162 6
                            $this->config
163 6
                        );
164 6
                        $diffContent = $htmlDiff->build();
165
166 6
                        $diffOutput .= sprintf('%s%s%s', $newListItem->getStartTagWithDiffClass($replacement ? 'replacement' : 'normal'), $diffContent, $newListItem->getEndTag());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 179 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
167 6
                    } else {
168 1
                        $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del'));
169 1
                        $diffOutput .= sprintf('%s', $newListItem->getHtml('replacement', 'ins'));
170
                    }
171 7
                    ++$currentIndexInOld;
172 7
                } else {
173
                    $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins'));
174
                }
175
176 7
                ++$currentIndexInNew;
177 7
            }
178 7
        }
179
180
        // Output any additional list items
181 7
        while (array_key_exists($currentIndexInOld, $oldListIndices)) {
182 1
            $oldListIndex = $oldListIndices[$currentIndexInOld];
183 1
            $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del'));
184 1
            ++$currentIndexInOld;
185 1
        }
186
187 7
        return sprintf('%s%s%s', $newList->getStartTagWithDiffClass(), $diffOutput, $newList->getEndTag());
188
    }
189
190
    /**
191
     * @param array $matchData
192
     * @param int   $currentIndex
193
     *
194
     * @return bool
195
     */
196 3
    protected function hasBetterMatch(array $matchData, $currentIndex)
197
    {
198 3
        $matchPercentage = $matchData[$currentIndex];
199 3
        foreach ($matchData as $index => $percentage) {
200 3
            if ($index > $currentIndex &&
201 3
                $percentage > $matchPercentage &&
202 3
                $percentage > $this->config->getMatchThreshold()
203 3
            ) {
204 2
                return true;
205
            }
206 3
        }
207
208 2
        return false;
209
    }
210
211 7
    protected function buildDiffList($words)
212
    {
213 7
        $listType = null;
214 7
        $listStartTag = null;
215 7
        $listEndTag = null;
216 7
        $attributes = array();
217 7
        $openLists = 0;
218 7
        $openListItems = 0;
219 7
        $list = array();
220 7
        $currentListItem = null;
221 7
        $listItemType = null;
222 7
        $listItemStart = null;
223 7
        $listItemEnd = null;
0 ignored issues
show
Unused Code introduced by
$listItemEnd is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
224
225 7
        foreach ($words as $i => $word) {
226 7
            if ($this->isOpeningListTag($word, $listType)) {
227 7
                if ($openLists > 0) {
228 2
                    if ($openListItems > 0) {
229 2
                        $currentListItem[] = $word;
230 2
                    } else {
231
                        $list[] = $word;
232
                    }
233 2
                } else {
234 7
                    $listType = substr($word, 1, 2);
235 7
                    $listStartTag = $word;
236
                }
237
238 7
                ++$openLists;
239 7
            } elseif ($this->isClosingListTag($word, $listType)) {
240 7
                if ($openLists > 1) {
241 2
                    if ($openListItems > 0) {
242 2
                        $currentListItem[] = $word;
243 2
                    } else {
244
                        $list[] = $word;
245
                    }
246 2
                } else {
247 7
                    $listEndTag = $word;
248
                }
249
250 7
                --$openLists;
251 7
            } elseif ($this->isOpeningListItemTag($word, $listItemType)) {
252 7
                if ($openListItems === 0) {
253
                    // New top-level list item
254 7
                    $currentListItem = array();
255 7
                    $listItemType = substr($word, 1, 2);
256 7
                    $listItemStart = $word;
257 7
                } else {
258 4
                    $currentListItem[] = $word;
259
                }
260
261 7
                ++$openListItems;
262 7
            } elseif ($this->isClosingListItemTag($word, $listItemType)) {
263 7
                if ($openListItems === 1) {
264 7
                    $listItemEnd = $word;
265 7
                    $listItem = new DiffListItem($currentListItem, array(), $listItemStart, $listItemEnd);
266 7
                    $list[] = $listItem;
267 7
                    $currentListItem = null;
268 7
                } else {
269 4
                    $currentListItem[] = $word;
270
                }
271
272 7
                --$openListItems;
273 7
            } else {
274 7
                if ($openListItems > 0) {
275 7
                    $currentListItem[] = $word;
276 7
                } else {
277 7
                    $list[] = $word;
278
                }
279
            }
280 7
        }
281
282 7
        $diffList = new DiffList($listType, $listStartTag, $listEndTag, $list, $attributes);
283
284 7
        return $diffList;
285
    }
286
287 7
    protected function isOpeningListTag($word, $type = null)
288
    {
289 7
        $filter = $type !== null ? array('<'.$type) : array('<ul', '<ol', '<dl');
290
291 7
        return in_array(substr($word, 0, 3), $filter);
292
    }
293
294 7
    protected function isClosingListTag($word, $type = null)
295
    {
296 7
        $filter = $type !== null ? array('</'.$type) : array('</ul', '</ol', '</dl');
297
298 7
        return in_array(substr($word, 0, 4), $filter);
299
    }
300
301 7
    protected function isOpeningListItemTag($word, $type = null)
302
    {
303 7
        $filter = $type !== null ? array('<'.$type) : array('<li', '<dd', '<dt');
304
305 7
        return in_array(substr($word, 0, 3), $filter);
306
    }
307
308 7
    protected function isClosingListItemTag($word, $type = null)
309
    {
310 7
        $filter = $type !== null ? array('</'.$type) : array('</li', '</dd', '</dt');
311
312 7
        return in_array(substr($word, 0, 4), $filter);
313
    }
314
}
315