Skip to content

Commit

Permalink
Implement correct behavior for empty patterns (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Mar 7, 2021
1 parent f6624bb commit 7bb5807
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "1.1-dev"
}
}
}
36 changes: 36 additions & 0 deletions src/Ereg.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ final class Ereg
{
public static function ereg($pattern, $string, &$regs = null)
{
if ($pattern === '' || $pattern === null) {
trigger_error('ereg(): REG_EMPTY', E_USER_WARNING);

return false;
}

$pattern = self::convertPattern($pattern, false);

return \func_num_args() === 2
Expand All @@ -15,6 +21,12 @@ public static function ereg($pattern, $string, &$regs = null)

public static function eregi($pattern, $string, &$regs = null)
{
if ($pattern === '' || $pattern === null) {
trigger_error('eregi(): REG_EMPTY', E_USER_WARNING);

return false;
}

$pattern = self::convertPattern($pattern, true);

return \func_num_args() === 2
Expand All @@ -24,21 +36,45 @@ public static function eregi($pattern, $string, &$regs = null)

public static function ereg_replace($pattern, $replacement, $string)
{
if ($pattern === '' || $pattern === null) {
trigger_error('ereg_replace(): REG_EMPTY', E_USER_WARNING);

return false;
}

return \preg_replace(self::convertPattern($pattern, false), $replacement, $string);
}

public static function eregi_replace($pattern, $replacement, $string)
{
if ($pattern === '' || $pattern === null) {
trigger_error('eregi_replace(): REG_EMPTY', E_USER_WARNING);

return false;
}

return \preg_replace(self::convertPattern($pattern, true), $replacement, $string);
}

public static function split($pattern, $string, $limit = -1)
{
if ($pattern === '' || $pattern === null) {
trigger_error('split(): REG_EMPTY', E_USER_WARNING);

return false;
}

return \preg_split(self::convertPattern($pattern, false), $string, $limit);
}

public static function spliti($pattern, $string, $limit = -1)
{
if ($pattern === '' || $pattern === null) {
trigger_error('spliti(): REG_EMPTY', E_USER_WARNING);

return false;
}

return \preg_split(self::convertPattern($pattern, true), $string, $limit);
}

Expand Down
29 changes: 27 additions & 2 deletions tests/EregReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rabus\EregShim;

use PHPUnit\Framework\TestCase;

class EregReplaceTest extends TestCase
{
private $oldErrorReporting;
Expand All @@ -29,6 +27,16 @@ public function testSimpleReplace()
$this->assertSame('abcdef', \ereg_replace('123', 'def', 'abc123'));
}

public function testNullReplacement()
{
$this->assertSame('abc', \ereg_replace('123', null, 'abc123'));
}

public function testNullString()
{
$this->assertSame('', \ereg_replace('123', 'def', null));
}

public function testReplaceWithEmptyString()
{
$this->assertSame('abc', \ereg_replace('123', '', 'abc123'));
Expand Down Expand Up @@ -221,4 +229,21 @@ public function provideNonMatchingTestCases()
// array('[:alpha:]', 'x'),
);
}

/**
* @dataProvider provideEmptyPatterns
*/
public function testEmptyPattern($pattern)
{
$this->expectEmptyPatternWarning('ereg_replace');
$this->assertFalse(\ereg_replace($pattern, 'def', 'abc123'));
}

public function provideEmptyPatterns()
{
return array(
array(null),
array(''),
);
}
}
22 changes: 19 additions & 3 deletions tests/EregTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rabus\EregShim;

use PHPUnit\Framework\TestCase;

class EregTest extends TestCase
{
private $oldErrorReporting;
Expand Down Expand Up @@ -112,7 +110,7 @@ public function testNonMatchingEregWithoutRegs($pattern, $string)
public function provideNonMatchingTestCases()
{
return array(
array('.*doesn\'t exist.*','This is a nice and simple string'),
array('.*doesn\'t exist.*', 'This is a nice and simple string'),
array('A', 'a'),
array('[A-Z]', '0'),
array('(a){4}', 'aaa'),
Expand All @@ -122,4 +120,22 @@ public function provideNonMatchingTestCases()
// array('[:alpha:]', 'x'),
);
}

/**
* @dataProvider provideEmptyPatterns
*/
public function testEmptyPattern($pattern)
{
$this->expectEmptyPatternWarning('ereg');

$this->assertFalse(\ereg($pattern, 'This is a nice and simple string'));
}

public function provideEmptyPatterns()
{
return array(
array(null),
array(''),
);
}
}
19 changes: 17 additions & 2 deletions tests/EregiReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rabus\EregShim;

use PHPUnit\Framework\TestCase;

class EregiReplaceTest extends TestCase
{
private $oldErrorReporting;
Expand Down Expand Up @@ -136,4 +134,21 @@ public function provideNonMatchingTestCases()
// array('[:alpha:]', 'x'),
);
}

