CombineConsecutiveUnsetsFixerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\LanguageConstruct;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveUnsetsFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveUnsetsFixer>
  20. */
  21. final class CombineConsecutiveUnsetsFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php //1
  37. unset($foo/*;*/, /*;*/$bar, $c , $foobar , $foobar2);
  38. //test
  39. /* more comment test*/
  40. '.'
  41. ',
  42. '<?php //1
  43. unset($foo/*;*/);
  44. unset(/*;*/$bar, $c ); //test
  45. unset($foobar ); /* more comment test*/
  46. unset( $foobar2);
  47. ',
  48. ];
  49. yield [
  50. '<?php unset($d , $e);/*unset( $d2);unset($e );;*/ ',
  51. '<?php unset($d );/*unset( $d2);unset($e );;*/ uNseT($e);',
  52. ];
  53. yield [
  54. '<?php UNSET($a, $b,$c/**/); ',
  55. '<?php UNSET($a); unset($b,$c/**/);',
  56. ];
  57. yield [
  58. '<?php
  59. $config = array();
  60. if ($config) {
  61. }
  62. unset($config[\'autoescape_service\'], $config[\'autoescape_service_method\']);
  63. ',
  64. ];
  65. yield [
  66. '<?php //2
  67. unset($foo, $bar, $foobar, $foobar2, $foobar3);/*1*/
  68. /*2*/
  69. //3
  70. /*4*/
  71. /*5*/ '.'
  72. ',
  73. '<?php //2
  74. unset($foo);/*1*/
  75. unset($bar);/*2*/
  76. unset($foobar);//3
  77. unset($foobar2);/*4*/
  78. /*5*/ unset($foobar3);
  79. ',
  80. ];
  81. yield [
  82. '<?php
  83. unset($foo3, $bar, $test,$test1);
  84. /* c1 */
  85. '.'
  86. '.'
  87. // c2
  88. '.'
  89. ',
  90. '<?php
  91. unset($foo3);
  92. /* c1 */
  93. unset($bar);
  94. '.'
  95. // c2
  96. unset($test,$test1);
  97. ',
  98. ];
  99. yield [
  100. '<?php unset($x, $b , $d); /**/ ?> b',
  101. '<?php unset($x); /**/ unset ($b , $d) ?> b',
  102. ];
  103. yield [
  104. '<?php unset($x) ?>',
  105. ];
  106. yield [
  107. '<?php unset($y, $u); ?>',
  108. '<?php unset($y);unset($u) ?>',
  109. ];
  110. yield [
  111. '<?php
  112. unset($a[0], $a[\'a\'], $a["b"], $a->b, $a->b->c, $a->b[0]->c[\'a\']);
  113. '.'
  114. '.'
  115. '.'
  116. '.'
  117. '.'
  118. ',
  119. '<?php
  120. unset($a[0]);
  121. unset($a[\'a\']);
  122. unset($a["b"]);
  123. unset($a->b);
  124. unset($a->b->c);
  125. unset($a->b[0]->c[\'a\']);
  126. ',
  127. ];
  128. }
  129. /**
  130. * @dataProvider provideFixPre80Cases
  131. *
  132. * @requires PHP <8.0
  133. */
  134. public function testFixPre80(string $expected, ?string $input = null): void
  135. {
  136. $this->doTest($expected, $input);
  137. }
  138. /**
  139. * @return iterable<array{string}>
  140. */
  141. public static function provideFixPre80Cases(): iterable
  142. {
  143. yield [
  144. '<?php (unset)$f;',
  145. ];
  146. }
  147. }