Skip to content

Commit 346c3b2

Browse files
authored
Fix escaping of backslashes in regex and test cases (#53)
1 parent 15d5e40 commit 346c3b2

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

src/Arr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function unique(array $array, bool $keepKeys = false): array
3838

3939
$uniqueArray[$value] = $key;
4040
}
41-
41+
4242
return \array_flip($uniqueArray);
4343
}
4444

src/FS.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ public static function openFile(string $filepath): ?string
173173

174174
$realPath = \realpath($filepath);
175175
if ($realPath !== false) {
176-
$handle = \fopen($realPath, 'r');
176+
$handle = \fopen($realPath, 'r');
177+
$fileSize = (int)\filesize($realPath);
178+
if ($fileSize === 0) {
179+
return null;
180+
}
181+
177182
if ($handle !== false) {
178-
$contents = (string)\fread($handle, (int)\filesize($realPath));
183+
$contents = (string)\fread($handle, $fileSize);
179184
\fclose($handle);
180185
}
181186
}
@@ -370,9 +375,9 @@ public static function clean(?string $path, string $dirSep = \DIRECTORY_SEPARATO
370375
$path = \trim((string)$path);
371376

372377
if (($dirSep === '\\') && ($path[0] === '\\') && ($path[1] === '\\')) {
373-
$path = '\\' . \preg_replace('#[/\\\\]+#', $dirSep, $path);
378+
$path = '\\' . \preg_replace('#[/\\\]+#', $dirSep, $path);
374379
} else {
375-
$path = (string)\preg_replace('#[/\\\\]+#', $dirSep, $path);
380+
$path = (string)\preg_replace('#[/\\\]+#', $dirSep, $path);
376381
}
377382

378383
return $path;

tests/FileSystemTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ public function testClean(): void
394394
isSame('/path/path', FS::clean('///path///path'));
395395
isSame('/path/path/path', FS::clean('///path///path/path'));
396396
isSame('/path/path/path/', FS::clean('\path\path\path\\\\\\\\'));
397-
isSame('\\path\\path\\path\\', FS::clean('\path\path\path\\\\\\\\', '\\'));
398-
isSame('\\path\\path\\path\\', FS::clean('\\path\\path\\path\\\\\\\\', '\\'));
399-
isSame('\\\\path\\path\\path\\', FS::clean('\\\\path\\path\\path\\\\\\\\', '\\'));
397+
isSame('\path\path\path\\', FS::clean('\path\path\path\\\\\\\\', '\\'));
398+
isSame('\path\path\path\\', FS::clean('\path\path\path\\\\\\\\', '\\'));
399+
isSame('\\\path\path\path\\', FS::clean('\\\path\path\path\\\\\\\\', '\\'));
400400

401401
isSame('../../path/', FS::clean('..///..///path/', '/'));
402402
isSame('./../path/', FS::clean('.///..///path/', '/'));
@@ -438,10 +438,10 @@ public function testGetRelative(): void
438438

439439
$root = __DIR__ . '/..';
440440
isSame('tests/FileSystemTest.php', FS::getRelative($file, $root, '/'));
441-
isSame('tests\\FileSystemTest.php', FS::getRelative($file, $root, '\\'));
441+
isSame('tests\FileSystemTest.php', FS::getRelative($file, $root, '\\'));
442442

443443
$root = null;
444444
isSame('tests/FileSystemTest.php', FS::getRelative($file, $root, '/'));
445-
isSame('tests\\FileSystemTest.php', FS::getRelative($file, $root, '\\'));
445+
isSame('tests\FileSystemTest.php', FS::getRelative($file, $root, '\\'));
446446
}
447447
}

tests/StringTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testClean(): void
7474
$input = ' <b>ASDF</b> !@#$%^&*()_+"\';:>< ';
7575

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

8080
public function testParseLines(): void
@@ -272,15 +272,15 @@ public function testEsc(): void
272272
{
273273
isSame(
274274
'&lt;a href="/test"&gt;Test !@#$%^&amp;*()_+\/&lt;/a&gt;',
275-
Str::esc('<a href="/test">Test !@#$%^&*()_+\\/</a>'),
275+
Str::esc('<a href="/test">Test !@#$%^&*()_+\/</a>'),
276276
);
277277
}
278278

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

@@ -333,10 +333,10 @@ public function testTestName2Human(): void
333333
isSame('Function IE Test', Str::testName2Human('Test_FunctionIE_TestTest'));
334334
isSame('Test Function IE Test', Str::testName2Human('Test_testFunctionIE_TestTest'));
335335

336-
isSame('Function IE', Str::testName2Human('\\JBZoo\\Test_FunctionIE_Test'));
337-
isSame('Function IE', Str::testName2Human('\\JBZoo\\PHPHunit\\Test_FunctionIE_Test'));
338-
isSame('Function IE', Str::testName2Human('\\JBZoo\\PHPHunit\\Some\\Test_FunctionIE_Test'));
339-
isSame('Function IE', Str::testName2Human('\\JBZoo\\PHPHunit\\Some\\Some\\Test_FunctionIE_Test'));
336+
isSame('Function IE', Str::testName2Human('\JBZoo\Test_FunctionIE_Test'));
337+
isSame('Function IE', Str::testName2Human('\JBZoo\PHPHunit\Test_FunctionIE_Test'));
338+
isSame('Function IE', Str::testName2Human('\JBZoo\PHPHunit\Some\Test_FunctionIE_Test'));
339+
isSame('Function IE', Str::testName2Human('\JBZoo\PHPHunit\Some\Some\Test_FunctionIE_Test'));
340340
}
341341

342342
public function testGenerateUUID(): void

tests/SysTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testGetDocumentRoot(): void
8989
$_SERVER['DOCUMENT_ROOT'] = '../../';
9090
isSame(\realpath('../../'), Sys::getDocRoot());
9191

92-
$_SERVER['DOCUMENT_ROOT'] = __DIR__ . '\\..\\';
92+
$_SERVER['DOCUMENT_ROOT'] = __DIR__ . '\..\\';
9393
isSame(PROJECT_ROOT, Sys::getDocRoot());
9494
}
9595

tests/XmlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function testEscape(): void
263263
{
264264
isSame(
265265
'&lt;a href=&quot;/test&quot;&gt;Test!@#$%^&amp;*()_+\/&lt;/a&gt;',
266-
Xml::escape('<a href="/test">Test!@#$%^&*()_+\\/</a>'),
266+
Xml::escape('<a href="/test">Test!@#$%^&*()_+\/</a>'),
267267
);
268268
}
269269

0 commit comments

Comments
 (0)