/**
* @dataProvider provideEmptyPatterns
*/
public function testEmptyPattern($pattern)
{
$this->expectEmptyPatternWarning('eregi_replace');
$this->assertFalse(\eregi_replace($pattern, 'def', 'abc123'));
}

public function provideEmptyPatterns()
{
return array(
array(null),
array(''),
);
}
}
19 changes: 17 additions & 2 deletions tests/EregiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rabus\EregShim;

use PHPUnit\Framework\TestCase;

class EregiTest extends TestCase
{
private $oldErrorReporting;
Expand Down Expand Up @@ -136,4 +134,21 @@ public function provideNonMatchingTestCases()
// array('[:alpha:]', 'x'),
);
}

/**
* @dataProvider provideEmptyPatterns
*/
public function testEmptyPattern($pattern)
{
$this->expectEmptyPatternWarning('eregi');
$this->assertFalse(\eregi($pattern, 'This is a nice and simple string'));
}

public function provideEmptyPatterns()
{
return array(
array(null),
array(''),
);
}
}
29 changes: 26 additions & 3 deletions tests/SplitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rabus\EregShim;

use PHPUnit\Framework\TestCase;

class SplitTest extends TestCase
{
private $oldErrorReporting;
Expand All @@ -28,7 +26,15 @@ public function testSplitWithSpaceClass()
{
$this->assertSame(
array('this', 'is', 'a', 'test'),
\split("[[:space:]]","this is\ta\ntest")
\split("[[:space:]]", "this is\ta\ntest")
);
}

public function testNullString()
{
$this->assertSame(
array(''),
\split("[[:space:]]", null)
);
}

Expand Down Expand Up @@ -166,4 +172,21 @@ public function provideNonMatchingTestCases()
// array('[:alpha:]', '--- x ---'),
);
}

/**
* @dataProvider provideEmptyPatterns
*/
public function testEmptyPattern($pattern)
{
$this->expectEmptyPatternWarning('split');
$this->assertFalse(\split($pattern, 'This is a nice and simple string'));
}

public function provideEmptyPatterns()
{
return array(
array(null),
array(''),
);
}
}
19 changes: 17 additions & 2 deletions tests/SplitiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rabus\EregShim;

use PHPUnit\Framework\TestCase;

class SplitiTest extends TestCase
{
private $oldErrorReporting;
Expand Down Expand Up @@ -175,4 +173,21 @@ public function provideNonMatchingTestCases()
// array('[:alpha:]', '--- x ---'),
);
}

/**
* @dataProvider provideEmptyPatterns
*/
public function testEmptyPattern($pattern)
{
$this->expectEmptyPatternWarning('spliti');
$this->assertFalse(\spliti($pattern, 'This is a nice and simple string'));
}

public function provideEmptyPatterns()
{
return array(
array(null),
array(''),
);
}
}
27 changes: 27 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rabus\EregShim;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
protected function expectEmptyPatternWarning($functionName)
{
$message = sprintf('%s(): REG_EMPTY', $functionName);

if (method_exists($this, 'expectWarning')) {
$this->expectWarning();
$this->expectWarningMessage($message);
} elseif (method_exists($this, 'expectException')) {
$this->expectException('PHPUnit\Framework\Error\Warning');
$this->expectExceptionMessage($message);
} else {
$this->setExpectedException('PHPUnit\Framework\Error\Warning', $message);
}
}
}

if (!class_exists('PHPUnit\Framework\Warning')) {
class_alias('PHPUnit_Framework_Error_Warning', 'PHPUnit\Framework\Error\Warning');
}

0 comments on commit 7bb5807

Please sign in to comment.