Skip to content

Commit

Permalink
Improve file errors
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 20, 2024
1 parent 9400431 commit f11f396
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 19 deletions.
70 changes: 60 additions & 10 deletions src/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,57 @@ protected function findPrevious(int|string|array $type, array $tokens, int $star

protected function addWarning(string $message, Token $token, ?string $messageId = null): bool
{
return $this->addMessage(Violation::LEVEL_WARNING, $message, $token, $messageId);
return $this->addMessage(
Violation::LEVEL_WARNING,
$message,
$token->getFilename(),
$token->getLine(),
$token->getPosition(),
$messageId,
);
}

protected function addFileWarning(string $message, Token $token, ?string $messageId = null): bool
{
return $this->addMessage(
Violation::LEVEL_WARNING,
$message,
$token->getFilename(),
null,
null,
$messageId,
);
}

protected function addError(string $message, Token $token, ?string $messageId = null): bool
{
return $this->addMessage(Violation::LEVEL_ERROR, $message, $token, $messageId);
return $this->addMessage(
Violation::LEVEL_ERROR,
$message,
$token->getFilename(),
$token->getLine(),
$token->getPosition(),
$messageId,
);
}

protected function addFixableWarning(string $message, Token $token, ?string $messageId = null): ?FixerInterface
protected function addFileError(string $message, Token $token, ?string $messageId = null): bool
{
return $this->addMessage(
Violation::LEVEL_ERROR,
$message,
$token->getFilename(),
null,
null,
$messageId,
);
}

protected function addFixableWarning(
string $message,
Token $token,
?string $messageId = null
): ?FixerInterface {
$added = $this->addWarning($message, $token, $messageId);
if (!$added) {
return null;
Expand All @@ -142,8 +183,11 @@ protected function addFixableWarning(string $message, Token $token, ?string $mes
return $this->fixer;
}

protected function addFixableError(string $message, Token $token, ?string $messageId = null): ?FixerInterface
{
protected function addFixableError(
string $message,
Token $token,
?string $messageId = null
): ?FixerInterface {
$added = $this->addError($message, $token, $messageId);
if (!$added) {
return null;
Expand All @@ -152,13 +196,19 @@ protected function addFixableError(string $message, Token $token, ?string $messa
return $this->fixer;
}

private function addMessage(int $messageType, string $message, Token $token, ?string $messageId = null): bool
{
private function addMessage(
int $messageType,
string $message,
string $fileName,
?int $line = null,
?int $position = null,
?string $messageId = null
): bool {
$id = new ViolationId(
$this->getShortName(),
$messageId ?? ucfirst(strtolower(Violation::getLevelAsString($messageType))),
$token->getLine(),
$token->getPosition(),
$line,
$position,
);
foreach ($this->ignoredViolations as $ignoredViolation) {
if ($ignoredViolation->match($id)) {
Expand All @@ -171,7 +221,7 @@ private function addMessage(int $messageType, string $message, Token $token, ?st
$violation = new Violation(
$messageType,
$message,
$token->getFilename(),
$fileName,
$this->getName(),
$id,
);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/File/DirectoryNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function process(int $tokenPosition, array $tokens): void
};

if ($expected !== $directory) {
$this->addError(
$this->addFileError(
sprintf('The directory name must use %s ; expected %s.', $this->case, $expected),
$token,
);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/File/FileNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function process(int $tokenPosition, array $tokens): void
};

if ($expected !== $fileName) {
$this->addError(
$this->addFileError(
sprintf('The file name must use %s ; expected %s.', $this->case, $expected),
$token,
);
Expand Down
4 changes: 2 additions & 2 deletions tests/Rules/File/DirectoryName/DirectoryNameRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testRuleInvalidTemplatesDirectory(): void
{
$this->checkRule(
new DirectoryNameRule(baseDirectory: 'templates'),
['DirectoryName.Error:1:1'],
['DirectoryName.Error'],
__DIR__.'/templates/directoryNameRuleTest/DirectoryNameRuleTest.twig'
);
}
Expand All @@ -64,7 +64,7 @@ public function testRulePascalCase(): void

public function testRuleInvalidDirectory(): void
{
$this->checkRule(new DirectoryNameRule(baseDirectory: 'File'), ['DirectoryName.Error:1:1']);
$this->checkRule(new DirectoryNameRule(baseDirectory: 'File'), ['DirectoryName.Error']);
}

public function testRuleKebabCase(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Rules/File/FileName/FileNameRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testConfiguration(): void
public function testRule(): void
{
$this->checkRule(new FileNameRule(), [
'FileName.Error:1:1',
'FileName.Error',
]);
}

Expand Down Expand Up @@ -69,7 +69,7 @@ public function testRuleValidFileWithDot(): void
public function testRuleBaseDir(): void
{
$this->checkRule(new FileNameRule(baseDirectory: 'File'), [
'FileName.Error:1:1',
'FileName.Error',
]);
}

Expand Down
8 changes: 5 additions & 3 deletions tests/Rules/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ protected function process(int $tokenPosition, array $tokens): void

if (0 === $tokenPosition) {
$this->addWarning('Fake Warning', $token);
$this->addError('Fake Error', $token);
$this->addFileWarning('Fake File Warning', $token);
$this->addError('Fake File Error', $token);
$this->addFileError('Fake Error', $token);
$this->addFixableWarning('Fake fixable warning', $token);
$this->addFixableError('Fake fixable error', $token);
}
Expand All @@ -38,8 +40,8 @@ protected function process(int $tokenPosition, array $tokens): void

$rule->lintFile([new Token(Token::EOF_TYPE, 0, 0, 'fakeFile.html.twig')], $report);

static::assertSame(2, $report->getTotalWarnings());
static::assertSame(2, $report->getTotalErrors());
static::assertSame(3, $report->getTotalWarnings());
static::assertSame(3, $report->getTotalErrors());
}

public function testRuleName(): void
Expand Down

0 comments on commit f11f396

Please sign in to comment.