CombineConsecutiveUnsetsFixerTest.php 3.9 KB

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