Skip to content

Commit

Permalink
Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 17, 2024
1 parent 63ca2e7 commit 6faa507
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 32 deletions.
4 changes: 2 additions & 2 deletions docs/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ The `TwigCsFixer\Token\Tokenizer` transform the file into a list of tokens which

The `#}` delimiter.

- **TwigCsFixer\Token\Token::NAMED_ARGUMENT_OPERATOR_TYPE**:
- **TwigCsFixer\Token\Token::NAMED_ARGUMENT_SEPARATOR_TYPE**:

The `=` or `:` operator used when using named argument. Like `{{ foo(bar=true, baz: false) }}`.
The `=` or `:` separator used when using named argument. Like `{{ foo(bar=true, baz: false) }}`.

Then, the easiest way to write a custom rule is to implement the `TwigCsFixer\Rules\AbstractRule` class
or the `TwigCsFixer\Rules\AbstractFixableRule` if the rule can be automatically fixed.
Expand Down
2 changes: 1 addition & 1 deletion docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- `spaceRatio`: how many spaces replace a tab (default 4).
- `useTab`: indentation must be done with tab (default false).

- **NamedArgumentOperatorRule**:
- **NamedArgumentSeparatorRule**:

Ensures named arguments use `:` syntax instead of `=` (For `twig/twig >= 3.12.0`).

Expand Down
1 change: 0 additions & 1 deletion src/Environment/StubbedEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace TwigCsFixer\Environment;

use Composer\InstalledVersions;
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

declare(strict_types=1);

