From c453f29f60992167a5aa46d4926c18a420ae7b26 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 19 Aug 2023 13:15:48 +0200 Subject: [PATCH] `RuleErrorBuilder::file()` - file path needs to exist --- src/Rules/RuleErrorBuilder.php | 4 ++++ tests/PHPStan/Rules/RuleErrorBuilderTest.php | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Rules/RuleErrorBuilder.php b/src/Rules/RuleErrorBuilder.php index 443f8ec798..35c4d4c253 100644 --- a/src/Rules/RuleErrorBuilder.php +++ b/src/Rules/RuleErrorBuilder.php @@ -8,6 +8,7 @@ use function class_exists; use function count; use function implode; +use function is_file; use function sprintf; /** @@ -143,6 +144,9 @@ public function line(int $line): self */ public function file(string $file, ?string $fileDescription = null): self { + if (!is_file($file)) { + throw new ShouldNotHappenException(sprintf('File %s does not exist.', $file)); + } $this->properties['file'] = $file; $this->properties['fileDescription'] = $fileDescription ?? $file; $this->type |= self::TYPE_FILE; diff --git a/tests/PHPStan/Rules/RuleErrorBuilderTest.php b/tests/PHPStan/Rules/RuleErrorBuilderTest.php index bb59e928b6..59826f7172 100644 --- a/tests/PHPStan/Rules/RuleErrorBuilderTest.php +++ b/tests/PHPStan/Rules/RuleErrorBuilderTest.php @@ -26,24 +26,24 @@ public function testMessageAndLineAndBuild(): void public function testMessageAndFileAndBuild(): void { - $builder = RuleErrorBuilder::message('Foo')->file('Bar.php'); + $builder = RuleErrorBuilder::message('Foo')->file(__FILE__); $ruleError = $builder->build(); $this->assertSame('Foo', $ruleError->getMessage()); $this->assertInstanceOf(FileRuleError::class, $ruleError); // @phpstan-ignore method.alreadyNarrowedType - $this->assertSame('Bar.php', $ruleError->getFile()); + $this->assertSame(__FILE__, $ruleError->getFile()); } public function testMessageAndLineAndFileAndBuild(): void { - $builder = RuleErrorBuilder::message('Foo')->line(25)->file('Bar.php'); + $builder = RuleErrorBuilder::message('Foo')->line(25)->file(__FILE__); $ruleError = $builder->build(); $this->assertSame('Foo', $ruleError->getMessage()); $this->assertInstanceOf(LineRuleError::class, $ruleError); // @phpstan-ignore method.alreadyNarrowedType $this->assertInstanceOf(FileRuleError::class, $ruleError); // @phpstan-ignore method.alreadyNarrowedType $this->assertSame(25, $ruleError->getLine()); - $this->assertSame('Bar.php', $ruleError->getFile()); + $this->assertSame(__FILE__, $ruleError->getFile()); } }