-
-
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
6b75478
commit d3577e9
Showing
8 changed files
with
137 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
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Rules\String; | ||
|
||
use TwigCsFixer\Rules\AbstractFixableRule; | ||
use TwigCsFixer\Rules\ConfigurableRuleInterface; | ||
use TwigCsFixer\Token\Token; | ||
|
||
/** | ||
* Ensures that strings use single quotes when possible. | ||
*/ | ||
final class SingleQuoteRule extends AbstractFixableRule implements ConfigurableRuleInterface | ||
{ | ||
public function __construct(private bool $skipStringContainingSingleQuote = true) | ||
{ | ||
} | ||
|
||
public function getConfiguration(): array | ||
{ | ||
return [ | ||
'skip_string_containing_single_quote' => $this->skipStringContainingSingleQuote, | ||
]; | ||
} | ||
|
||
protected function process(int $tokenPosition, array $tokens): void | ||
{ | ||
$token = $tokens[$tokenPosition]; | ||
if (!$this->isTokenMatching($token, Token::STRING_TYPE)) { | ||
return; | ||
} | ||
|
||
$content = $token->getValue(); | ||
if ( | ||
!str_starts_with($content, '"') | ||
|| str_contains($content, '\'') && $this->skipStringContainingSingleQuote | ||
) { | ||
return; | ||
} | ||
|
||
$fixer = $this->addFixableError('String should be declared with single quotes.', $token); | ||
if (null === $fixer) { | ||
return; | ||
} | ||
|
||
$content = substr($content, 1, -1); | ||
$content = str_replace( | ||
['\\"', '\\#{', '#\\{', '\\\'', '\''], | ||
['"', '#{', '#{', '\'', '\\\''], | ||
$content | ||
); | ||
$fixer->replaceToken($tokenPosition, '\''.$content.'\''); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
tests/Rules/String/SimpleQuote/SingleQuoteRuleTest.all.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}' %} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Tests\Rules\String\SimpleQuote; | ||
|
||
use TwigCsFixer\Rules\String\SingleQuoteRule; | ||
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase; | ||
|
||
class SingleQuoteRuleTest extends AbstractRuleTestCase | ||
{ | ||
public function testConfiguration(): void | ||
{ | ||
static::assertSame( | ||
['skip_string_containing_single_quote' => true], | ||
(new SingleQuoteRule())->getConfiguration() | ||
); | ||
static::assertSame( | ||
['skip_string_containing_single_quote' => false], | ||
(new SingleQuoteRule(false))->getConfiguration() | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->checkRule(new SingleQuoteRule(), [ | ||
'SingleQuote.Error:5:15' => 'String should be declared with single quotes.', | ||
'SingleQuote.Error:10:15' => 'String should be declared with single quotes.', | ||
'SingleQuote.Error:11:15' => 'String should be declared with single quotes.', | ||
]); | ||
} | ||
|
||
public function testRuleWithoutSkippingSingleQuote(): void | ||
{ | ||
$this->checkRule(new SingleQuoteRule(false), [ | ||
'SingleQuote.Error:5:15' => 'String should be declared with single quotes.', | ||
'SingleQuote.Error:6:15' => 'String should be declared with single quotes.', | ||
'SingleQuote.Error:7:15' => 'String should be declared with single quotes.', | ||
'SingleQuote.Error:10:15' => 'String should be declared with single quotes.', | ||
'SingleQuote.Error:11:15' => 'String should be declared with single quotes.', | ||
], fixedFilePath: __DIR__.'/SingleQuoteRuleTest.all.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