Skip to content

Commit 0f3b990

Browse files
authored
Add option for ignoring different types of quotes (fixes #916) (#978)
1 parent e55510c commit 0f3b990

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
66
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
77
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
8-
![Tests](https://img.shields.io/badge/tests-3543-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3545-brightgreen.svg)
99
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
1010

1111
[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)
@@ -313,6 +313,7 @@ There must be no parameters passed by reference in functions.
313313
There must be no superfluous concatenation of literal strings.
314314
Configuration options:
315315
- `allow_preventing_trailing_spaces` (`bool`): whether to keep concatenation if it prevents having trailing spaces in string; defaults to `false`
316+
- `keep_concatenation_for_different_quotes` (`bool`): whether to keep concatenation if single-quoted and double-quoted would be concatenated; defaults to `false`
316317
```diff
317318
<?php
318319
-echo 'foo' . 'bar';

src/Fixer/NoSuperfluousConcatenationFixer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
final class NoSuperfluousConcatenationFixer extends AbstractFixer implements ConfigurableFixerInterface
3232
{
3333
private bool $allowPreventingTrailingSpaces = false;
34+
private bool $keepConcatenationForDifferentQuotes = false;
3435

3536
public function getDefinition(): FixerDefinitionInterface
3637
{
@@ -48,6 +49,10 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
4849
->setAllowedTypes(['bool'])
4950
->setDefault($this->allowPreventingTrailingSpaces)
5051
->getOption(),
52+
(new FixerOptionBuilder('keep_concatenation_for_different_quotes', 'whether to keep concatenation if single-quoted and double-quoted would be concatenated'))
53+
->setAllowedTypes(['bool'])
54+
->setDefault($this->keepConcatenationForDifferentQuotes)
55+
->getOption(),
5156
]);
5257
}
5358

@@ -59,6 +64,9 @@ public function configure(array $configuration): void
5964
if (\array_key_exists('allow_preventing_trailing_spaces', $configuration)) {
6065
$this->allowPreventingTrailingSpaces = $configuration['allow_preventing_trailing_spaces'];
6166
}
67+
if (\array_key_exists('keep_concatenation_for_different_quotes', $configuration)) {
68+
$this->keepConcatenationForDifferentQuotes = $configuration['keep_concatenation_for_different_quotes'];
69+
}
6270
}
6371

6472
/**
@@ -105,6 +113,12 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
105113
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $index, $secondIndex)) {
106114
continue;
107115
}
116+
if (
117+
$this->keepConcatenationForDifferentQuotes
118+
&& substr($tokens[$firstIndex]->getContent(), 0, 1) !== substr($tokens[$secondIndex]->getContent(), 0, 1)
119+
) {
120+
continue;
121+
}
108122

109123
$this->fixConcat($tokens, $firstIndex, $secondIndex);
110124
}

tests/Fixer/NoSuperfluousConcatenationFixerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ public static function provideFixCases(): iterable
186186
['allow_preventing_trailing_spaces' => true],
187187
];
188188

189+
yield 'option for leaving double-quoted string and single-quoted string concatendated' => [
190+
'\'abc\'."def"',
191+
null,
192+
['keep_concatenation_for_different_quotes' => true]
193+
];
194+
189195
yield 'dollar as last character in double quotes merged with double quotes' => [
190196
'"My name is \\$foo"',
191197
'"My name is $" . "foo"',

0 commit comments

Comments
 (0)