namespace TwigCsFixer\Rules\Operator;
namespace TwigCsFixer\Rules\Function;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Environment\StubbedEnvironment;
use TwigCsFixer\Rules\AbstractFixableRule;
use TwigCsFixer\Token\Token;
Expand All @@ -14,7 +12,7 @@
/**
* Ensures named arguments use `:` syntax instead of `=` (For `twig/twig >= 3.12.0`).
*/
final class NamedArgumentOperatorRule extends AbstractFixableRule
final class NamedArgumentSeparatorRule extends AbstractFixableRule
{
public function __construct()
{
Expand All @@ -26,7 +24,7 @@ public function __construct()
protected function process(int $tokenIndex, Tokens $tokens): void
{
$token = $tokens->get($tokenIndex);
if (!$token->isMatching(Token::NAMED_ARGUMENT_OPERATOR_TYPE)) {
if (!$token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE)) {
return;
}

Expand All @@ -35,7 +33,7 @@ protected function process(int $tokenIndex, Tokens $tokens): void
}

$fixer = $this->addFixableError(
\sprintf('Named arguments should be declared with the operator "%s".', ':'),
\sprintf('Named arguments should be declared with the separator "%s".', ':'),
$token
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TwigCsFixer\Rules\Operator;
namespace TwigCsFixer\Rules\Function;

use TwigCsFixer\Rules\AbstractSpacingRule;
use TwigCsFixer\Token\Token;
Expand All @@ -16,7 +16,7 @@ final class NamedArgumentSpacingRule extends AbstractSpacingRule
protected function getSpaceBefore(int $tokenIndex, Tokens $tokens): ?int
{
$token = $tokens->get($tokenIndex);
if ($token->isMatching(Token::NAMED_ARGUMENT_OPERATOR_TYPE)) {
if ($token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE)) {
return 0;
}

Expand All @@ -26,10 +26,10 @@ protected function getSpaceBefore(int $tokenIndex, Tokens $tokens): ?int
protected function getSpaceAfter(int $tokenIndex, Tokens $tokens): ?int
{
$token = $tokens->get($tokenIndex);
if ($token->isMatching(Token::NAMED_ARGUMENT_OPERATOR_TYPE, '=')) {
if ($token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, '=')) {
return 0;
}
if ($token->isMatching(Token::NAMED_ARGUMENT_OPERATOR_TYPE, ':')) {
if ($token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, ':')) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Standard/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace TwigCsFixer\Standard;

use TwigCsFixer\Rules\Delimiter\DelimiterSpacingRule;
use TwigCsFixer\Rules\Operator\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Function\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
Expand Down
2 changes: 1 addition & 1 deletion src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class Token
public const COMMENT_TAB_TYPE = 'COMMENT_TAB_TYPE';
public const COMMENT_EOL_TYPE = 'COMMENT_EOL_TYPE';
public const COMMENT_END_TYPE = 'COMMENT_END_TYPE';
public const NAMED_ARGUMENT_OPERATOR_TYPE = 'NAMED_ARGUMENT_OPERATOR_TYPE';
public const NAMED_ARGUMENT_SEPARATOR_TYPE = 'NAMED_ARGUMENT_SEPARATOR_TYPE';

public const WHITESPACE_TOKENS = [
self::WHITESPACE_TYPE => self::WHITESPACE_TYPE,
Expand Down
8 changes: 4 additions & 4 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ private function lexOperator(string $operator): void
) {
$bracket = end($this->bracketsAndTernary);
if (false !== $bracket && '(' === $bracket->getValue()) {
// This is a named argument operator instead
$this->pushToken(Token::NAMED_ARGUMENT_OPERATOR_TYPE, $operator);
// This is a named argument separator instead
$this->pushToken(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, $operator);

return;
}
Expand Down Expand Up @@ -699,8 +699,8 @@ private function lexPunctuation(): void
return;
}
if ('(' === $bracket->getValue()) {
// This is a named argument operator instead
$this->pushToken(Token::NAMED_ARGUMENT_OPERATOR_TYPE, $currentCode);
// This is a named argument separator instead
$this->pushToken(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, $currentCode);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

declare(strict_types=1);

namespace TwigCsFixer\Tests\Rules\Operator\NamedArgumentOperator;
namespace TwigCsFixer\Tests\Rules\Function;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Operator\NamedArgumentOperatorRule;
use TwigCsFixer\Rules\Function\NamedArgumentSeparatorRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class NamedArgumentOperatorRuleTest extends AbstractRuleTestCase
final class NamedArgumentSeparatorRuleTest extends AbstractRuleTestCase
{
public function testRule(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.12.0')) {
$this->expectException(\InvalidArgumentException::class);
}

$this->checkRule(new NamedArgumentOperatorRule(), [
'NamedArgumentOperator.Error:1:11' => 'Named arguments should be declared with the operator ":".',
$this->checkRule(new NamedArgumentSeparatorRule(), [
'NamedArgumentSeparator.Error:1:11' => 'Named arguments should be declared with the separator ":".',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace TwigCsFixer\Tests\Rules\Operator\NamedArgumentSpacing;
namespace TwigCsFixer\Tests\Rules\Function\NamedArgumentSpacing;

use TwigCsFixer\Rules\Operator\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Function\NamedArgumentSpacingRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class NamedArgumentSpacingRuleTest extends AbstractRuleTestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/Standard/SymfonyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use TwigCsFixer\Rules\File\DirectoryNameRule;
use TwigCsFixer\Rules\File\FileExtensionRule;
use TwigCsFixer\Rules\File\FileNameRule;
use TwigCsFixer\Rules\Operator\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Function\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
Expand Down
2 changes: 1 addition & 1 deletion tests/Standard/TwigCsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use TwigCsFixer\Rules\Delimiter\BlockNameSpacingRule;
use TwigCsFixer\Rules\Delimiter\DelimiterSpacingRule;
use TwigCsFixer\Rules\Function\IncludeFunctionRule;
use TwigCsFixer\Rules\Operator\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Function\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
Expand Down
2 changes: 1 addition & 1 deletion tests/Standard/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use TwigCsFixer\Rules\Delimiter\DelimiterSpacingRule;
use TwigCsFixer\Rules\Operator\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Function\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
Expand Down
4 changes: 2 additions & 2 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ public static function tokenizeDataProvider(): iterable
12 => Token::FUNCTION_NAME_TYPE,
13 => Token::PUNCTUATION_TYPE,
14 => Token::NAME_TYPE,
15 => Token::NAMED_ARGUMENT_OPERATOR_TYPE,
15 => Token::NAMED_ARGUMENT_SEPARATOR_TYPE,
16 => Token::NUMBER_TYPE,
17 => Token::PUNCTUATION_TYPE,
18 => Token::WHITESPACE_TYPE,
Expand All @@ -718,7 +718,7 @@ public static function tokenizeDataProvider(): iterable
23 => Token::FUNCTION_NAME_TYPE,
24 => Token::PUNCTUATION_TYPE,
25 => Token::NAME_TYPE,
26 => Token::NAMED_ARGUMENT_OPERATOR_TYPE,
26 => Token::NAMED_ARGUMENT_SEPARATOR_TYPE,
27 => Token::NUMBER_TYPE,
28 => Token::PUNCTUATION_TYPE,
29 => Token::WHITESPACE_TYPE,
Expand Down

0 comments on commit 6faa507

Please sign in to comment.