Skip to content

Commit 8db1657

Browse files
committed
Merge pull request #17 from caxy/feature-list_diffing-new
Fix: Updated code so that null is not given in list formatting.
2 parents 661f8b2 + 3271637 commit 8db1657

File tree

1 file changed

+69
-39
lines changed

1 file changed

+69
-39
lines changed

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
class ListDiff extends HtmlDiff
66
{
7+
/**
8+
* This is the minimum percentage a list item can match its counterpart in order to be considered a match.
9+
* @var integer
10+
*/
11+
protected static $listMatchThreshold = 50;
12+
713
/** @var array */
814
protected $listWords = array();
915

@@ -106,22 +112,42 @@ protected function diffListContent()
106112
*/
107113
protected function formatThisListContent()
108114
{
109-
foreach ($this->oldIsolatedDiffTags as $key => $diffTagArray) {
110-
$openingTag = $this->getAndStripTag($diffTagArray[0]);
111-
$closingTag = $this->getAndStripTag($diffTagArray[count($diffTagArray) - 1]);
112-
113-
if (array_key_exists($openingTag, $this->isolatedDiffTags) &&
114-
array_key_exists($closingTag, $this->isolatedDiffTags)
115-
) {
116-
$this->listType = $openingTag;
117-
array_shift($this->oldIsolatedDiffTags[$key]);
118-
array_pop($this->oldIsolatedDiffTags[$key]);
119-
array_shift($this->newIsolatedDiffTags[$key]);
120-
array_pop($this->newIsolatedDiffTags[$key]);
121-
$this->list['old'] = $this->oldIsolatedDiffTags[$key];
122-
$this->list['new'] = $this->newIsolatedDiffTags[$key];
115+
$formatArray = array(
116+
array('type' => 'old', 'array' => $this->oldIsolatedDiffTags),
117+
array('type' => 'new', 'array' => $this->newIsolatedDiffTags)
118+
);
119+
120+
foreach ($formatArray as $item) {
121+
$values = array_values($item['array']);
122+
$this->list[$item['type']] = count($values)
123+
? $this->formatList($values[0], $item['type'])
124+
: array();
125+
}
126+
}
127+
128+
/**
129+
*
130+
* @param array $arrayData
131+
* @param string $index
132+
* @return array
133+
*/
134+
protected function formatList(array $arrayData, $index = 'old')
135+
{
136+
$openingTag = $this->getAndStripTag($arrayData[0]);
137+
$closingTag = $this->getAndStripTag($arrayData[count($arrayData) - 1]);
138+
139+
if (array_key_exists($openingTag, $this->isolatedDiffTags) &&
140+
array_key_exists($closingTag, $this->isolatedDiffTags)
141+
) {
142+
if ($index == 'old') {
143+
$this->listType = $this->getAndStripTag($arrayData[0]);
123144
}
145+
146+
array_shift($arrayData);
147+
array_pop($arrayData);
124148
}
149+
150+
return $arrayData;
125151
}
126152

127153
/**
@@ -206,22 +232,27 @@ function ($v) use ($percent) {
206232
);
207233

208234
arsort($thisBestMatches);
209-
210-
// If no greater amounts, use this one.
211-
if (!count($thisBestMatches)) {
212-
$highestMatch = $percent;
213-
$highestMatchKey = $key;
214-
$takenItemKey = $item;
215-
break;
216-
}
217-
218-
// Loop through, comparing only the items that have not already been added.
219-
foreach ($thisBestMatches as $k => $v) {
220-
if (in_array($k, $takenItems)) {
235+
236+
/**
237+
* If the list item does not meet the threshold, it will not be considered a match.
238+
*/
239+
if ($percent >= self::$listMatchThreshold) {
240+
// If no greater amounts, use this one.
241+
if (!count($thisBestMatches)) {
221242
$highestMatch = $percent;
222243
$highestMatchKey = $key;
223244
$takenItemKey = $item;
224-
break(2);
245+
break;
246+
}
247+
248+
// Loop through, comparing only the items that have not already been added.
249+
foreach ($thisBestMatches as $k => $v) {
250+
if (in_array($k, $takenItems)) {
251+
$highestMatch = $percent;
252+
$highestMatchKey = $key;
253+
$takenItemKey = $item;
254+
break(2);
255+
}
225256
}
226257
}
227258
}
@@ -350,27 +381,27 @@ protected function convertListContentArrayToString($listContentArray)
350381
* @return string
351382
*/
352383
protected function processPlaceholders($text, array $matches)
353-
{
384+
{
354385
// Prepare return
355386
$returnText = array();
356387
// Save the contents of all list nodes, new and old.
357388
$contentVault = array(
358389
'old' => $this->getListContent('old', $matches),
359390
'new' => $this->getListContent('new', $matches)
360391
);
361-
392+
362393
$count = 0;
363394
// Loop through the text checking for placeholders. If a nested list is found, create a new ListDiff object for it.
364395
foreach (explode(' ', $text) as $word) {
365396
$preContent = $this->checkWordForDiffTag($this->stripNewLine($word));
366-
397+
367398
if (in_array(
368399
is_array($preContent) ? $preContent[1] : $preContent,
369400
$this->isolatedDiffTags
370401
)
371402
) {
372-
$oldText = implode('', $contentVault['old'][$count]);
373-
$newText = implode('', $contentVault['new'][$count]);
403+
$oldText = array_key_exists($count, $contentVault['old']) ? implode('', $contentVault['old'][$count]) : '';
404+
$newText = array_key_exists($count, $contentVault['new']) ? implode('', $contentVault['new'][$count]) : '';
374405
$content = $this->diffList($oldText, $newText);
375406
$count++;
376407
} else {
@@ -427,22 +458,21 @@ protected function stripNewLine($text)
427458
* @return array
428459
*/
429460
protected function getListContent($indexKey = 'new', array $matches)
430-
{
461+
{
431462
$bucket = array();
432463

433464
if (isset($matches[$indexKey]) && $matches[$indexKey] !== null) {
434465
$start = $this->listsIndex[$indexKey][$matches[$indexKey]];
435-
$stop = array_key_exists(($matches[$indexKey] + 1), $this->listsIndex[$indexKey])
436-
? $this->listsIndex[$indexKey][$matches[$indexKey] + 1]
437-
: $this->findEndForIndex($this->list[$indexKey], $start);
438-
439-
for ($x = $start; $x < $stop; $x++) {
466+
$stop = $this->findEndForIndex($this->list[$indexKey], $start);
467+
468+
for ($x = $start; $x <= $stop; $x++) {
469+
440470
if (in_array($this->list[$indexKey][$x], $this->isolatedDiffTags)) {
441471
$bucket[] = $this->listIsolatedDiffTags[$indexKey][$x];
442472
}
443473
}
444474
}
445-
475+
446476
return $bucket;
447477
}
448478

0 commit comments

Comments
 (0)