IncrementStyleFixerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\IncrementStyleFixer>
  25. *
  26. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\IncrementStyleFixer
  27. */
  28. final class IncrementStyleFixerTest extends AbstractFixerTestCase
  29. {
  30. /**
  31. * @dataProvider provideFixPreIncrementCases
  32. */
  33. public function testFixPreIncrement(string $expected, ?string $input = null): void
  34. {
  35. $this->fixer->configure(['style' => IncrementStyleFixer::STYLE_PRE]);
  36. $this->doTest($expected, $input);
  37. }
  38. /**
  39. * @dataProvider provideFixPostIncrementCases
  40. */
  41. public function testFixPostIncrement(string $expected, ?string $input = null): void
  42. {
  43. $this->fixer->configure(['style' => IncrementStyleFixer::STYLE_POST]);
  44. $this->doTest($expected, $input);
  45. }
  46. /**
  47. * @return iterable<array{0: string, 1?: string}>
  48. */
  49. public static function provideFixPostIncrementCases(): iterable
  50. {
  51. foreach (self::provideFixPreIncrementCases() as $case) {
  52. yield array_reverse($case);
  53. }
  54. }
  55. /**
  56. * @return iterable<array{0: string, 1?: string}>
  57. */
  58. public static function provideFixPreIncrementCases(): iterable
  59. {
  60. yield [
  61. '<?php ++$a;',
  62. '<?php $a++;',
  63. ];
  64. yield [
  65. '<?php ++$$a;',
  66. '<?php $$a++;',
  67. ];
  68. yield [
  69. '<?php ++${"a"};',
  70. '<?php ${"a"}++;',
  71. ];
  72. yield [
  73. '<?php --$a;',
  74. '<?php $a--;',
  75. ];
  76. yield [
  77. '<?php foo(); ++$a;',
  78. '<?php foo(); $a++;',
  79. ];
  80. yield [
  81. '<?php if (true) { ++$a; }',
  82. '<?php if (true) { $a++; }',
  83. ];
  84. yield [
  85. '<?php if (true) {} ++$a;',
  86. '<?php if (true) {} $a++;',
  87. ];
  88. yield [
  89. '<?php for ($i = 0; $i < $count; ++$i) {}',
  90. '<?php for ($i = 0; $i < $count; $i++) {}',
  91. ];
  92. yield [
  93. '<?php ++$a->foo;',
  94. '<?php $a->foo++;',
  95. ];
  96. yield [
  97. '<?php ++$a->{"foo"};',
  98. '<?php $a->{"foo"}++;',
  99. ];
  100. yield [
  101. '<?php ++$a->$b;',
  102. '<?php $a->$b++;',
  103. ];
  104. yield [
  105. '<?php ++Foo\Bar::$bar;',
  106. '<?php Foo\Bar::$bar++;',
  107. ];
  108. yield [
  109. '<?php ++$a::$bar;',
  110. '<?php $a::$bar++;',
  111. ];
  112. yield [
  113. '<?php ++$a[0];',
  114. '<?php $a[0]++;',
  115. ];
  116. yield [
  117. '<?php ++$a[$b];',
  118. '<?php $a[$b]++;',
  119. ];
  120. yield ['<?php $a = $b++;'];
  121. yield ['<?php $a + $b++;'];
  122. yield ['<?php $a++ + $b;'];
  123. yield ['<?php foo($b++);'];
  124. yield ['<?php foo($a, $b++);'];
  125. yield ['<?php $a[$b++];'];
  126. yield ['<?php echo $a++;'];
  127. yield ['<?php $a = ++$b;'];
  128. yield ['<?php $a + ++$b;'];
  129. yield ['<?php ++$a + $b;'];
  130. yield ['<?php foo(++$b);'];
  131. yield ['<?php foo($a, ++$b);'];
  132. yield ['<?php $a[++$b];'];
  133. yield ['<?php echo ++$a;'];
  134. yield ['<?= ++$a;'];
  135. yield [
  136. '<?php class Test {
  137. public function foo() {
  138. $a = 123;
  139. ++self::$st;
  140. }
  141. }',
  142. '<?php class Test {
  143. public function foo() {
  144. $a = 123;
  145. self::$st++;
  146. }
  147. }',
  148. ];
  149. yield [
  150. '<?php class Test {
  151. public function foo() {
  152. $a = 123;
  153. ++static::$st;
  154. }
  155. }',
  156. '<?php class Test {
  157. public function foo() {
  158. $a = 123;
  159. static::$st++;
  160. }
  161. }',
  162. ];
  163. yield [
  164. '<?php if ($foo) ++$a;',
  165. '<?php if ($foo) $a++;',
  166. ];
  167. }
  168. /**
  169. * @dataProvider provideFixPre80Cases
  170. *
  171. * @requires PHP <8.0
  172. */
  173. public function testFixPre80(string $expected, ?string $input = null): void
  174. {
  175. $this->doTest($expected, $input);
  176. }
  177. /**
  178. * @return iterable<array{string, 1?: string}>
  179. */
  180. public static function provideFixPre80Cases(): iterable
  181. {
  182. yield [
  183. '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;',
  184. '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h++;',
  185. ];
  186. yield [
  187. '<?php ++$a{0};',
  188. '<?php $a{0}++;',
  189. ];
  190. yield [
  191. '<?php ++${$a}->{$b."foo"}->bar[$c]->$baz;',
  192. '<?php ${$a}->{$b."foo"}->bar[$c]->$baz++;',
  193. ];
  194. }
  195. }