TernaryToNullCoalescingFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Operator\TernaryToNullCoalescingFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\TernaryToNullCoalescingFixer>
  22. */
  23. final class TernaryToNullCoalescingFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield // Do not fix cases.
  38. ['<?php $x = isset($a) ? $a[1] : null;'];
  39. yield ['<?php $x = isset($a) and $a ? $a : "";'];
  40. yield ['<?php $x = "isset($a) ? $a : null";'];
  41. yield ['<?php $x = isset($a) ? $$a : null;'];
  42. yield ['<?php $x = isset($this) ? $this : null;'];
  43. yield ['<?php $x = isset($A) ? $a : null;']; // different case
  44. yield ['<?php $x = isset($a) ? "$a" : null;'];
  45. yield ['<?php $x = isset($a) ?: false;'];
  46. yield ['<?php $x = $y ?? isset($a) ? $a : null;'];
  47. yield ['<?php $x = isset($a) ?: $b;'];
  48. yield ['<?php $x = isset($a, $b) ? $a : null;'];
  49. yield ['<?php $x = $a && isset($b) ? $b : null;'];
  50. yield ['<?php $x = $a & isset($b) ? $b : null;'];
  51. yield ['<?php $x = ! isset($a) ? $a : null;'];
  52. yield ['<?php $x = false === isset($a) ? $a : 2;'];
  53. yield ['<?php $x = 4 * isset($a) ? $a : 2;'];
  54. yield ['<?php $x = "4" . isset($a) ? $a : 2;'];
  55. yield ['<?php $x = 3 ** isset($a) ? $a : 2;'];
  56. yield ['<?php $x = 1 | isset($a) ? $a : 2;'];
  57. yield ['<?php $x = (array) isset($a) ? $a : 2;'];
  58. yield ['<?php $x = isset($a[++$i]) ? $a[++$i] : null;'];
  59. yield ['<?php $x = function(){isset($a[yield]) ? $a[yield] : null;};'];
  60. yield ['<?php $x = isset($a[foo()]) ? $a[foo()] : null;'];
  61. yield ['<?php $x = isset($a[$callback()]) ? $a[$callback()] : null;'];
  62. yield ['<?php $y = isset($a) ? 2**3 : 3**2;'];
  63. yield ['<?php $x = function(){isset($a[yield from $a]) ? $a[yield from $a] : null;};'];
  64. // Fix cases.
  65. yield 'Common fix case (I).' => [
  66. '<?php $x = $a ?? null;',
  67. '<?php $x = isset($a) ? $a : null;',
  68. ];
  69. yield 'Common fix case (II).' => [
  70. '<?php $x = $a[0] ?? 1;',
  71. '<?php $x = isset($a[0]) ? $a[0] : 1;',
  72. ];
  73. yield 'Minimal number of tokens case.' => [
  74. '<?php
  75. $x=$a??null?>',
  76. '<?php
  77. $x=isset($a)?$a:null?>',
  78. ];
  79. yield [
  80. '<?php $x = $a ?? 1; $y = isset($b) ? "b" : 2; $x = $c ?? 3;',
  81. '<?php $x = isset($a) ? $a : 1; $y = isset($b) ? "b" : 2; $x = isset($c) ? $c : 3;',
  82. ];
  83. yield [
  84. '<?php $x = $a[ $b[ "c" ]] ?? null;',
  85. '<?php $x = isset ( $a[$b["c"]]) ?$a[ $b[ "c" ]] : null;',
  86. ];
  87. yield [
  88. '<?php $x = $a ?? $b[func(1, true)];',
  89. '<?php $x = isset($a) ? $a : $b[func(1, true)];',
  90. ];
  91. yield [
  92. '<?php $x = $a ?? ($b ?? "");',
  93. '<?php $x = isset($a) ? $a : (isset($b) ? $b : "");',
  94. ];
  95. yield [
  96. '<?php $x = ($a ?? isset($b)) ? $b : "";',
  97. '<?php $x = (isset($a) ? $a : isset($b)) ? $b : "";',
  98. ];
  99. yield [
  100. '<?php $x = $obj->a ?? null;',
  101. '<?php $x = isset($obj->a) ? $obj->a : null;',
  102. ];
  103. yield [
  104. '<?php $x = $obj->a->b ?? null;',
  105. '<?php $x = isset($obj->a->b) ? $obj->a->b : null;',
  106. ];
  107. yield [
  108. '<?php $x = $obj->$a ?? null;',
  109. '<?php $x = isset($obj->$a) ? $obj->$a : null;',
  110. ];
  111. yield [
  112. '<?php $x = $obj->a->$b ?? null;',
  113. '<?php $x = isset($obj->a->$b) ? $obj->a->$b : null;',
  114. ];
  115. yield [
  116. '<?php $x = $obj->a[3] ?? null;',
  117. '<?php $x = isset($obj->a[3]) ? $obj->a[3] : null;',
  118. ];
  119. yield [
  120. '<?php $x = $obj->a[\'foo\'] ?? null;',
  121. '<?php $x = isset($obj->a[\'foo\']) ? $obj->a[\'foo\'] : null;',
  122. ];
  123. yield [
  124. '<?php $x = $obj->a[$b] ?? null;',
  125. '<?php $x = isset($obj->a[$b]) ? $obj->a[$b] : null;',
  126. ];
  127. yield [
  128. '<?php $x = $obj->a[$b][\'foo\'] ?? null;',
  129. '<?php $x = isset($obj->a[$b][\'foo\']) ? $obj->a[$b][\'foo\'] : null;',
  130. ];
  131. yield [
  132. '<?php $x = $obj->a[$b[\'foo\']] ?? null;',
  133. '<?php $x = isset($obj->a[$b[\'foo\']]) ? $obj->a[$b[\'foo\']] : null;',
  134. ];
  135. yield [
  136. '<?php $x = $a[$obj->b] ?? null;',
  137. '<?php $x = isset($a[$obj->b]) ? $a[$obj->b] : null;',
  138. ];
  139. yield [
  140. '<?php $x = Foo::A[$b] ?? null;',
  141. '<?php $x = isset(Foo::A[$b]) ? Foo::A[$b] : null;',
  142. ];
  143. yield [
  144. '<?php $x = $a[Foo::B] ?? null;',
  145. '<?php $x = isset($a[Foo::B]) ? $a[Foo::B] : null;',
  146. ];
  147. yield [
  148. '<?php $x = (
  149. // c1
  150. // c2
  151. // c3
  152. $a
  153. // c4
  154. ??
  155. // c5
  156. null
  157. /* c6 */
  158. )
  159. # c7
  160. ;',
  161. '<?php $x = (
  162. // c1
  163. isset($a)
  164. // c2
  165. ?
  166. // c3
  167. $a
  168. // c4
  169. :
  170. // c5
  171. null
  172. /* c6 */
  173. )
  174. # c7
  175. ;',
  176. ];
  177. yield [
  178. '<?php $x = $THIS ?? null;',
  179. '<?php $x = isset($THIS) ? $THIS : null;',
  180. ];
  181. }
  182. /**
  183. * @dataProvider provideFixPre80Cases
  184. *
  185. * @requires PHP <8.0
  186. */
  187. public function testFixPre80(string $expected, ?string $input = null): void
  188. {
  189. $this->doTest($expected, $input);
  190. }
  191. /**
  192. * @return iterable<array{0: string, 1?: string}>
  193. */
  194. public static function provideFixPre80Cases(): iterable
  195. {
  196. yield ['<?php $x = $a ? $a : isset($b) ? $b : isset($c) ? $c : "";'];
  197. yield [
  198. '<?php $x = $a ?? isset($b) ? $b : isset($c) ? $c : "";',
  199. '<?php $x = isset($a) ? $a : isset($b) ? $b : isset($c) ? $c : "";',
  200. ];
  201. yield [
  202. '<?php $x = /*a1*//*a2*/ /*b*/ $a /*c*/ ?? /*d*/ isset($b) /*e*/ ? /*f*/ $b /*g*/ : /*h*/ isset($c) /*i*/ ? /*j*/ $c /*k*/ : /*l*/ "";',
  203. '<?php $x = isset($a) /*a1*//*a2*/ ? /*b*/ $a /*c*/ : /*d*/ isset($b) /*e*/ ? /*f*/ $b /*g*/ : /*h*/ isset($c) /*i*/ ? /*j*/ $c /*k*/ : /*l*/ "";',
  204. ];
  205. }
  206. }