IncrementStyleFixerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Operator;
  13. use PhpCsFixer\Fixer\Operator\IncrementStyleFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Gregor Harlan <gharlan@web.de>
  17. * @author Kuba Werłos <werlos@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\AbstractIncrementOperatorFixer
  22. * @covers \PhpCsFixer\Fixer\Operator\IncrementStyleFixer
  23. */
  24. final class IncrementStyleFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixPreIncrementCases
  28. */
  29. public function testFixPreIncrement(string $expected, ?string $input = null): void
  30. {
  31. $this->fixer->configure(['style' => IncrementStyleFixer::STYLE_PRE]);
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @dataProvider provideFixPostIncrementCases
  36. */
  37. public function testFixPostIncrement(string $expected, ?string $input = null): void
  38. {
  39. $this->fixer->configure(['style' => IncrementStyleFixer::STYLE_POST]);
  40. $this->doTest($expected, $input);
  41. }
  42. public static function provideFixPostIncrementCases(): iterable
  43. {
  44. foreach (self::provideFixPreIncrementCases() as $case) {
  45. yield array_reverse($case);
  46. }
  47. }
  48. public static function provideFixPreIncrementCases(): iterable
  49. {
  50. yield [
  51. '<?php ++$a;',
  52. '<?php $a++;',
  53. ];
  54. yield [
  55. '<?php ++$$a;',
  56. '<?php $$a++;',
  57. ];
  58. yield [
  59. '<?php ++${"a"};',
  60. '<?php ${"a"}++;',
  61. ];
  62. yield [
  63. '<?php --$a;',
  64. '<?php $a--;',
  65. ];
  66. yield [
  67. '<?php foo(); ++$a;',
  68. '<?php foo(); $a++;',
  69. ];
  70. yield [
  71. '<?php if (true) { ++$a; }',
  72. '<?php if (true) { $a++; }',
  73. ];
  74. yield [
  75. '<?php if (true) {} ++$a;',
  76. '<?php if (true) {} $a++;',
  77. ];
  78. yield [
  79. '<?php for ($i = 0; $i < $count; ++$i) {}',
  80. '<?php for ($i = 0; $i < $count; $i++) {}',
  81. ];
  82. yield [
  83. '<?php ++$a->foo;',
  84. '<?php $a->foo++;',
  85. ];
  86. yield [
  87. '<?php ++$a->{"foo"};',
  88. '<?php $a->{"foo"}++;',
  89. ];
  90. yield [
  91. '<?php ++$a->$b;',
  92. '<?php $a->$b++;',
  93. ];
  94. yield [
  95. '<?php ++Foo\Bar::$bar;',
  96. '<?php Foo\Bar::$bar++;',
  97. ];
  98. yield [
  99. '<?php ++$a::$bar;',
  100. '<?php $a::$bar++;',
  101. ];
  102. yield [
  103. '<?php ++$a[0];',
  104. '<?php $a[0]++;',
  105. ];
  106. yield [
  107. '<?php ++$a[$b];',
  108. '<?php $a[$b]++;',
  109. ];
  110. yield ['<?php $a = $b++;'];
  111. yield ['<?php $a + $b++;'];
  112. yield ['<?php $a++ + $b;'];
  113. yield ['<?php foo($b++);'];
  114. yield ['<?php foo($a, $b++);'];
  115. yield ['<?php $a[$b++];'];
  116. yield ['<?php echo $a++;'];
  117. yield ['<?php $a = ++$b;'];
  118. yield ['<?php $a + ++$b;'];
  119. yield ['<?php ++$a + $b;'];
  120. yield ['<?php foo(++$b);'];
  121. yield ['<?php foo($a, ++$b);'];
  122. yield ['<?php $a[++$b];'];
  123. yield ['<?php echo ++$a;'];
  124. yield ['<?= ++$a;'];
  125. yield [
  126. '<?php class Test {
  127. public function foo() {
  128. $a = 123;
  129. ++self::$st;
  130. }
  131. }',
  132. '<?php class Test {
  133. public function foo() {
  134. $a = 123;
  135. self::$st++;
  136. }
  137. }',
  138. ];
  139. yield [
  140. '<?php class Test {
  141. public function foo() {
  142. $a = 123;
  143. ++static::$st;
  144. }
  145. }',
  146. '<?php class Test {
  147. public function foo() {
  148. $a = 123;
  149. static::$st++;
  150. }
  151. }',
  152. ];
  153. yield [
  154. '<?php if ($foo) ++$a;',
  155. '<?php if ($foo) $a++;',
  156. ];
  157. if (\PHP_VERSION_ID < 8_00_00) {
  158. yield [
  159. '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;',
  160. '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h++;',
  161. ];
  162. yield [
  163. '<?php ++$a{0};',
  164. '<?php $a{0}++;',
  165. ];
  166. yield [
  167. '<?php ++${$a}->{$b."foo"}->bar[$c]->$baz;',
  168. '<?php ${$a}->{$b."foo"}->bar[$c]->$baz++;',
  169. ];
  170. }
  171. }
  172. }