Skip to content

Commit 9035154

Browse files
committed
Add identifier in rule
1 parent b723cf2 commit 9035154

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,6 @@ parameters:
576576
count: 1
577577
path: src/Rules/Generics/TemplateTypeCheck.php
578578

579-
-
580-
message: "#^Method PHPStan\\\\Rules\\\\Keywords\\\\RequireFileExistsRule\\:\\:processNode\\(\\) should return list\\<PHPStan\\\\Rules\\\\IdentifierRuleError\\> but returns array\\{PHPStan\\\\Rules\\\\RuleError\\}\\.$#"
581-
count: 1
582-
path: src/Rules/Keywords/RequireFileExistsRule.php
583-
584579
-
585580
message: "#^Function class_implements\\(\\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\\. Use objects retrieved from ReflectionProvider instead\\.$#"
586581
count: 1

src/Rules/Keywords/RequireFileExistsRule.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\Include_;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\IdentifierRuleError;
89
use PHPStan\Rules\Rule;
9-
use PHPStan\Rules\RuleError;
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\ShouldNotHappenException;
1212
use function is_file;
1313
use function is_string;
1414
use function sprintf;
15+
use function str_replace;
1516

1617
/**
1718
* @implements Rule<Include_>
@@ -36,31 +37,36 @@ public function processNode(Node $node, Scope $scope): array
3637
return [];
3738
}
3839

39-
private function getErrorMessage(Include_ $node, string $filePath): RuleError
40+
private function getErrorMessage(Include_ $node, string $filePath): IdentifierRuleError
4041
{
42+
$message = 'Path in %s() "%s" is not a file or it does not exist.';
43+
4144
switch ($node->type) {
4245
case Include_::TYPE_REQUIRE:
43-
$message = 'Path in require() "%s" is not a file or it does not exist.';
46+
$type = 'require';
4447
break;
4548
case Include_::TYPE_REQUIRE_ONCE:
46-
$message = 'Path in require_once() "%s" is not a file or it does not exist.';
49+
$type = 'require_once';
4750
break;
4851
case Include_::TYPE_INCLUDE:
49-
$message = 'Path in include() "%s" is not a file or it does not exist.';
52+
$type = 'include';
5053
break;
5154
case Include_::TYPE_INCLUDE_ONCE:
52-
$message = 'Path in include_once() "%s" is not a file or it does not exist.';
55+
$type = 'include_once';
5356
break;
5457
default:
5558
throw new ShouldNotHappenException('Rule should have already validated the node type.');
5659
}
5760

61+
$identifier = sprintf('%s.fileNotFound', str_replace('_once', 'Once', $type));
62+
5863
return RuleErrorBuilder::message(
5964
sprintf(
6065
$message,
66+
$type,
6167
$filePath,
6268
),
63-
)->build();
69+
)->nonIgnorable()->identifier($identifier)->build();
6470
}
6571

6672
private function resolveFilePath(Include_ $node, Scope $scope): ?string

0 commit comments

Comments
 (0)