IncrementStyleFixerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. return array_map(static function (array $case): array {
  45. return array_reverse($case);
  46. }, self::provideFixPreIncrementCases());
  47. }
  48. public static function provideFixPreIncrementCases(): iterable
  49. {
  50. $cases = [
  51. [
  52. '<?php ++$a;',
  53. '<?php $a++;',
  54. ],
  55. [
  56. '<?php ++$$a;',
  57. '<?php $$a++;',
  58. ],
  59. [
  60. '<?php ++${"a"};',
  61. '<?php ${"a"}++;',
  62. ],
  63. [
  64. '<?php --$a;',
  65. '<?php $a--;',
  66. ],
  67. [
  68. '<?php foo(); ++$a;',
  69. '<?php foo(); $a++;',
  70. ],
  71. [
  72. '<?php if (true) { ++$a; }',
  73. '<?php if (true) { $a++; }',
  74. ],
  75. [
  76. '<?php if (true) {} ++$a;',
  77. '<?php if (true) {} $a++;',
  78. ],
  79. [
  80. '<?php for ($i = 0; $i < $count; ++$i) {}',
  81. '<?php for ($i = 0; $i < $count; $i++) {}',
  82. ],
  83. [
  84. '<?php ++$a->foo;',
  85. '<?php $a->foo++;',
  86. ],
  87. [
  88. '<?php ++$a->{"foo"};',
  89. '<?php $a->{"foo"}++;',
  90. ],
  91. [
  92. '<?php ++$a->$b;',
  93. '<?php $a->$b++;',
  94. ],
  95. [
  96. '<?php ++Foo\Bar::$bar;',
  97. '<?php Foo\Bar::$bar++;',
  98. ],
  99. [
  100. '<?php ++$a::$bar;',
  101. '<?php $a::$bar++;',
  102. ],
  103. [
  104. '<?php ++$a[0];',
  105. '<?php $a[0]++;',
  106. ],
  107. [
  108. '<?php ++$a[$b];',
  109. '<?php $a[$b]++;',
  110. ],
  111. ['<?php $a = $b++;'],
  112. ['<?php $a + $b++;'],
  113. ['<?php $a++ + $b;'],
  114. ['<?php foo($b++);'],
  115. ['<?php foo($a, $b++);'],
  116. ['<?php $a[$b++];'],
  117. ['<?php echo $a++;'],
  118. ['<?php $a = ++$b;'],
  119. ['<?php $a + ++$b;'],
  120. ['<?php ++$a + $b;'],
  121. ['<?php foo(++$b);'],
  122. ['<?php foo($a, ++$b);'],
  123. ['<?php $a[++$b];'],
  124. ['<?php echo ++$a;'],
  125. ['<?= ++$a;'],
  126. [
  127. '<?php class Test {
  128. public function foo() {
  129. $a = 123;
  130. ++self::$st;
  131. }
  132. }',
  133. '<?php class Test {
  134. public function foo() {
  135. $a = 123;
  136. self::$st++;
  137. }
  138. }',
  139. ],
  140. [
  141. '<?php class Test {
  142. public function foo() {
  143. $a = 123;
  144. ++static::$st;
  145. }
  146. }',
  147. '<?php class Test {
  148. public function foo() {
  149. $a = 123;
  150. static::$st++;
  151. }
  152. }',
  153. ],
  154. [
  155. '<?php if ($foo) ++$a;',
  156. '<?php if ($foo) $a++;',
  157. ],
  158. ];
  159. if (\PHP_VERSION_ID < 8_00_00) {
  160. $cases[] = [
  161. '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;',
  162. '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h++;',
  163. ];
  164. $cases[] = [
  165. '<?php ++$a{0};',
  166. '<?php $a{0}++;',
  167. ];
  168. $cases[] = [
  169. '<?php ++${$a}->{$b."foo"}->bar[$c]->$baz;',
  170. '<?php ${$a}->{$b."foo"}->bar[$c]->$baz++;',
  171. ];
  172. }
  173. return $cases;
  174. }
  175. }