123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- <?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\Operator;
- use PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\AbstractShortOperatorFixer
- * @covers \PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer>
- */
- final class LongToShorthandOperatorFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield 'simple I' => [
- '<?php $a += 123;',
- '<?php $a = $a + 123;',
- ];
- yield 'simple II' => [
- '<?php $b[0] *= 789;',
- '<?php $b[0] = ($b[0]) * 789;',
- ];
- yield 'simple III' => [
- '<?php ($b *= 789);',
- '<?php ($b = $b * 789);',
- ];
- yield 'simple IV' => [
- '<?php foo($c /= 1234, 1);',
- '<?php foo($c = $c / 1234, 1);',
- ];
- yield 'simple V' => [
- '<?php foo(1, $x *= 1235, 1);',
- '<?php foo(1, $x = $x * 1235, 1);',
- ];
- yield 'simple II\' array' => [
- '<?php $aa[1] %= 963;',
- '<?php $aa[1] = $aa[1] % 963;',
- ];
- yield 'simple III array' => [
- '<?php $a[1][2] -= 852;',
- '<?php $a[1][2] = $a[1][2] - 852;',
- ];
- yield 'simple IV array' => [
- '<?php {$a[0][1][122] ^= $a;}',
- '<?php {$a[0][1][122] = $a[0][1][122] ^ $a;}',
- ];
- yield [
- '<?php $xa .= $b;',
- '<?php $xa = $xa . $b;',
- ];
- $constants = ['"foo"', "'foo'", '1', '1.1'];
- foreach ($constants as $i => $constant) {
- yield 'c #'.$i => [
- \sprintf('<?php $fa .= %s;', $constant),
- \sprintf('<?php $fa = $fa . %s;', $constant),
- ];
- yield 'c reverse #'.$i => [
- \sprintf('<?php $ga *= %s ;', $constant),
- \sprintf('<?php $ga = %s * $ga;', $constant),
- ];
- }
- foreach (['-', '/', '.', '%'] as $nonCommutativeKind) {
- yield \sprintf('non commutative kind "%s"', $nonCommutativeKind) => [
- \sprintf('<?php $nck = 5 %s $nck;', $nonCommutativeKind),
- ];
- }
- foreach (['*' => '*=', '|' => '|=', '&' => '&=', '^' => '^='] as $operator => $shortHand) {
- yield \sprintf('commutative operator "%s".', $operator) => [
- \sprintf('<?php $a3 %s "456" ;', $shortHand),
- \sprintf('<?php $a3 = "456" %s $a3;', $operator),
- ];
- }
- // array index
- yield 'simple I array' => [
- '<?php $ai[1] += 566;',
- '<?php $ai[1] = $ai[1] + 566;',
- ];
- yield 'simple II array' => [
- '<?php $p[1] += 789;',
- '<?php $p[1] = $p[1] + 789;',
- ];
- // minimal and multiple
- yield 'minimal' => [
- '<?php $a += 1;',
- '<?php $a=$a+1;',
- ];
- yield 'minimal, multiple' => [
- '<?php $a += 1;$a += 1;$a += 1;$a += 1;',
- '<?php $a=$a+1;$a=$a+1;$a=$a+1;$a=$a+1;',
- ];
- // test simple with all operators
- $reflection = new \ReflectionClass(LongToShorthandOperatorFixer::class);
- $operators = $reflection->getStaticProperties()['operators'];
- foreach ($operators as $operator => $info) {
- $shortHand = $info[1];
- yield \sprintf('Simple test with operator "%s" var/var.', $operator) => [
- \sprintf('<?php $a1 %s $b;', $shortHand),
- \sprintf('<?php $a1 = $a1 %s $b;', $operator),
- ];
- yield \sprintf('Simple test with operator "%s" var/const.', $operator) => [
- \sprintf('<?php $a2 %s 1;', $shortHand),
- \sprintf('<?php $a2 = $a2 %s 1;', $operator),
- ];
- }
- // odds and ends
- yield [
- '<?php $a4 += ++$b;',
- '<?php $a4 = $a4 + ++$b;',
- ];
- yield [
- '<?php $a5 .= '.'
- <<<EOD
- EOD
- ;',
- '<?php $a5 = $a5 .
- <<<EOD
- EOD
- ;',
- ];
- yield [
- '<?php $a6 .= '.'
- <<<\'EOD\'
- EOD
- ?>',
- '<?php $a6 = $a6 .
- <<<\'EOD\'
- EOD
- ?>',
- ];
- yield [
- '<?php
- $t += 1;
- $t1 -= 1;
- $t2 *= 1;
- $t3 /= 1;
- $t4 .= /* */ 1;',
- '<?php
- $t = ((($t))) + 1;
- $t1 = ($t1) - 1;
- $t2 = $t2 * 1;
- $t3 = ($t3) / 1;
- $t4 = ($t4) /* */ . 1;',
- ];
- // before assignment var
- yield 'minus itself' => [
- '<?php ;$a -= $a;',
- '<?php ;$a = $a - $a;',
- ];
- yield 'after not needed block' => [
- '<?php {echo 1;} $a &= $a;',
- '<?php {echo 1;} $a = $a & $a;',
- ];
- yield 'after if' => [
- '<?php if($z){echo 2;} $a |= $a;',
- '<?php if($z){echo 2;} $a = $a | $a;',
- ];
- yield 'fn minus itself' => [
- '<?php foo(1, $an -= $an);',
- '<?php foo(1, $an = $an - $an);',
- ];
- yield 'simple, before ) I' => [
- '<?php if ($a) $a .= "X"?>',
- '<?php if ($a) $a = $a . "X"?>',
- ];
- yield [
- '<?php
- $a1 /= +$b1;
- $a2 /= -$b2;
- ',
- '<?php
- $a1 = $a1 / +$b1;
- $a2 = $a2 / -$b2;
- ',
- ];
- // do not fix
- yield 'do not fix various' => ['<?php
- $a = ${foo} . 1;
- $a = ${foo}++ + 1;
- $a = $a[1] * 1;
- $a = $a(1 + 2) . 1;
- $a = $a[1][2] . 1;
- $a = $a[1][2][3][foo()][$a++][1+$a][${"foo"}][99] . 1;
- $a = ${foo}++ . 1;
- $a = ($a /* */ /* */ /* */ /* */ + 1 /* */ ) + 1;
- $a = 1 . 1 + foo();
- $a = 1 . foo() + 1;
- $a = 1 . foo();
- $a = 1 . foo(1, ++$a);
- $a = foo() . 1;
- $a = foo(1, ++$a) . 1;
- $a = $a[1] * 1;
- $a[1] = $a[0] * 1;
- $a = $a(1 + 2) . 1;
- foo($b, ${foo} + 1);
- foo($a + 1);
- $a++ + 2;
- 2 + $a++;
- $a = 7 . (int) $a;
- $a = (int) $a . 7;
- (int) $a = 7 . (int) $a;
- (int) $a = (int) $a . 7;
- $a = 1 . $a + foo();
- $a = $a instanceof \Foo & $b;
- $a = $a + $b instanceof \Foo;
- $a = $d / $a + $b;
- $d + $a = $a - $e;
- $a = $a >= $b;
- $a[1] = $a[1] instanceof \Foo & $b;
- '];
- yield ['<?php $a = 123 + $a + $c ?>'];
- yield ['<?php $a = $a + 123 + $c ?>'];
- // do not fix; not assignment
- yield ['<?php ($a + 123);'];
- yield ['<?php while(true){$a + 123;}'];
- yield ['<?php $a + 123;'];
- yield ['<?php ; $a + 123;'];
- // do not fix; precedence
- yield [
- '<?php
- $a = 1;
- $b = 3;
- $a = $a + $b ? 1 : 2;
- var_dump($a);
- $a = 1;
- $b = 3;
- $a += $b ? 1 : 2;
- var_dump($a);
- //---------------------
- $a = 2;
- $b = null;
- $a = $a + $b ?? 3;
- var_dump($a);
- $a = 2;
- $b = null;
- $a += $b ?? 3;
- var_dump($a);
- //---------------------
- $a = 3;
- $b = null;
- $a = $a + $b === null ? 3 : 1;
- var_dump($a);
- $a = 3;
- $b = null;
- $a += $b === null ? 3 : 1;
- var_dump($a);
- //---------------------
- $a = $a & $a ^ true;
- $a = $a ^ true & $a;
- $a = 1 . $a + foo();
- //---------------------
- $a = 1;
- $b = false;
- $z = true;
- $a = $a + $b || $z;
- var_dump($a);
- ',
- ];
- yield ['<?php {echo 1;} $a = new class{} & $a;'];
- // reverse
- yield 'simple I reverse' => [
- '<?php $a *= 9988 ?>',
- '<?php $a = 9988 * $a ?>',
- ];
- yield 'simple V, comments, reverse' => [
- '<?php foo(1, /*1*/$x /*2*/*= /*3*/123/*4*//*5*//*6*/, 1);',
- '<?php foo(1, /*1*/$x/*2*/=/*3*/123/*4*/*/*5*/$x/*6*/, 1);',
- ];
- yield 'simple VI, `)`, reverse' => [
- '<?php foo(1, $x *= 123);',
- '<?php foo(1, $x=123*$x);',
- ];
- yield [
- '<?php $a99 .= // foo
- <<<EOD
- EOD
- ;',
- '<?php $a99 = $a99 . // foo
- <<<EOD
- EOD
- ;',
- ];
- yield [
- '<?php $a00 .= // foo2
- <<<\'EOD\'
- EOD
- ;',
- '<?php $a00 = $a00 . // foo2
- <<<\'EOD\'
- EOD
- ;',
- ];
- yield 'do bother with to much mess' => [
- '<?php
- $a = 1 + $a + 2 + $a;
- $a = $a + 1 + $a + 2;
- ',
- ];
- yield [
- '<?php
- $r[1] = [&$r[1]];
- $r[1] = [$r[1],&$r[1]];
- ',
- ];
- yield 'switch case & default' => [
- '<?php
- switch(foo()) {
- case \'X\':
- $pX -= 789;
- break;
- default:
- $pY -= $b5;
- }
- ',
- '<?php
- switch(foo()) {
- case \'X\':
- $pX = $pX - 789;
- break;
- default:
- $pY = $pY - $b5;
- }
- ',
- ];
- yield 'operator precedence' => [
- '<?php $x = $z ? $b : $a = $a + 123;',
- ];
- yield 'alternative syntax' => [
- '<?php foreach([1, 2, 3] as $i): $a += $i; endforeach;',
- '<?php foreach([1, 2, 3] as $i): $a = $a + $i; endforeach;',
- ];
- yield 'assign and return' => [
- '<?php
- class Foo
- {
- private int $test = 1;
- public function bar(int $i): int
- {
- return $this->test += $i;
- }
- }',
- '<?php
- class Foo
- {
- private int $test = 1;
- public function bar(int $i): int
- {
- return $this->test = $this->test + $i;
- }
- }',
- ];
- }
- /**
- * @requires PHP <8.0
- *
- * @dataProvider provideFixPre80Cases
- */
- public function testFixPre80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixPre80Cases(): iterable
- {
- yield [
- '<?php
- $a = $a[1]{2} . 1;
- $a = $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99] . 1;
- $a = 1 . $a[1]{2};
- $a = 1 . $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99];',
- ];
- yield 'simple I\' array' => [
- '<?php $a[1] += 963;',
- '<?php $a[1] = $a{1} + 963;',
- ];
- yield 'simple II array' => [
- '<?php $a[1]{1} += 852;',
- '<?php $a[1]{1} = $a[1]{1} + 852;',
- ];
- yield 'simple III array' => [
- '<?php $a{7} += 742;',
- '<?php $a{7} = $a[7] + 742;',
- ];
- yield 'simple IV array' => [
- '<?php {$a[0]{1}[1] ^= $azz;} ?>',
- '<?php {$a[0]{1}[1] = $a[0][1]{1} ^ $azz;} ?>',
- ];
- }
- }
|