-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d79179
commit e5be674
Showing
10 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
tests/Sniff/OperatorNameSpacing/OperatorNameSpacingTest.fixed.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
tests/Sniff/OperatorNameSpacing/OperatorNameSpacingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters