CombineConsecutiveIssetsFixerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\CombineConsecutiveIssetsFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveIssetsFixer>
  20. */
  21. final class CombineConsecutiveIssetsFixerTest 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<int|string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php $a = isset($a, $b) ;',
  37. '<?php $a = isset($a) && isset($b);',
  38. ];
  39. yield [
  40. '<?php $a = isset($a, $b,$c) ;',
  41. '<?php $a = isset($a) && isset($b,$c);',
  42. ];
  43. yield [
  44. '<?php $a = isset($a,$c, $b,$c) ;',
  45. '<?php $a = isset($a,$c) && isset($b,$c);',
  46. ];
  47. yield [
  48. '<?php $a = isset($a,$c, $b) ;',
  49. '<?php $a = isset($a,$c) && isset($b);',
  50. ];
  51. yield [
  52. '<?php $a = isset($a, $b) || isset($c, $e) ?>',
  53. '<?php $a = isset($a) && isset($b) || isset($c) && isset($e)?>',
  54. ];
  55. yield [
  56. '<?php $a = isset($a[a() ? b() : $d], $b) ;',
  57. '<?php $a = isset($a[a() ? b() : $d]) && isset($b);',
  58. ];
  59. yield [
  60. '<?php $a = isset($a[$b], $b/**/) ;',
  61. '<?php $a = isset($a[$b]/**/) && isset($b);',
  62. ];
  63. yield [
  64. '<?php $a = isset ( $a, $c, $d /*1*/ ) ;',
  65. '<?php $a = isset ( $a /*1*/ ) && isset ( $c ) && isset( $d );',
  66. ];
  67. yield 'minimal fix case' => [
  68. '<?php {{isset($a, $b);}}',
  69. '<?php {{isset($a)&&isset($b);}}',
  70. ];
  71. yield [
  72. '<?php foo(isset($a, $b, $c) );',
  73. '<?php foo(isset($a) && isset($b) && isset($c));',
  74. ];
  75. yield [
  76. '<?php isset($a, $b) && !isset($c) ?>',
  77. '<?php isset($a) && isset($b) && !isset($c) ?>',
  78. ];
  79. yield [
  80. '<?php $a = isset($a,$c, $b,$c, $b,$c,$d,$f, $b) ;',
  81. '<?php $a = isset($a,$c) && isset($b,$c) && isset($b,$c,$d,$f) && isset($b);',
  82. ];
  83. yield 'comments' => [
  84. '<?php
  85. $a =#0
  86. isset#1
  87. (#2
  88. $a, $b,$c, $d#3
  89. )#4
  90. #5
  91. #6
  92. #7
  93. #8
  94. #9
  95. /*10*/ /**11
  96. */
  97. '.'
  98. ;',
  99. '<?php
  100. $a =#0
  101. isset#1
  102. (#2
  103. $a#3
  104. )#4
  105. &&#5
  106. isset
  107. #6
  108. #7
  109. ( #8
  110. $b #9
  111. /*10*/, $c/**11
  112. */
  113. )&& isset($d)
  114. ;',
  115. ];
  116. yield [
  117. '<?php
  118. $a = isset($a, $b, $c, $d, $e, $f) ;
  119. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  120. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  121. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  122. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  123. $a = isset($a, $b) ;
  124. ',
  125. '<?php
  126. $a = isset($a) && isset($b) && isset($c) && isset($d) && isset($e) && isset($f);
  127. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  128. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  129. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  130. echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
  131. $a = isset($a) && isset($b);
  132. ',
  133. ];
  134. yield [
  135. '<?php $d = isset($z[1], $z[2], $z[3]) || false;',
  136. '<?php $d = isset($z[1]) && isset($z[2]) && isset($z[3]) || false;',
  137. ];
  138. yield [
  139. '<?php
  140. $a = isset($a, $b) && isset($c) === false;
  141. $a = isset($a, $b) && isset($c) | false;
  142. $a = isset($a, $b) && isset($c) ^ false;
  143. ',
  144. '<?php
  145. $a = isset($a) && isset($b) && isset($c) === false;
  146. $a = isset($a) && isset($b) && isset($c) | false;
  147. $a = isset($a) && isset($b) && isset($c) ^ false;
  148. ',
  149. ];
  150. // don't fix cases
  151. yield [
  152. '<?php $a = isset($a) && $a->isset(); $b=isset($d);',
  153. ];
  154. yield [
  155. '<?php
  156. $a = !isset($a) && isset($b);
  157. $a = !isset($a) && !isset($b);
  158. $a = isset($a) && !isset($b);
  159. //
  160. $a = isset($b) && isset($c) === false;
  161. $a = isset($b) && isset($c) | false;
  162. $a = isset($b) && isset($c) ^ false;
  163. //
  164. $a = false === isset($b) && isset($c);
  165. $a = false | isset($b) && isset($c);
  166. $a = false ^ isset($b) && isset($c);
  167. ',
  168. ];
  169. yield [
  170. '<?php $a = !isset($container[$a]) && isset($container[$b]) && !isset($container[$c]) && isset($container[$d]);',
  171. ];
  172. yield 'anonymous class' => [
  173. '<?php
  174. class A {function isset(){}} // isset($b) && isset($c)
  175. $a = new A(); /** isset($b) && isset($c) */
  176. if (isset($b) && $a->isset()) {}
  177. ',
  178. ];
  179. }
  180. }