Skip to content

Commit f5737a6

Browse files
authored
Merge pull request #42 from petrkotek/php7-compatibility
PHP7 compatibility: Avoid using 'e' modifier in preg_replace
2 parents f4db229 + 2900dd9 commit f5737a6

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

composer.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
{
22
"name": "chrisboulton/php-diff",
3-
"type": "library",
3+
"type": "library",
44
"description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).",
55
"authors": [
66
{
77
"name": "Chris Boulton",
88
"email": "@chrisboulton"
99
}
1010
],
11-
"autoload": {
12-
"psr-0": {
13-
"Diff": "lib/"
14-
}
15-
}
16-
}
11+
"autoload": {
12+
"psr-0": {
13+
"Diff": "lib/"
14+
}
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "~5.5"
18+
}
19+
}

lib/Diff/Renderer/Html/Array.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,20 @@ protected function formatLines($lines)
177177
$lines = array_map(array($this, 'ExpandTabs'), $lines);
178178
$lines = array_map(array($this, 'HtmlSafe'), $lines);
179179
foreach($lines as &$line) {
180-
$line = preg_replace('# ( +)|^ #e', "\$this->fixSpaces('\\1')", $line);
180+
$line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
181181
}
182182
return $lines;
183183
}
184184

185185
/**
186186
* Replace a string containing spaces with a HTML representation using  .
187187
*
188-
* @param string $spaces The string of spaces.
188+
* @param string[] $matches Array with preg matches.
189189
* @return string The HTML representation of the string.
190190
*/
191-
function fixSpaces($spaces='')
191+
private function fixSpaces(array $matches)
192192
{
193+
$spaces = $matches[1];
193194
$count = strlen($spaces);
194195
if($count == 0) {
195196
return '';

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="unit">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Tests\Diff\Renderer\Html;
4+
5+
class ArrayTest extends \PHPUnit_Framework_TestCase
6+
{
7+
public function testRenderSimpleDelete()
8+
{
9+
$htmlRenderer = new \Diff_Renderer_Html_Array();
10+
$htmlRenderer->diff = new \Diff(
11+
array('a'),
12+
array()
13+
);
14+
15+
$result = $htmlRenderer->render();
16+
17+
static::assertEquals(array(
18+
array(
19+
array(
20+
'tag' => 'delete',
21+
'base' => array(
22+
'offset' => 0,
23+
'lines' => array(
24+
'a'
25+
)
26+
),
27+
'changed' => array(
28+
'offset' => 0,
29+
'lines' => array()
30+
)
31+
)
32+
)
33+
), $result);
34+
}
35+
36+
public function testRenderFixesSpaces()
37+
{
38+
$htmlRenderer = new \Diff_Renderer_Html_Array();
39+
$htmlRenderer->diff = new \Diff(
40+
array(' a'),
41+
array('a')
42+
);
43+
44+
$result = $htmlRenderer->render();
45+
46+
static::assertEquals(array(
47+
array(
48+
array(
49+
'tag' => 'replace',
50+
'base' => array(
51+
'offset' => 0,
52+
'lines' => array(
53+
'<del>&nbsp; &nbsp;</del>a',
54+
)
55+
),
56+
'changed' => array(
57+
'offset' => 0,
58+
'lines' => array(
59+
'<ins></ins>a'
60+
)
61+
)
62+
)
63+
)
64+
), $result);
65+
}
66+
}

tests/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)