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. * @return iterable<array{0: string, 1?: string}>
  40. */
  41. public static function provideFixPreIncrementCases(): iterable
  42. {
  43. yield [
  44. '<?php ++$a;',
  45. '<?php $a++;',
  46. ];
  47. yield [
  48. '<?php ++$$a;',
  49. '<?php $$a++;',
  50. ];
  51. yield [
  52. '<?php ++${"a"};',
  53. '<?php ${"a"}++;',
  54. ];
  55. yield [
  56. '<?php --$a;',
  57. '<?php $a--;',
  58. ];
  59. yield [
  60. '<?php foo(); ++$a;',
  61. '<?php foo(); $a++;',
  62. ];
  63. yield [
  64. '<?php if (true) { ++$a; }',
  65. '<?php if (true) { $a++; }',
  66. ];
  67. yield [
  68. '<?php if (true) {} ++$a;',
  69. '<?php if (true) {} $a++;',
  70. ];
  71. yield [
  72. '<?php for ($i = 0; $i < $count; ++$i) {}',
  73. '<?php for ($i = 0; $i < $count; $i++) {}',
  74. ];
  75. yield [
  76. '<?php ++$a->foo;',
  77. '<?php $a->foo++;',
  78. ];
  79. yield [
  80. '<?php ++$a->{"foo"};',
  81. '<?php $a->{"foo"}++;',
  82. ];
  83. yield [
  84. '<?php ++$a->$b;',
  85. '<?php $a->$b++;',
  86. ];
  87. yield [
  88. '<?php ++Foo\Bar::$bar;',
  89. '<?php Foo\Bar::$bar++;',
  90. ];
  91. yield [
  92. '<?php ++$a::$bar;',
  93. '<?php $a::$bar++;',
  94. ];
  95. yield [
  96. '<?php ++$a[0];',
  97. '<?php $a[0]++;',
  98. ];
  99. yield [
  100. '<?php ++$a[$b];',
  101. '<?php $a[$b]++;',
  102. ];
  103. yield ['<?php $a = $b++;'];
  104. yield ['<?php $a + $b++;'];
  105. yield ['<?php $a++ + $b;'];
  106. yield ['<?php foo($b++);'];
  107. yield ['<?php foo($a, $b++);'];
  108. yield ['<?php $a[$b++];'];
  109. yield ['<?php echo $a++;'];
  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 ['<?= ++$a;'];
  118. yield [
  119. '<?php class Test {
  120. public function foo() {
  121. $a = 123;
  122. ++self::$st;
  123. }
  124. }',
  125. '<?php class Test {
  126. public function foo() {
  127. $a = 123;
  128. self::$st++;
  129. }
  130. }',
  131. ];
  132. yield [
  133. '<?php class Test {
  134. public function foo() {
  135. $a = 123;
  136. ++static::$st;
  137. }
  138. }',
  139. '<?php class Test {
  140. public function foo() {
  141. $a = 123;
  142. static::$st++;
  143. }
  144. }',
  145. ];
  146. yield [
  147. '<?php if ($foo) ++$a;',
  148. '<?php if ($foo) $a++;',
  149. ];
  150. }
  151. /**
  152. * @dataProvider provideFixPostIncrementCases
  153. */
  154. public function testFixPostIncrement(string $expected, ?string $input = null): void
  155. {
  156. $this->fixer->configure(['style' => IncrementStyleFixer::STYLE_POST]);
  157. $this->doTest($expected, $input);
  158. }
  159. /**
  160. * @return iterable<array{0: string, 1?: string}>
  161. */
  162. public static function provideFixPostIncrementCases(): iterable
  163. {
  164. foreach (self::provideFixPreIncrementCases() as $case) {
  165. yield array_reverse($case);
  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. }