Skip to content

Commit

Permalink
Allow _prefix in symfony coding standard
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 24, 2024
1 parent 5f364b9 commit ee25c5d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Rules/File/FileNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
private string $case = self::SNAKE_CASE,
private ?string $baseDirectory = null,
private array $ignoredSubDirectories = [],
private string $allowedPrefix = '',
) {
}

Expand All @@ -36,6 +37,7 @@ public function getConfiguration(): array
'case' => $this->case,
'baseDirectory' => $this->baseDirectory,
'ignoredSubDirectories' => $this->ignoredSubDirectories,
'allowedPrefix' => $this->allowedPrefix,
];
}

Expand All @@ -59,6 +61,12 @@ protected function process(int $tokenPosition, array $tokens): void
// in order to avoid conflict with some file extensions.
$fileName = explode('.', FileHelper::removeDot($fileName))[0];

$prefix = '';
if (str_starts_with($fileName, $this->allowedPrefix)) {
$prefix = $this->allowedPrefix;
$fileName = substr($fileName, \strlen($this->allowedPrefix));
}

$expected = match ($this->case) {
self::SNAKE_CASE => StringUtil::toSnakeCase($fileName),
self::CAMEL_CASE => StringUtil::toCamelCase($fileName),
Expand All @@ -68,7 +76,7 @@ protected function process(int $tokenPosition, array $tokens): void

if ($expected !== $fileName) {
$this->addFileError(
sprintf('The file name must use %s; expected %s.', $this->case, $expected),
sprintf('The file name must use %s; expected %s.', $this->case, $prefix.$expected),
$token,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Standard/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
*
* @see https://twig.symfony.com/doc/3.x/coding_standards.html
* @see https://symfony.com/doc/current/templates.html#template-naming
* @see https://symfony.com/doc/current/best_practices.html#templates
*/
final class Symfony implements StandardInterface
{
public function getRules(): array
{
return [
...(new Twig())->getRules(),
new FileNameRule(baseDirectory: 'templates', ignoredSubDirectories: ['bundles']),
new FileNameRule(baseDirectory: 'templates', ignoredSubDirectories: ['bundles'], allowedPrefix: '_'),
new DirectoryNameRule(baseDirectory: 'templates', ignoredSubDirectories: ['bundles']),
new FileExtensionRule(),
];
Expand Down
6 changes: 6 additions & 0 deletions tests/Rules/File/FileName/FileNameRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,10 @@ public function testRuleIgnoredPath(): void
{
$this->checkRule(new FileNameRule(baseDirectory: __DIR__.'/..', ignoredSubDirectories: ['FileName']), []);
}

public function testRuleAllowedPrefix(): void
{
$this->checkRule(new FileNameRule(), ['FileName.Error'], __DIR__.'/_file_name_rule_test.twig');
$this->checkRule(new FileNameRule(allowedPrefix: '_'), [], __DIR__.'/_file_name_rule_test.twig');
}
}
1 change: 1 addition & 0 deletions tests/Rules/File/FileName/_file_name_rule_test.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nothing

0 comments on commit ee25c5d

Please sign in to comment.