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