From f0f2862d77400721f9ae7b956902271a2a0c51d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gamez?= Date: Fri, 16 Dec 2022 11:30:46 +0100 Subject: [PATCH] Fixed symlinks not being properly resolved --- CHANGELOG.md | 2 ++ src/Json.php | 21 +++++++-------------- tests/DecodeJsonTest.php | 7 +++++++ tests/symlinked/symlinked.json | 1 + 4 files changed, 17 insertions(+), 14 deletions(-) create mode 120000 tests/symlinked/symlinked.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 21cde62..3e8fd4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* Fixed symlinks not being properly resolved + ## [1.2.0] - 2022-12-01 * Added more checks when decoding JSON Files diff --git a/src/Json.php b/src/Json.php index bb3e273..4df2399 100644 --- a/src/Json.php +++ b/src/Json.php @@ -40,29 +40,22 @@ public static function decode(string $json, ?bool $forceArray = null): mixed */ public static function decodeFile(string $path, bool $forceArray = null): mixed { - $fileInfo = new SplFileInfo($path); - - if ($fileInfo->isLink() && $linkTarget = $fileInfo->getLinkTarget()) { - $fileInfo = new SplFileInfo($linkTarget); - } - - if (!$fileInfo->isFile()) { - throw new UnexpectedValueException("`$path` does not point to a file."); + if (!is_readable($path)) { + throw new UnexpectedValueException("The file at '$path' is not readable"); } - if (!$fileInfo->isReadable()) { - throw new UnexpectedValueException("`$path` is not readable."); + if (is_dir($path)) { + throw new UnexpectedValueException("'$path' points to a directory"); } - $file = $fileInfo->openFile(); - $contents = $file->fread($file->getSize()); + $contents = file_get_contents($path); if ($contents === false) { - throw new UnexpectedValueException("Unable to read contents of `$path`"); + throw new UnexpectedValueException("The file at '$path' is not readable"); } if ($contents === '') { - throw new UnexpectedValueException("The file at `$path` is empty"); + throw new UnexpectedValueException("The file at '$path' is empty"); } return self::decode($contents, $forceArray); diff --git a/tests/DecodeJsonTest.php b/tests/DecodeJsonTest.php index 78a1886..b60aa58 100644 --- a/tests/DecodeJsonTest.php +++ b/tests/DecodeJsonTest.php @@ -96,6 +96,13 @@ public function it_resolves_links(): void } finally { unlink($symlinkPath); } + } + /** @test */ + public function it_resolves_relative_links(): void + { + $path = __DIR__.'/symlinked/symlinked.json'; + + $this->assertIsObject(Json::decodeFile($path)); } } diff --git a/tests/symlinked/symlinked.json b/tests/symlinked/symlinked.json new file mode 120000 index 0000000..eb2d8f9 --- /dev/null +++ b/tests/symlinked/symlinked.json @@ -0,0 +1 @@ +../valid.json \ No newline at end of file