From 26a9decabaee30ffac1bb77271ead85f6e1a7c86 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sun, 14 Sep 2025 12:54:38 +0200 Subject: [PATCH] [BUGFIX] Use safe file functions in `ParserTest` Part of #1168 --- tests/ParserTest.php | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index f6425a0b1..b80280a77 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -30,6 +30,9 @@ use Sabberworm\CSS\Value\URL; use Sabberworm\CSS\Value\ValueList; +use function Safe\file_get_contents; +use function Safe\opendir; + /** * @covers \Sabberworm\CSS\Parser */ @@ -58,25 +61,26 @@ public function parseForOneDeclarationBlockReturnsDocumentWithOneDeclarationBloc public function files(): void { $directory = __DIR__ . '/fixtures'; - if ($directoryHandle = \opendir($directory)) { - /* This is the correct way to loop over the directory. */ - while (false !== ($filename = \readdir($directoryHandle))) { - if (\strpos($filename, '.') === 0) { - continue; - } - if (\strrpos($filename, '.css') !== \strlen($filename) - \strlen('.css')) { - continue; - } - if (\strpos($filename, '-') === 0) { - // Either a file which SHOULD fail (at least in strict mode) - // or a future test of an as-of-now missing feature - continue; - } - $parser = new Parser(\file_get_contents($directory . '/' . $filename)); - self::assertNotSame('', $parser->parse()->render()); + $directoryHandle = opendir($directory); + + /* This is the correct way to loop over the directory. */ + while (false !== ($filename = \readdir($directoryHandle))) { + if (\strpos($filename, '.') === 0) { + continue; } - \closedir($directoryHandle); + if (\strrpos($filename, '.css') !== \strlen($filename) - \strlen('.css')) { + continue; + } + if (\strpos($filename, '-') === 0) { + // Either a file which SHOULD fail (at least in strict mode) + // or a future test of an as-of-now missing feature + continue; + } + $parser = new Parser(file_get_contents($directory . '/' . $filename)); + self::assertNotSame('', $parser->parse()->render()); } + + \closedir($directoryHandle); } /** @@ -943,7 +947,7 @@ public function missingPropertyValueLenient(): void public static function parsedStructureForFile($filename, $settings = null): Document { $filename = __DIR__ . "/fixtures/$filename.css"; - $parser = new Parser(\file_get_contents($filename), $settings); + $parser = new Parser(file_get_contents($filename), $settings); return $parser->parse(); }