IsNullFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\LanguageConstruct;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Vladimir Reznichenko <kalessil@gmail.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer
  19. */
  20. final class IsNullFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @dataProvider provideFixCases
  24. *
  25. * @param string $expected
  26. * @param null|string $input
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. $multiLinePatternToFix = <<<'FIX'
  35. <?php $x =
  36. is_null
  37. (
  38. json_decode
  39. (
  40. $x
  41. )
  42. )
  43. ;
  44. FIX;
  45. $multiLinePatternFixed = <<<'FIXED'
  46. <?php $x =
  47. null === json_decode
  48. (
  49. $x
  50. )
  51. ;
  52. FIXED;
  53. return [
  54. ['<?php $x = "is_null";'],
  55. ['<?php $x = ClassA::is_null(json_decode($x));'],
  56. ['<?php $x = ScopeA\\is_null(json_decode($x));'],
  57. ['<?php $x = namespace\\is_null(json_decode($x));'],
  58. ['<?php $x = $object->is_null(json_decode($x));'],
  59. ['<?php $x = new \\is_null(json_decode($x));'],
  60. ['<?php $x = new is_null(json_decode($x));'],
  61. ['<?php $x = new ScopeB\\is_null(json_decode($x));'],
  62. ['<?php is_nullSmth(json_decode($x));'],
  63. ['<?php smth_is_null(json_decode($x));'],
  64. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  65. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  66. ['<?php "test" . "is_null" . "in concatenation";'],
  67. ['<?php $x = null === json_decode($x);', '<?php $x = is_null(json_decode($x));'],
  68. ['<?php $x = null !== json_decode($x);', '<?php $x = !is_null(json_decode($x));'],
  69. ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null(json_decode($x));'],
  70. ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null( json_decode($x) );'],
  71. ['<?php $x = null === json_decode($x);', '<?php $x = \\is_null(json_decode($x));'],
  72. ['<?php $x = null !== json_decode($x);', '<?php $x = !\\is_null(json_decode($x));'],
  73. ['<?php $x = null !== json_decode($x);', '<?php $x = ! \\is_null(json_decode($x));'],
  74. ['<?php $x = null !== json_decode($x);', '<?php $x = ! \\is_null( json_decode($x) );'],
  75. ['<?php $x = null === json_decode($x).".dist";', '<?php $x = is_null(json_decode($x)).".dist";'],
  76. ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !is_null(json_decode($x)).".dist";'],
  77. ['<?php $x = null === json_decode($x).".dist";', '<?php $x = \\is_null(json_decode($x)).".dist";'],
  78. ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !\\is_null(json_decode($x)).".dist";'],
  79. [$multiLinePatternFixed, $multiLinePatternToFix],
  80. [
  81. '<?php $x = /**/null === /**/ /** x*//**//** */json_decode($x)/***//*xx*/;',
  82. '<?php $x = /**/is_null/**/ /** x*/(/**//** */json_decode($x)/***/)/*xx*/;',
  83. ],
  84. [
  85. '<?php $x = null === (null === $x ? z(null === $y) : z(null === $z));',
  86. '<?php $x = is_null(is_null($x) ? z(is_null($y)) : z(is_null($z)));',
  87. ],
  88. [
  89. '<?php $x = null === ($x = array());',
  90. '<?php $x = is_null($x = array());',
  91. ],
  92. [
  93. '<?php null === a(null === a(null === a(null === b())));',
  94. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
  95. ],
  96. [
  97. '<?php $d= null === ($a)/*=?*/?>',
  98. "<?php \$d= is_null(\n(\$a)/*=?*/\n)?>",
  99. ],
  100. [
  101. '<?php is_null()?>',
  102. ],
  103. // edge cases: is_null wrapped into a binary operations
  104. [
  105. '<?php $result = (false === (null === $a)); ?>',
  106. '<?php $result = (false === is_null($a)); ?>',
  107. ],
  108. [
  109. '<?php $result = ((null === $a) === false); ?>',
  110. '<?php $result = (is_null($a) === false); ?>',
  111. ],
  112. [
  113. '<?php $result = (false !== (null === $a)); ?>',
  114. '<?php $result = (false !== is_null($a)); ?>',
  115. ],
  116. [
  117. '<?php $result = ((null === $a) !== false); ?>',
  118. '<?php $result = (is_null($a) !== false); ?>',
  119. ],
  120. [
  121. '<?php $result = (false == (null === $a)); ?>',
  122. '<?php $result = (false == is_null($a)); ?>',
  123. ],
  124. [
  125. '<?php $result = ((null === $a) == false); ?>',
  126. '<?php $result = (is_null($a) == false); ?>',
  127. ],
  128. [
  129. '<?php $result = (false != (null === $a)); ?>',
  130. '<?php $result = (false != is_null($a)); ?>',
  131. ],
  132. [
  133. '<?php $result = ((null === $a) != false); ?>',
  134. '<?php $result = (is_null($a) != false); ?>',
  135. ],
  136. [
  137. '<?php $result = (false <> (null === $a)); ?>',
  138. '<?php $result = (false <> is_null($a)); ?>',
  139. ],
  140. [
  141. '<?php $result = ((null === $a) <> false); ?>',
  142. '<?php $result = (is_null($a) <> false); ?>',
  143. ],
  144. [
  145. '<?php if (null === $x) echo "foo"; ?>',
  146. '<?php if (is_null($x)) echo "foo"; ?>',
  147. ],
  148. // check with logical operator
  149. [
  150. '<?php if (null === $x && $y) echo "foo"; ?>',
  151. '<?php if (is_null($x) && $y) echo "foo"; ?>',
  152. ],
  153. [
  154. '<?php if (null === $x || $y) echo "foo"; ?>',
  155. '<?php if (is_null($x) || $y) echo "foo"; ?>',
  156. ],
  157. [
  158. '<?php if (null === $x xor $y) echo "foo"; ?>',
  159. '<?php if (is_null($x) xor $y) echo "foo"; ?>',
  160. ],
  161. [
  162. '<?php if (null === $x and $y) echo "foo"; ?>',
  163. '<?php if (is_null($x) and $y) echo "foo"; ?>',
  164. ],
  165. [
  166. '<?php if (null === $x or $y) echo "foo"; ?>',
  167. '<?php if (is_null($x) or $y) echo "foo"; ?>',
  168. ],
  169. [
  170. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  171. '<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
  172. ],
  173. ];
  174. }
  175. }