Skip to content

Commit

Permalink
RuleErrorBuilder::file() - file path needs to exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 19, 2023
1 parent d4ee76c commit c453f29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Rules/RuleErrorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function class_exists;
use function count;
use function implode;
use function is_file;
use function sprintf;

/**
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Rules/RuleErrorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

0 comments on commit c453f29

Please sign in to comment.