Skip to content

Commit

Permalink
Add OperatorNameSpacingSniff
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Oct 25, 2023
1 parent 0d79179 commit e5be674
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/Sniff/OperatorNameSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Sniff;

use TwigCsFixer\Token\Token;

/**
* Ensure there is no consecutive spaces inside operator names.
*/
final class OperatorNameSpacingSniff extends AbstractSniff
{
protected function process(int $tokenPosition, array $tokens): void
{
$token = $tokens[$tokenPosition];
if (!$this->isTokenMatching($token, Token::OPERATOR_TYPE)) {
return;
}

$value = $token->getValue();
// Ignore multi lines operators
if (1 === preg_match('#\n#', $value)) {
return;
}

$newValue = preg_replace('#\s+#', ' ', $value);
if (!\is_string($newValue) || $value === $newValue) {
return;
}

$fixer = $this->addFixableError(
'A single line operator should not have consecutive spaces.',
$token
);

if (null === $fixer) {
return;
}

$fixer->replaceToken($tokenPosition, $newValue);
}
}
2 changes: 2 additions & 0 deletions src/Standard/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TwigCsFixer\Standard;

use TwigCsFixer\Sniff\DelimiterSpacingSniff;
use TwigCsFixer\Sniff\OperatorNameSpacingSniff;
use TwigCsFixer\Sniff\OperatorSpacingSniff;
use TwigCsFixer\Sniff\PunctuationSpacingSniff;

Expand All @@ -19,6 +20,7 @@ public function getSniffs(): array
{
return [
new DelimiterSpacingSniff(),
new OperatorNameSpacingSniff(),
new OperatorSpacingSniff(),
new PunctuationSpacingSniff(),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TwigCsFixer\Tests\Sniff\BlockNameSpacingSniff;
namespace TwigCsFixer\Tests\Sniff\BlockNameSpacing;

use TwigCsFixer\Sniff\BlockNameSpacingSniff;
use TwigCsFixer\Tests\Sniff\AbstractSniffTestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{ 1 + 2 }}
{{ 'array' starts with 'a' }}
{{ 'array' ends with 'a' }}
{{ false is not true }}
{{ false is
not true }}
20 changes: 20 additions & 0 deletions tests/Sniff/OperatorNameSpacing/OperatorNameSpacingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Sniff\OperatorNameSpacing;

use TwigCsFixer\Sniff\OperatorNameSpacingSniff;
use TwigCsFixer\Tests\Sniff\AbstractSniffTestCase;

final class OperatorNameSpacingTest extends AbstractSniffTestCase
{
public function testSniff(): void
{
$this->checkSniff(new OperatorNameSpacingSniff(), [
[2 => 13],
[3 => 13],
[4 => 10],
]);
}
}
6 changes: 6 additions & 0 deletions tests/Sniff/OperatorNameSpacing/OperatorNameSpacingTest.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{ 1 + 2 }}
{{ 'array' starts with 'a' }}
{{ 'array' ends with 'a' }}
{{ false is not true }}
{{ false is
not true }}
2 changes: 2 additions & 0 deletions tests/Standard/GenericTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TwigCsFixer\Sniff\DelimiterSpacingSniff;
use TwigCsFixer\Sniff\EmptyLinesSniff;
use TwigCsFixer\Sniff\IndentSniff;
use TwigCsFixer\Sniff\OperatorNameSpacingSniff;
use TwigCsFixer\Sniff\OperatorSpacingSniff;
use TwigCsFixer\Sniff\PunctuationSpacingSniff;
use TwigCsFixer\Sniff\TrailingCommaSingleLineSniff;
Expand All @@ -24,6 +25,7 @@ public function testGetSniffs(): void

static::assertEquals([
new DelimiterSpacingSniff(),
new OperatorNameSpacingSniff(),
new OperatorSpacingSniff(),
new PunctuationSpacingSniff(),
new BlankEOFSniff(),
Expand Down
2 changes: 2 additions & 0 deletions tests/Standard/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use TwigCsFixer\Sniff\DelimiterSpacingSniff;
use TwigCsFixer\Sniff\OperatorNameSpacingSniff;
use TwigCsFixer\Sniff\OperatorSpacingSniff;
use TwigCsFixer\Sniff\PunctuationSpacingSniff;
use TwigCsFixer\Standard\Twig;
Expand All @@ -18,6 +19,7 @@ public function testGetSniffs(): void

static::assertEquals([
new DelimiterSpacingSniff(),
new OperatorNameSpacingSniff(),
new OperatorSpacingSniff(),
new PunctuationSpacingSniff(),
], $standard->getSniffs());
Expand Down

0 comments on commit e5be674

Please sign in to comment.