diff --git a/src/Rules/RuleErrorBuilder.php b/src/Rules/RuleErrorBuilder.php index 443f8ec79..35c4d4c25 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 bb59e928b..59826f717 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()); } }