Skip to content

Commit

Permalink
Introduce SingleQuoteRule
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 17, 2024
1 parent 2523706 commit 6e5259c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Rules/String/SingleQuoteRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules\String;

use TwigCsFixer\Rules\AbstractFixableRule;
use TwigCsFixer\Token\Token;

/**
* Ensures that string use single quotes when possible.
*/
final class SingleQuoteRule extends AbstractFixableRule
{
protected function process(int $tokenPosition, array $tokens): void
{
$token = $tokens[$tokenPosition];
if (!$this->isTokenMatching($token, Token::STRING_TYPE)) {
return;
}

$content = $token->getValue();
if (
'"' !== $content[0]
// && (true === $this->configuration['strings_containing_single_quote_chars'] || !str_contains($content, "'"))
// regex: odd number of backslashes, not followed by double quote or dollar
) {
return;
}

$fixer = $this->addFixableError('String should be defined with single quotes.', $token);
if (null === $fixer) {
return;
}

$content = substr($content, 1, -1);
$content = str_replace(
['\\"', '\\#{', '#\\{', '\\\'', '\''],
['"', '#{', '#{', '\'', '\\\''],
$content);
$fixer->replaceToken($tokenPosition, '\''.$content.'\'');
}
}
11 changes: 11 additions & 0 deletions tests/Rules/String/SimpleQuote/SingleQuoteRuleTest.fixed.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'foo'
"foo"

{% set foo = 'foo' %}
{% set foo2 = 'foo' %}
{% set foo3 = '\'foo\'' %}
{% set foo4 = '\'foo\'' %}

{% set foo5 = "#{p.first}" %}
{% set foo6 = '#{p.first}' %}
{% set foo7 = '#{p.first}' %}
20 changes: 20 additions & 0 deletions tests/Rules/String/SimpleQuote/SingleQuoteRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace TwigCsFixer\Tests\Rules\String\SimpleQuote;

use TwigCsFixer\Rules\String\SingleQuoteRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

class SingleQuoteRuleTest extends AbstractRuleTestCase
{
public function testRule(): void
{
$this->checkRule(new SingleQuoteRule(), [
'SingleQuote.Error:5:15' => 'String should be defined with single quotes.',
'SingleQuote.Error:6:15' => 'String should be defined with single quotes.',
'SingleQuote.Error:7:15' => 'String should be defined with single quotes.',
'SingleQuote.Error:10:15' => 'String should be defined with single quotes.',
'SingleQuote.Error:11:15' => 'String should be defined with single quotes.',
]);
}
}
11 changes: 11 additions & 0 deletions tests/Rules/String/SimpleQuote/SingleQuoteRuleTest.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'foo'
"foo"

{% set foo = 'foo' %}
{% set foo2 = "foo" %}
{% set foo3 = "'foo'" %}
{% set foo4 = "\'foo\'" %}

{% set foo5 = "#{p.first}" %}
{% set foo6 = "\#{p.first}" %}
{% set foo7 = "#\{p.first}" %}

0 comments on commit 6e5259c

Please sign in to comment.