Skip to content

Commit 9f51b41

Browse files
authored
NoSuperfluousConcatenationFixer - fix for "$" being at the end of string on the left of concatenation operator. (#347)
1 parent 85f9b2b commit 9f51b41

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
1010
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
11-
![Tests](https://img.shields.io/badge/tests-2292-brightgreen.svg)
11+
![Tests](https://img.shields.io/badge/tests-2297-brightgreen.svg)
1212
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
1313
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)
1414

src/Fixer/NoSuperfluousConcatenationFixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,22 @@ private function fixConcat(Tokens $tokens, int $firstIndex, int $secondIndex): v
160160
[
161161
new Token(
162162
[T_CONSTANT_ENCAPSED_STRING,
163-
$prefix . $border . $this->getContentForBorder($firstContent, $border) . $this->getContentForBorder($secondContent, $border) . $border,
163+
$prefix . $border . $this->getContentForBorder($firstContent, $border, true) . $this->getContentForBorder($secondContent, $border, false) . $border,
164164
]
165165
),
166166
]
167167
);
168168
}
169169

170-
private function getContentForBorder(string $content, string $targetBorder): string
170+
private function getContentForBorder(string $content, string $targetBorder, bool $escapeDollarWhenIsLastCharacter): string
171171
{
172172
$currentBorder = $content[0];
173173
$content = \substr($content, 1, -1);
174174
if ($currentBorder === '"') {
175+
if ($escapeDollarWhenIsLastCharacter && $content[\strlen($content) - 1] === '$') {
176+
$content = \substr($content, 0, -1) . '\$';
177+
}
178+
175179
return $content;
176180
}
177181
if ($targetBorder === "'") {

tests/Fixer/NoSuperfluousConcatenationFixerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,31 @@ public static function provideFixCases(): iterable
177177
['allow_preventing_trailing_spaces' => true],
178178
];
179179

180+
yield [
181+
'<?php echo "My name is \$foo";',
182+
'<?php echo "My name is $" . "foo";',
183+
];
184+
185+
yield [
186+
'<?php echo "My name is \$foo";',
187+
'<?php echo "My name is $" . \'foo\';',
188+
];
189+
190+
yield [
191+
'<?php echo "My name is \$foo";',
192+
'<?php echo \'My name is $\' . "foo";',
193+
];
194+
195+
yield [
196+
'<?php echo \'My name is $foo\';',
197+
'<?php echo \'My name is $\' . \'foo\';',
198+
];
199+
200+
yield [
201+
'<?php echo "one \$two \$three $";',
202+
'<?php echo "one $" . "two $" . "three $";',
203+
];
204+
180205
for ($bytevalue = 0; $bytevalue < 256; $bytevalue++) {
181206
$char = \chr($bytevalue);
182207
yield [

0 commit comments

Comments
 (0)