Skip to content

Commit

Permalink
Fixed warnings "count(): Parameter must be an array... (#65)
Browse files Browse the repository at this point in the history
* Fixed warnings "count(): Parameter must be an array or an object that implements Countable" in PHP7.1

* Added ability to preserve newlines in the diff (disabled by default)
  • Loading branch information
di-maroo authored and jschroed91 committed Jan 6, 2018
1 parent d7540c9 commit 231250a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/Caxy/HtmlDiff/AbstractDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ protected function convertHtmlToListOfWords($characterString)
$mode = 'character';
$current_word = '';
$words = array();
$keepNewLines = $this->getConfig()->isKeepNewLines();
foreach ($characterString as $i => $character) {
switch ($mode) {
case 'character':
Expand All @@ -488,7 +489,7 @@ protected function convertHtmlToListOfWords($characterString)
if ($current_word !== '') {
$words[] = $current_word;
}
$current_word = preg_replace('/\s+/S', ' ', $character);
$current_word = $keepNewLines ? $character : preg_replace('/\s+/S', ' ', $character);
$mode = 'whitespace';
} else {
if (
Expand Down Expand Up @@ -526,7 +527,7 @@ protected function convertHtmlToListOfWords($characterString)
$mode = 'tag';
} elseif (preg_match("/\s/", $character)) {
$current_word .= $character;
$current_word = preg_replace('/\s+/S', ' ', $current_word);
if (!$keepNewLines) $current_word = preg_replace('/\s+/S', ' ', $current_word);
} else {
if ($current_word != '') {
$words[] = $current_word;
Expand Down
2 changes: 1 addition & 1 deletion lib/Caxy/HtmlDiff/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ protected function insertTag($tag, $cssClass, &$words)
}
}
}
if (count($words) == 0 && count($specialCaseTagInjection) == 0) {
if (count($words) == 0 && strlen($specialCaseTagInjection) == 0) {
break;
}
if ($specialCaseTagInjectionIsBefore) {
Expand Down
23 changes: 23 additions & 0 deletions lib/Caxy/HtmlDiff/HtmlDiffConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class HtmlDiffConfig
*/
protected $insertSpaceInReplace = false;

/**
* Whether to keep newlines in the diff
* @var bool
*/
protected $keepNewLines = false;

/**
* @var string
*/
Expand All @@ -48,6 +54,7 @@ class HtmlDiffConfig
'i' => '[[REPLACE_EM]]',
'a' => '[[REPLACE_A]]',
'img' => '[[REPLACE_IMG]]',
'pre' => '[[REPLACE_PRE]]',
);

/**
Expand Down Expand Up @@ -293,6 +300,22 @@ public function setInsertSpaceInReplace($insertSpaceInReplace)
return $this;
}

/**
* @return bool
*/
public function isKeepNewLines()
{
return $this->keepNewLines;
}

/**
* @param bool $keepNewLines
*/
public function setKeepNewLines($keepNewLines)
{
$this->keepNewLines = $keepNewLines;
}

/**
* @return array
*/
Expand Down
6 changes: 5 additions & 1 deletion lib/Caxy/HtmlDiff/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Caxy\HtmlDiff;

class Match
class Match implements \Countable
{
public $startInOld;
public $startInNew;
Expand All @@ -24,4 +24,8 @@ public function endInNew()
{
return $this->startInNew + $this->size;
}

public function count() {
return (int)$this->size;
}
}

0 comments on commit 231250a

Please sign in to comment.