-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleIdentifierConventionTest.php
More file actions
62 lines (50 loc) · 2.08 KB
/
Copy pathRuleIdentifierConventionTest.php
File metadata and controls
62 lines (50 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types = 1);
namespace ScriptDevelopment\PhpstanWarroomRules\Tests\Rules;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use function sprintf;
/**
* Locks the package's own rule-author convention: every
* `RuleErrorBuilder::message()->identifier(...)` call in `src/Rules/*.php` must
* use a `cameLCase.cameLCase` identifier so consumers see a uniform shape across
* every rule the package ships.
*
* Doctrine source: ADR-0021 §Identifier convention.
*
* Like RuleDocblockContractTest, this enforces rule-authoring discipline (a
* lexical contract on identifier strings), not rule enforcement.
*/
final class RuleIdentifierConventionTest extends TestCase
{
private const string IDENTIFIER_PATTERN = '/->identifier\(\s*[\'"]([^\'"]+)[\'"]\s*\)/';
private const string CONVENTION_PATTERN = '/^[a-z][a-zA-Z0-9]*\.[a-z][a-zA-Z0-9]*$/';
#[Test]
public function every_rule_identifier_follows_camel_dot_camel_convention(): void
{
$ruleFiles = glob(__DIR__ . '/../../src/Rules/*.php');
self::assertNotEmpty($ruleFiles, 'No rule files found under src/Rules');
$allIdentifiers = [];
foreach ($ruleFiles as $file) {
$source = file_get_contents($file);
self::assertNotFalse($source, "Could not read {$file}");
preg_match_all(self::IDENTIFIER_PATTERN, $source, $matches);
foreach ($matches[1] as $identifier) {
$allIdentifiers[] = [$file, $identifier];
self::assertMatchesRegularExpression(
self::CONVENTION_PATTERN,
$identifier,
sprintf(
'Identifier "%s" in %s does not follow ADR-0021 §Identifier convention (cameLCase.cameLCase).',
$identifier,
basename($file),
),
);
}
}
self::assertNotEmpty(
$allIdentifiers,
'No identifiers found across rule files — regex broken or rules lack ->identifier() calls.',
);
}
}