-
-
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
2523706
commit 6e5259c
Showing
4 changed files
with
85 additions
and
0 deletions.
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\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
11
tests/Rules/String/SimpleQuote/SingleQuoteRuleTest.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,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}' %} |
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 | ||
|
||
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.', | ||
]); | ||
} | ||
} |
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,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}" %} |