Passed
Push — master ( 948458...49e3d0 )
by Josh
01:11
created

ListDiffNew   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 261
Duplicated Lines 3.83 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.53%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 51
c 3
b 2
f 1
lcom 1
cbo 5
dl 10
loc 261
ccs 159
cts 170
cp 0.9353
rs 8.3206

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 10 10 2
A build() 0 9 1
F diffLists() 0 125 27
C buildDiffList() 0 75 13
A isOpeningListTag() 0 5 2
A isClosingListTag() 0 6 2
A isOpeningListItemTag() 0 6 2
A isClosingListItemTag() 0 6 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ListDiffNew 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 ListDiffNew, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Caxy\HtmlDiff;
4
5
use Caxy\HtmlDiff\ListDiff\DiffList;
6
use Caxy\HtmlDiff\ListDiff\DiffListItem;
7
8
class ListDiffNew 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 self
18
     */
19 4 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 4
        $diff = new self($oldText, $newText);
22
23 4
        if (null !== $config) {
24 4
            $diff->setConfig($config);
25 4
        }
26
27 4
        return $diff;
28
    }
29
30 4
    public function build()
31
    {
32 4
        $this->splitInputsToWords();
33
34 4
        return $this->diffLists(
35 4
            $this->buildDiffList($this->oldWords),
36 4
            $this->buildDiffList($this->newWords)
37 4
        );
38
    }
39
40 4
    protected function diffLists(DiffList $oldList, DiffList $newList)
41
    {
42 4
        $oldMatchData = array();
43 4
        $newMatchData = array();
44 4
        $oldListIndices = array();
45 4
        $newListIndices = array();
46 4
        $oldListItems = array();
47 4
        $newListItems = array();
48
49 4
        foreach ($oldList->getListItems() as $oldIndex => $oldListItem) {
50 4
            if ($oldListItem instanceof DiffListItem) {
51 4
                $oldListItems[$oldIndex] = $oldListItem;
52
53 4
                $oldListIndices[] = $oldIndex;
54 4
                $oldMatchData[$oldIndex] = array();
55
56
                // Get match percentages
57 4
                foreach ($newList->getListItems() as $newIndex => $newListItem) {
58 4
                    if ($newListItem instanceof DiffListItem) {
59 4
                        if (!in_array($newListItem, $newListItems)) {
60 4
                            $newListItems[$newIndex] = $newListItem;
61 4
                        }
62 4
                        if (!in_array($newIndex, $newListIndices)) {
63 4
                            $newListIndices[] = $newIndex;
64 4
                        }
65 4
                        if (!array_key_exists($newIndex, $newMatchData)) {
66 4
                            $newMatchData[$newIndex] = array();
67 4
                        }
68
69 4
                        $oldText = implode('', $oldListItem->getText());
70 4
                        $newText = implode('', $newListItem->getText());
71
72
                        // similar_text
73 4
                        $percentage = null;
74 4
                        similar_text($oldText, $newText, $percentage);
75
76 4
                        $oldMatchData[$oldIndex][$newIndex] = $percentage;
77 4
                        $newMatchData[$newIndex][$oldIndex] = $percentage;
78 4
                    }
79 4
                }
80 4
            }
81 4
        }
82
83 4
        $currentIndexInOld = 0;
84 4
        $currentIndexInNew = 0;
85 4
        $oldCount = count($oldListIndices);
86 4
        $newCount = count($newListIndices);
87 4
        $difference = max($oldCount, $newCount) - min($oldCount, $newCount);
88
89 4
        $diffOutput = '';
90
91 4
        foreach ($newList->getListItems() as $newIndex => $newListItem) {
92 4
            if ($newListItem instanceof DiffListItem) {
93 4
                $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...
94
95 4
                $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...
96 4
                $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...
97
98 4
                if (null !== $oldListIndex && array_key_exists($oldListIndex, $oldMatchData)) {
99
                    // Check percentage matches of upcoming list items in old.
100 4
                    $matchPercentage = $oldMatchData[$oldListIndex][$newIndex];
101
102
                    // does the old list item match better?
103 4
                    $otherMatchBetter = false;
104 4
                    foreach ($oldMatchData[$oldListIndex] as $index => $percentage) {
105 4
                        if ($index > $newIndex && $percentage > $matchPercentage) {
106 2
                            $otherMatchBetter = $index;
107 2
                        }
108 4
                    }
109
110 4
                    if (false !== $otherMatchBetter && $newCount > $oldCount && $difference > 0) {
111 1
                        $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins'));
112 1
                        $currentIndexInNew++;
113 1
                        $difference--;
114
115 1
                        continue;
116
                    }
117
118 4
                    $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...
119
120 4
                    $replacement = false;
121
122 4
                    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...
123
                        // Following list item in old is better match, use that.
124
                        $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del'));
125
126
                        $currentIndexInOld++;
127
                        $oldListIndex = $nextOldListIndex;
128
                        $matchPercentage = $oldMatchData[$oldListIndex];
129
                        $replacement = true;
130
                    }
131
132 4
                    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...
133
                        // Diff the two lists.
134 4
                        $htmlDiff = HtmlDiff::create(
135 4
                            $oldListItems[$oldListIndex]->getInnerHtml(),
136 4
                            $newListItem->getInnerHtml(),
137 4
                            $this->config
138 4
                        );
139 4
                        $diffContent = $htmlDiff->build();
140
141 4
                        $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...
142
143 4
                    } else {
144
                        $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del'));
145
                        $diffOutput .= sprintf('%s', $newListItem->getHtml('replacement', 'ins'));
146
                    }
147 4
                    $currentIndexInOld++;
148 4
                } else {
149
                    $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins'));
150
                }
151
152 4
                $currentIndexInNew++;
153 4
            }
