Skip to content
Permalink
Browse files

NoUnneededConcatenationFixer - fix for binary strings (#192)

  • Loading branch information
kubawerlos committed Nov 23, 2019
1 parent ec894cc commit 34d8def725f8ae081bd38b2448298ebb57c8dd10
Showing with 39 additions and 6 deletions.
  1. +1 −1 README.md
  2. +18 −5 src/Fixer/NoUnneededConcatenationFixer.php
  3. +20 −0 tests/Fixer/NoUnneededConcatenationFixerTest.php
@@ -8,7 +8,7 @@

[![Build status](https://img.shields.io/travis/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://travis-ci.org/kubawerlos/php-cs-fixer-custom-fixers)
[![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)
![Tests](https://img.shields.io/badge/tests-1548-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1552-brightgreen.svg)
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
[![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)

@@ -80,16 +80,29 @@ private function areOnlyHorizontalWhitespacesBetween(Tokens $tokens, int $indexS
return true;
}

private function fixConcat(Tokens $tokens, int $prevIndex, int $nextIndex): void
private function fixConcat(Tokens $tokens, int $firstIndex, int $secondIndex): void
{
if ($tokens[$prevIndex]->getContent()[0] !== $tokens[$nextIndex]->getContent()[0]) {
$firstStringContent = $tokens[$firstIndex]->getContent();
$secondStringContent = $tokens[$secondIndex]->getContent();

if ($firstStringContent[\strlen($firstStringContent) - 1] !== $secondStringContent[\strlen($secondStringContent) - 1]) {
return;
}

$tokens->overrideRange(
$prevIndex,
$nextIndex,
[new Token([T_CONSTANT_ENCAPSED_STRING, \substr($tokens[$prevIndex]->getContent(), 0, -1) . \substr($tokens[$nextIndex]->getContent(), 1)])]
$firstIndex,
$secondIndex,
[new Token([
T_CONSTANT_ENCAPSED_STRING,
\substr($firstStringContent, 0, -1) . $this->getStringContent($secondStringContent) . $firstStringContent[\strlen($firstStringContent) - 1],
])]
);
}

private function getStringContent(string $string): string
{
$offset = \strtolower($string[0]) === 'b' ? 2 : 1;

return \substr($string, $offset, -1);
}
}
@@ -58,6 +58,26 @@ public static function provideFixCases(): iterable
"<?php 'foo' . 'bar';",
];

yield [
'<?php b"foobar";',
'<?php b"foo" . "bar";',
];

yield [
'<?php B"foobar";',
'<?php B"foo" . "bar";',
];

yield [
'<?php "foobar";',
'<?php "foo" . B"bar";',
];

yield [
'<?php b"foobar";',
'<?php b"foo" . b"bar";',
];

yield [
'<?php "foobarbazqux";',
'<?php "foo" . "bar" . "baz" . "qux";',

0 comments on commit 34d8def

Please sign in to comment.
You can’t perform that action at this time.