Skip to content

Commit

Permalink
Add VarNameRule
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 26, 2024
1 parent 76deacb commit 7139f95
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Rules/Variable/VariableNameRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules\Variable;

use Symfony\Component\String\UnicodeString;
use TwigCsFixer\Rules\AbstractRule;
use TwigCsFixer\Rules\ConfigurableRuleInterface;
use TwigCsFixer\Token\Token;

final class VariableNameRule extends AbstractRule implements ConfigurableRuleInterface
{
// Kebab case is not a valid case for variable names.
public const SNAKE_CASE = 'snake_case';
public const CAMEL_CASE = 'camelCase';
public const PASCAL_CASE = 'PascalCase';

/**
* @param self::* $case
*/
public function __construct(private string $case = self::SNAKE_CASE)
{
}

public function getConfiguration(): array
{
return [
'case' => $this->case,
];
}

protected function process(int $tokenPosition, array $tokens): void
{
$token = $tokens[$tokenPosition];

if (!$this->isTokenMatching($token, Token::BLOCK_NAME_TYPE, 'set')) {
return;
}

$nameTokenPosition = $this->findNext(Token::NAME_TYPE, $tokens, $tokenPosition + 1);
if (false === $nameTokenPosition) {
return;
}
$name = $tokens[$nameTokenPosition]->getValue();

$expected = match ($this->case) {
self::SNAKE_CASE => (new UnicodeString($name))->snake()->toString(),
self::CAMEL_CASE => (new UnicodeString($name))->camel()->toString(),
self::PASCAL_CASE => ucfirst((new UnicodeString($name))->camel()->toString()),
};

if ($expected !== $name) {
$this->addError(
sprintf('The var name must use %s; expected %s.', $this->case, $expected),
$token,
);
}
}
}
2 changes: 2 additions & 0 deletions src/Standard/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
use TwigCsFixer\Rules\Variable\VariableNameRule;

/**
* Standard from twig.
Expand All @@ -23,6 +24,7 @@ public function getRules(): array
new OperatorNameSpacingRule(),
new OperatorSpacingRule(),
new PunctuationSpacingRule(),
new VariableNameRule(),
];
}
}
49 changes: 49 additions & 0 deletions tests/Rules/Variable/VariableName/VariableNameRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Rules\Variable\VariableName;

use TwigCsFixer\Rules\Variable\VariableNameRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class VariableNameRuleTest extends AbstractRuleTestCase
{
public function testConfiguration(): void
{
static::assertSame(
['case' => VariableNameRule::SNAKE_CASE],
(new VariableNameRule())->getConfiguration()
);

static::assertSame(
['case' => VariableNameRule::PASCAL_CASE],
(new VariableNameRule(VariableNameRule::PASCAL_CASE))->getConfiguration()
);
}

public function testRule(): void
{
$this->checkRule(new VariableNameRule(), [
'VariableName.Error:2:4',
'VariableName.Error:4:4',
]);
}

public function testRuleCamelCase(): void
{
$this->checkRule(new VariableNameRule(VariableNameRule::CAMEL_CASE), [
'VariableName.Error:3:4',
'VariableName.Error:4:4',
]);
}

public function testRulePascalCase(): void
{
$this->checkRule(new VariableNameRule(VariableNameRule::PASCAL_CASE), [
'VariableName.Error:1:4',
'VariableName.Error:2:4',
'VariableName.Error:3:4',
]);
}
}
4 changes: 4 additions & 0 deletions tests/Rules/Variable/VariableName/VariableNameRuleTest.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% set foo = 'foo' %}
{% set fooBar = 'foo' %}
{% set foo_bar = 'foo' %}
{% set FooBar = 'foo' %}
2 changes: 2 additions & 0 deletions tests/Standard/SymfonyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
use TwigCsFixer\Rules\Variable\VariableNameRule;
use TwigCsFixer\Standard\Symfony;

final class SymfonyTest extends TestCase
Expand All @@ -24,6 +25,7 @@ public function testGetRules(): void
new OperatorNameSpacingRule(),
new OperatorSpacingRule(),
new PunctuationSpacingRule(),
new VariableNameRule(),
new FileNameRule(baseDirectory: 'templates', ignoredSubDirectories: ['bundles']),
new DirectoryNameRule(baseDirectory: 'templates', ignoredSubDirectories: ['bundles']),
], $standard->getRules());
Expand Down
2 changes: 2 additions & 0 deletions tests/Standard/TwigCsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
use TwigCsFixer\Rules\Punctuation\TrailingCommaSingleLineRule;
use TwigCsFixer\Rules\Variable\VariableNameRule;
use TwigCsFixer\Rules\Whitespace\BlankEOFRule;
use TwigCsFixer\Rules\Whitespace\EmptyLinesRule;
use TwigCsFixer\Rules\Whitespace\IndentRule;
Expand All @@ -28,6 +29,7 @@ public function testGetRules(): void
new OperatorNameSpacingRule(),
new OperatorSpacingRule(),
new PunctuationSpacingRule(),
new VariableNameRule(),
new BlankEOFRule(),
new BlockNameSpacingRule(),
new EmptyLinesRule(),
Expand Down
2 changes: 2 additions & 0 deletions tests/Standard/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
use TwigCsFixer\Rules\Variable\VariableNameRule;
use TwigCsFixer\Standard\Twig;

final class TwigTest extends TestCase
Expand All @@ -22,6 +23,7 @@ public function testGetRules(): void
new OperatorNameSpacingRule(),
new OperatorSpacingRule(),
new PunctuationSpacingRule(),
new VariableNameRule(),
], $standard->getRules());
}
}

0 comments on commit 7139f95

Please sign in to comment.