154 4
        }
155
156
        // Output any additional list items
157 4
        while (array_key_exists($currentIndexInOld, $oldListIndices)) {
158 2
            $oldListIndex = $oldListIndices[$currentIndexInOld];
159 2
            $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del'));
160 2
            $currentIndexInOld++;
161 2
        }
162
163 4
        return sprintf('%s%s%s', $newList->getStartTagWithDiffClass(), $diffOutput, $newList->getEndTag());
164
    }
165
166 4
    protected function buildDiffList($words)
167
    {
168 4
        $listType = null;
169 4
        $listStartTag = null;
170 4
        $listEndTag = null;
171 4
        $attributes = array();
172 4
        $openLists = 0;
173 4
        $openListItems = 0;
174 4
        $list = array();
175 4
        $currentListItem = null;
176 4
        $listItemType = null;
177 4
        $listItemStart = null;
178 4
        $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...
179
180 4
        foreach ($words as $i => $word) {
181 4
            if ($this->isOpeningListTag($word, $listType)) {
182 4
                if ($openLists > 0) {
183 1
                    if ($openListItems > 0) {
184 1
                        $currentListItem[] = $word;
185 1
                    } else {
186
                        $list[] = $word;
187
                    }
188 1
                } else {
189 4
                    $listType = substr($word, 1, 2);
190 4
                    $listStartTag = $word;
191
                }
192
193 4
                $openLists++;
194 4
            } elseif ($this->isClosingListTag($word, $listType)) {
195 4
                if ($openLists > 1) {
196 1
                    if ($openListItems > 0) {
197 1
                        $currentListItem[] = $word;
198 1
                    } else {
199
                        $list[] = $word;
200
                    }
201 1
                } else {
202 4
                    $listEndTag = $word;
203
                }
204
205 4
                $openLists--;
206 4
            } elseif ($this->isOpeningListItemTag($word, $listItemType)) {
207 4
                if ($openListItems === 0) {
208
                    // New top-level list item
209 4
                    $currentListItem = array();
210 4
                    $listItemType = substr($word, 1, 2);
211 4
                    $listItemStart = $word;
212 4
                } else {
213 1
                    $currentListItem[] = $word;
214
                }
215
216 4
                $openListItems++;
217 4
            } elseif ($this->isClosingListItemTag($word, $listItemType)) {
218 4
                if ($openListItems === 1) {
219 4
                    $listItemEnd = $word;
220 4
                    $listItem = new DiffListItem($currentListItem, array(), $listItemStart, $listItemEnd);
221 4
                    $list[] = $listItem;
222 4
                    $currentListItem = null;
223 4
                } else {
224 1
                    $currentListItem[] = $word;
225
                }
226
227 4
                $openListItems--;
228 4
            } else {
229 4
                if ($openListItems > 0) {
230 4
                    $currentListItem[] = $word;
231 4
                } else {
232 4
                    $list[] = $word;
233
                }
234
            }
235 4
        }
236
237 4
        $diffList = new DiffList($listType, $listStartTag, $listEndTag, $list, $attributes);
238
239 4
        return $diffList;
240
    }
241
242 4
    protected function isOpeningListTag($word, $type = null)
243
    {
244 4
        $filter = $type !== null ? array('<' . $type) : array('<ul', '<ol', '<dl');
245 4
        return in_array(substr($word, 0, 3), $filter);
246
    }
247
248 4
    protected function isClosingListTag($word, $type = null)
249
    {
250 4
        $filter = $type !== null ? array('</' . $type) : array('</ul', '</ol', '</dl');
251
252 4
        return in_array(substr($word, 0, 4), $filter);
253
    }
254
255 4
    protected function isOpeningListItemTag($word, $type = null)
256
    {
257 4
        $filter = $type !== null ? array('<' . $type) : array('<li', '<dd', '<dt');
258
259 4
        return in_array(substr($word, 0, 3), $filter);
260
    }
261
262 4
    protected function isClosingListItemTag($word, $type = null)
263
    {
264 4
        $filter = $type !== null ? array('</' . $type) : array('</li', '</dd', '</dt');
265
266 4
        return in_array(substr($word, 0, 4), $filter);
267
    }
268
}
269