Skip to content

Commit 8d93f68

Browse files
authored
Follow up to #978 (#985)
1 parent 0f3b990 commit 8d93f68

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

33
## v3.22.0
4+
- NoSuperfluousConcatenationFixer - add option "keep_concatenation_for_different_quotes"
45
- NoPhpStormGeneratedCommentFixer - handle more comments
56
- Update minimum PHP CS Fixer version to 3.59.0
67

README.md

Lines changed: 1 addition & 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-3545-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3547-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)

src/Fixer/NoSuperfluousConcatenationFixer.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,19 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
9494
continue;
9595
}
9696

97-
$firstIndex = $tokens->getPrevMeaningfulToken($index);
98-
\assert(\is_int($firstIndex));
99-
100-
if (!$tokens[$firstIndex]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) {
101-
continue;
102-
}
103-
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $firstIndex, $index)) {
97+
$firstIndex = $this->getFirstIndex($tokens, $index);
98+
if ($firstIndex === null) {
10499
continue;
105100
}
106101

107-
$secondIndex = $tokens->getNextMeaningfulToken($index);
108-
\assert(\is_int($secondIndex));
109-
110-
if (!$tokens[$secondIndex]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) {
111-
continue;
112-
}
113-
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $index, $secondIndex)) {
102+
$secondIndex = $this->getSecondIndex($tokens, $index);
103+
if ($secondIndex === null) {
114104
continue;
115105
}
106+
116107
if (
117108
$this->keepConcatenationForDifferentQuotes
118-
&& substr($tokens[$firstIndex]->getContent(), 0, 1) !== substr($tokens[$secondIndex]->getContent(), 0, 1)
109+
&& \substr($tokens[$firstIndex]->getContent(), 0, 1) !== \substr($tokens[$secondIndex]->getContent(), 0, 1)
119110
) {
120111
continue;
121112
}
@@ -124,6 +115,36 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
124115
}
125116
}
126117

118+
private function getFirstIndex(Tokens $tokens, int $index): ?int
119+
{
120+
$firstIndex = $tokens->getPrevMeaningfulToken($index);
121+
\assert(\is_int($firstIndex));
122+
123+
if (!$tokens[$firstIndex]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) {
124+
return null;
125+
}
126+
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $firstIndex, $index)) {
127+
return null;
128+
}
129+
130+
return $firstIndex;
131+
}
132+
133+
private function getSecondIndex(Tokens $tokens, int $index): ?int
134+
{
135+
$secondIndex = $tokens->getNextMeaningfulToken($index);
136+
\assert(\is_int($secondIndex));
137+
138+
if (!$tokens[$secondIndex]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) {
139+
return null;
140+
}
141+
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $index, $secondIndex)) {
142+
return null;
143+
}
144+
145+
return $secondIndex;
146+
}
147+
127148
private function areOnlyHorizontalWhitespacesBetween(Tokens $tokens, int $indexStart, int $indexEnd): bool
128149
{
129150
for ($index = $indexStart + 1; $index < $indexEnd; $index++) {

tests/Fixer/NoSuperfluousConcatenationFixerTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,26 @@ 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' => [
189+
yield 'option for leaving double-quoted string and single-quoted string concatenated' => [
190190
'\'abc\'."def"',
191191
null,
192-
['keep_concatenation_for_different_quotes' => true]
192+
['keep_concatenation_for_different_quotes' => true],
193+
];
194+
195+
yield 'keep concatenation for different quotes and merge others' => [
196+
<<<'CONTENT'
197+
"ab";
198+
"c" . 'd';
199+
'e' . "f";
200+
'gh';
201+
CONTENT,
202+
<<<'CONTENT'
203+
"a" . "b";
204+
"c" . 'd';
205+
'e' . "f";
206+
'g' . 'h';
207+
CONTENT,
208+
['keep_concatenation_for_different_quotes' => true],
193209
];
194210

195211
yield 'dollar as last character in double quotes merged with double quotes' => [

0 commit comments

Comments
 (0)