123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\StringNotation;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Filippo Tessarotto <zoeslam@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer>
- */
- final class ExplicitStringVariableFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- $input = $expected = '<?php';
- for ($inc = 1; $inc < 15; ++$inc) {
- $expected .= " \$var{$inc} = \"My name is {\$name}!\";";
- $input .= " \$var{$inc} = \"My name is \$name!\";";
- }
- yield [
- $expected,
- $input,
- ];
- yield [
- '<?php $a = "My name is {$name}!";',
- '<?php $a = "My name is $name!";',
- ];
- yield [
- '<?php "My name is {$james}{$bond}!";',
- '<?php "My name is $james$bond!";',
- ];
- yield [
- '<?php $a = <<<EOF
- My name is {$name}!
- EOF;
- ',
- '<?php $a = <<<EOF
- My name is $name!
- EOF;
- ',
- ];
- yield [
- '<?php $a = "{$b}";',
- '<?php $a = "$b";',
- ];
- yield [
- '<?php $a = "{$b} start";',
- '<?php $a = "$b start";',
- ];
- yield [
- '<?php $a = "end {$b}";',
- '<?php $a = "end $b";',
- ];
- yield [
- '<?php $a = <<<EOF
- {$b}
- EOF;
- ',
- '<?php $a = <<<EOF
- $b
- EOF;
- ',
- ];
- yield ['<?php $a = \'My name is $name!\';'];
- yield ['<?php $a = "My name is " . $name;'];
- yield ['<?php $a = "My name is {$name}!";'];
- yield [
- '<?php $a = <<<EOF
- My name is {$name}!
- EOF;
- ',
- ];
- yield ['<?php $a = "My name is {$user->name}";'];
- yield [
- '<?php $a = <<<EOF
- My name is {$user->name}
- EOF;
- ',
- ];
- yield [
- '<?php $a = <<<\'EOF\'
- $b
- EOF;
- ',
- ];
- yield [
- '<?php $a = "My name is {$object->property} !";',
- '<?php $a = "My name is $object->property !";',
- ];
- yield [
- '<?php $a = "My name is {$array[1]} !";',
- '<?php $a = "My name is $array[1] !";',
- ];
- yield [
- '<?php $a = "My name is {$array[\'foo\']} !";',
- '<?php $a = "My name is $array[foo] !";',
- ];
- yield [
- '<?php $a = "My name is {$array[$foo]} !";',
- '<?php $a = "My name is $array[$foo] !";',
- ];
- yield [
- '<?php $a = "My name is {$array[$foo]}[{$bar}] !";',
- '<?php $a = "My name is $array[$foo][$bar] !";',
- ];
- yield [
- '<?php $a = "Closure not allowed {$closure}() text";',
- '<?php $a = "Closure not allowed $closure() text";',
- ];
- yield [
- '<?php $a = "Complex object chaining not allowed {$object->property}->method()->array[1] text";',
- '<?php $a = "Complex object chaining not allowed $object->property->method()->array[1] text";',
- ];
- yield [
- '<?php $a = "Complex array chaining not allowed {$array[1]}[2][MY_CONSTANT] text";',
- '<?php $a = "Complex array chaining not allowed $array[1][2][MY_CONSTANT] text";',
- ];
- yield [
- '<?php $a = "Concatenation: {$james}{$bond}{$object->property}{$array[1]}!";',
- '<?php $a = "Concatenation: $james$bond$object->property$array[1]!";',
- ];
- yield [
- '<?php $a = "{$a->b} start";',
- '<?php $a = "$a->b start";',
- ];
- yield [
- '<?php $a = "end {$a->b}";',
- '<?php $a = "end $a->b";',
- ];
- yield [
- '<?php $a = "{$a[1]} start";',
- '<?php $a = "$a[1] start";',
- ];
- yield [
- '<?php $a = "end {$a[1]}";',
- '<?php $a = "end $a[1]";',
- ];
- yield [
- '<?php $a = b"{$a->b} start";',
- '<?php $a = b"$a->b start";',
- ];
- yield [
- '<?php $a = b"end {$a->b}";',
- '<?php $a = b"end $a->b";',
- ];
- yield [
- '<?php $a = b"{$a[1]} start";',
- '<?php $a = b"$a[1] start";',
- ];
- yield [
- '<?php $a = b"end {$a[1]}";',
- '<?php $a = b"end $a[1]";',
- ];
- yield [
- '<?php $a = B"{$a->b} start";',
- '<?php $a = B"$a->b start";',
- ];
- yield [
- '<?php $a = B"end {$a->b}";',
- '<?php $a = B"end $a->b";',
- ];
- yield [
- '<?php $a = B"{$a[1]} start";',
- '<?php $a = B"$a[1] start";',
- ];
- yield [
- '<?php $a = B"end {$a[1]}";',
- '<?php $a = B"end $a[1]";',
- ];
- yield [
- '<?php $a = "*{$a[0]}{$b[1]}X{$c[2]}{$d[3]}";',
- '<?php $a = "*$a[0]$b[1]X$c[2]$d[3]";',
- ];
- yield [
- '<?php $a = `echo $foo`;',
- ];
- yield [
- '<?php $a = "My name is {$name}!"; $a = `echo $foo`; $a = "{$a->b} start";',
- '<?php $a = "My name is $name!"; $a = `echo $foo`; $a = "$a->b start";',
- ];
- yield [
- '<?php $mobileNumberVisible = "***-***-{$last4Digits[0]}{$last4Digits[1]}-{$last4Digits[2]}{$last4Digits[3]}";',
- '<?php $mobileNumberVisible = "***-***-$last4Digits[0]$last4Digits[1]-$last4Digits[2]$last4Digits[3]";',
- ];
- yield [
- '<?php $pair = "{$foo} {$bar[0]}";',
- '<?php $pair = "$foo {$bar[0]}";',
- ];
- yield [
- '<?php $pair = "{$foo}{$bar[0]}";',
- '<?php $pair = "$foo{$bar[0]}";',
- ];
- yield [
- '<?php $a = "My name is {$array[-1]} !";',
- '<?php $a = "My name is $array[-1] !";',
- ];
- yield [
- '<?php $a = "{$a[-1]} start";',
- '<?php $a = "$a[-1] start";',
- ];
- yield [
- '<?php $a = "end {$a[-1]}";',
- '<?php $a = "end $a[-1]";',
- ];
- yield [
- '<?php $a = b"end {$a[-1]}";',
- '<?php $a = b"end $a[-1]";',
- ];
- yield [
- '<?php $a = B"end {$a[-1]}";',
- '<?php $a = B"end $a[-1]";',
- ];
- }
- }
|