Skip to content

Commit

Permalink
Fix escaping of backslashes in regex and test cases (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis authored Jun 22, 2024
1 parent 15d5e40 commit 346c3b2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function unique(array $array, bool $keepKeys = false): array

$uniqueArray[$value] = $key;
}

return \array_flip($uniqueArray);
}

Expand Down
13 changes: 9 additions & 4 deletions src/FS.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ public static function openFile(string $filepath): ?string

$realPath = \realpath($filepath);
if ($realPath !== false) {
$handle = \fopen($realPath, 'r');
$handle = \fopen($realPath, 'r');
$fileSize = (int)\filesize($realPath);
if ($fileSize === 0) {
return null;
}

if ($handle !== false) {
$contents = (string)\fread($handle, (int)\filesize($realPath));
$contents = (string)\fread($handle, $fileSize);
\fclose($handle);
}
}
Expand Down Expand Up @@ -370,9 +375,9 @@ public static function clean(?string $path, string $dirSep = \DIRECTORY_SEPARATO
$path = \trim((string)$path);

if (($dirSep === '\\') && ($path[0] === '\\') && ($path[1] === '\\')) {
$path = '\\' . \preg_replace('#[/\\\\]+#', $dirSep, $path);
$path = '\\' . \preg_replace('#[/\\\]+#', $dirSep, $path);
} else {
$path = (string)\preg_replace('#[/\\\\]+#', $dirSep, $path);
$path = (string)\preg_replace('#[/\\\]+#', $dirSep, $path);
}

return $path;
Expand Down
10 changes: 5 additions & 5 deletions tests/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ public function testClean(): void
isSame('/path/path', FS::clean('///path///path'));
isSame('/path/path/path', FS::clean('///path///path/path'));
isSame('/path/path/path/', FS::clean('\path\path\path\\\\\\\\'));
isSame('\\path\\path\\path\\', FS::clean('\path\path\path\\\\\\\\', '\\'));
isSame('\\path\\path\\path\\', FS::clean('\\path\\path\\path\\\\\\\\', '\\'));
isSame('\\\\path\\path\\path\\', FS::clean('\\\\path\\path\\path\\\\\\\\', '\\'));
isSame('\path\path\path\\', FS::clean('\path\path\path\\\\\\\\', '\\'));
isSame('\path\path\path\\', FS::clean('\path\path\path\\\\\\\\', '\\'));
isSame('\\\path\path\path\\', FS::clean('\\\path\path\path\\\\\\\\', '\\'));

isSame('../../path/', FS::clean('..///..///path/', '/'));
isSame('./../path/', FS::clean('.///..///path/', '/'));
Expand Down Expand Up @@ -438,10 +438,10 @@ public function testGetRelative(): void

$root = __DIR__ . '/..';
isSame('tests/FileSystemTest.php', FS::getRelative($file, $root, '/'));
isSame('tests\\FileSystemTest.php', FS::getRelative($file, $root, '\\'));
isSame('tests\FileSystemTest.php', FS::getRelative($file, $root, '\\'));

$root = null;
isSame('tests/FileSystemTest.php', FS::getRelative($file, $root, '/'));
isSame('tests\\FileSystemTest.php', FS::getRelative($file, $root, '\\'));
isSame('tests\FileSystemTest.php', FS::getRelative($file, $root, '\\'));
}
}
14 changes: 7 additions & 7 deletions tests/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testClean(): void
$input = ' <b>ASDF</b> !@#$%^&*()_+"\';:>< ';

isSame('ASDF !@#$%^&*()_+"\';:><', Str::clean($input));
isSame('asdf !@#$%^&*()_+\\"\\\';:><', Str::clean($input, true, true));
isSame('asdf !@#$%^&*()_+\"\\\';:><', Str::clean($input, true, true));
}

public function testParseLines(): void
Expand Down Expand Up @@ -272,15 +272,15 @@ public function testEsc(): void
{
isSame(
'&lt;a href="/test"&gt;Test !@#$%^&amp;*()_+\/&lt;/a&gt;',
Str::esc('<a href="/test">Test !@#$%^&*()_+\\/</a>'),
Str::esc('<a href="/test">Test !@#$%^&*()_+\/</a>'),
);
}

public function testEscXML(): void
{
isSame(
'&lt;a href=&quot;/test&quot;&gt;Test!@#$%^&amp;*()_+\/&lt;/a&gt;',
Str::escXml('<a href="/test">Test!@#$%^&*()_+\\/</a>'),
Str::escXml('<a href="/test">Test!@#$%^&*()_+\/</a>'),
);
}

Expand Down Expand Up @@ -333,10 +333,10 @@ public function testTestName2Human(): void
isSame('Function IE Test', Str::testName2Human('Test_FunctionIE_TestTest'));
isSame('Test Function IE Test', Str::testName2Human('Test_testFunctionIE_TestTest'));

isSame('Function IE', Str::testName2Human('\\JBZoo\\Test_FunctionIE_Test'));
isSame('Function IE', Str::testName2Human('\\JBZoo\\PHPHunit\\Test_FunctionIE_Test'));
isSame('Function IE', Str::testName2Human('\\JBZoo\\PHPHunit\\Some\\Test_FunctionIE_Test'));
isSame('Function IE', Str::testName2Human('\\JBZoo\\PHPHunit\\Some\\Some\\Test_FunctionIE_Test'));
isSame('Function IE', Str::testName2Human('\JBZoo\Test_FunctionIE_Test'));
isSame('Function IE', Str::testName2Human('\JBZoo\PHPHunit\Test_FunctionIE_Test'));
isSame('Function IE', Str::testName2Human('\JBZoo\PHPHunit\Some\Test_FunctionIE_Test'));
isSame('Function IE', Str::testName2Human('\JBZoo\PHPHunit\Some\Some\Test_FunctionIE_Test'));
}

public function testGenerateUUID(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/SysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testGetDocumentRoot(): void
$_SERVER['DOCUMENT_ROOT'] = '../../';
isSame(\realpath('../../'), Sys::getDocRoot());

$_SERVER['DOCUMENT_ROOT'] = __DIR__ . '\\..\\';
$_SERVER['DOCUMENT_ROOT'] = __DIR__ . '\..\\';
isSame(PROJECT_ROOT, Sys::getDocRoot());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function testEscape(): void
{
isSame(
'&lt;a href=&quot;/test&quot;&gt;Test!@#$%^&amp;*()_+\/&lt;/a&gt;',
Xml::escape('<a href="/test">Test!@#$%^&*()_+\\/</a>'),
Xml::escape('<a href="/test">Test!@#$%^&*()_+\/</a>'),
);
}

Expand Down

0 comments on commit 346c3b2

Please sign in to comment.