Skip to content

Commit 1cf7d19

Browse files
authored
Merge pull request #19 from martijnengler/18-throw-from-parse-from-file
2 parents 1027827 + bad6ef1 commit 1cf7d19

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Comment.php

+4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ public static function parse(string $json, bool $assoc = false, int $depth = 512
204204

205205
public static function parseFromFile(string $file, bool $assoc = false, int $depth = 512, int $options = 0)
206206
{
207+
if (!is_file($file)) {
208+
throw new \InvalidArgumentException($file . ' does not exist or is not a file');
209+
}
210+
207211
$json = \file_get_contents($file);
208212

209213
return static::parse(\trim($json), $assoc, $depth, $options);

tests/CommentTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ public function testParseFromFile()
6868
$this->assertSame('adhocore/json-comment', $parsed['name']);
6969
}
7070

71+
public function testParseFromFileThrowsNotExists()
72+
{
73+
$file = 'does-not-exist.json';
74+
75+
$this->expectException(\InvalidArgumentException::class);
76+
$this->expectExceptionMessage($file . ' does not exist or is not a file');
77+
78+
Comment::parseFromFile($file, true);
79+
}
80+
81+
public function testParseFromFileThrowsNotFile()
82+
{
83+
$this->expectException(\InvalidArgumentException::class);
84+
$this->expectExceptionMessage(__DIR__ . ' does not exist or is not a file');
85+
86+
Comment::parseFromFile(__DIR__, true);
87+
}
88+
7189
public function testSubJson()
7290
{
7391
// https://github.com/adhocore/php-json-comment/issues/15

0 commit comments

Comments
 (0)