IsNullFixerTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * @author Vladimir Reznichenko <kalessil@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer
  20. */
  21. final class IsNullFixerTest 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. public function provideFixCases(): array
  31. {
  32. $multiLinePatternToFix = <<<'FIX'
  33. <?php $x =
  34. is_null
  35. (
  36. json_decode
  37. (
  38. $x
  39. )
  40. )
  41. ;
  42. FIX;
  43. $multiLinePatternFixed = <<<'FIXED'
  44. <?php $x =
  45. null === json_decode
  46. (
  47. $x
  48. )
  49. ;
  50. FIXED;
  51. return [
  52. ['<?php $x = "is_null";'],
  53. ['<?php $x = ClassA::is_null(json_decode($x));'],
  54. ['<?php $x = ScopeA\\is_null(json_decode($x));'],
  55. ['<?php $x = namespace\\is_null(json_decode($x));'],
  56. ['<?php $x = $object->is_null(json_decode($x));'],
  57. ['<?php $x = new \\is_null(json_decode($x));'],
  58. ['<?php $x = new is_null(json_decode($x));'],
  59. ['<?php $x = new ScopeB\\is_null(json_decode($x));'],
  60. ['<?php is_nullSmth(json_decode($x));'],
  61. ['<?php smth_is_null(json_decode($x));'],
  62. ['<?php namespace Foo; function &is_null($x) { return null === $x; }'],
  63. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  64. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  65. ['<?php "test" . "is_null" . "in concatenation";'],
  66. ['<?php $x = null === json_decode($x);', '<?php $x = is_null(json_decode($x));'],
  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).".dist";', '<?php $x = is_null(json_decode($x)).".dist";'],
  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. [$multiLinePatternFixed, $multiLinePatternToFix],
  79. [
  80. '<?php $x = /**/null === /**/ /** x*//**//** */json_decode($x)/***//*xx*/;',
  81. '<?php $x = /**/is_null/**/ /** x*/(/**//** */json_decode($x)/***/)/*xx*/;',
  82. ],
  83. [
  84. '<?php $x = null === (null === $x ? z(null === $y) : z(null === $z));',
  85. '<?php $x = is_null(is_null($x) ? z(is_null($y)) : z(is_null($z)));',
  86. ],
  87. [
  88. '<?php $x = null === ($x = array());',
  89. '<?php $x = is_null($x = array());',
  90. ],
  91. [
  92. '<?php null === a(null === a(null === a(null === b())));',
  93. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
  94. ],
  95. [
  96. '<?php $d= null === ($a)/*=?*/?>',
  97. "<?php \$d= is_null(\n(\$a)/*=?*/\n)?>",
  98. ],
  99. [
  100. '<?php is_null()?>',
  101. ],
  102. // edge cases: is_null wrapped into a binary operations
  103. [
  104. '<?php $result = (false === (null === $a)); ?>',
  105. '<?php $result = (false === is_null($a)); ?>',
  106. ],
  107. [
  108. '<?php $result = ((null === $a) === false); ?>',
  109. '<?php $result = (is_null($a) === false); ?>',
  110. ],
  111. [
  112. '<?php $result = (false !== (null === $a)); ?>',
  113. '<?php $result = (false !== is_null($a)); ?>',
  114. ],
  115. [
  116. '<?php $result = ((null === $a) !== false); ?>',
  117. '<?php $result = (is_null($a) !== false); ?>',
  118. ],
  119. [
  120. '<?php $result = (false == (null === $a)); ?>',
  121. '<?php $result = (false == is_null($a)); ?>',
  122. ],
  123. [
  124. '<?php $result = ((null === $a) == false); ?>',
  125. '<?php $result = (is_null($a) == false); ?>',
  126. ],
  127. [
  128. '<?php $result = (false != (null === $a)); ?>',
  129. '<?php $result = (false != is_null($a)); ?>',
  130. ],
  131. [
  132. '<?php $result = ((null === $a) != false); ?>',
  133. '<?php $result = (is_null($a) != false); ?>',
  134. ],
  135. [
  136. '<?php $result = (false <> (null === $a)); ?>',
  137. '<?php $result = (false <> is_null($a)); ?>',
  138. ],
  139. [
  140. '<?php $result = ((null === $a) <> false); ?>',
  141. '<?php $result = (is_null($a) <> false); ?>',
  142. ],
  143. [
  144. '<?php if (null === $x) echo "foo"; ?>',
  145. '<?php if (is_null($x)) echo "foo"; ?>',
  146. ],
  147. // check with logical operator
  148. [
  149. '<?php if (null === $x && $y) echo "foo"; ?>',
  150. '<?php if (is_null($x) && $y) echo "foo"; ?>',
  151. ],
  152. [
  153. '<?php if (null === $x || $y) echo "foo"; ?>',
  154. '<?php if (is_null($x) || $y) echo "foo"; ?>',
  155. ],
  156. [
  157. '<?php if (null === $x xor $y) echo "foo"; ?>',
  158. '<?php if (is_null($x) xor $y) echo "foo"; ?>',
  159. ],
  160. [
  161. '<?php if (null === $x and $y) echo "foo"; ?>',
  162. '<?php if (is_null($x) and $y) echo "foo"; ?>',
  163. ],
  164. [
  165. '<?php if (null === $x or $y) echo "foo"; ?>',
  166. '<?php if (is_null($x) or $y) echo "foo"; ?>',
  167. ],
  168. [
  169. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  170. '<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
  171. ],
  172. // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
  173. [
  174. '<?php null === ($a ? $x : $y);',
  175. '<?php is_null($a ? $x : $y);',
  176. ],
  177. [
  178. '<?php $a === (null === $x);',
  179. '<?php $a === is_null($x);',
  180. ],
  181. [
  182. '<?php $a === (null === ($a ? $x : $y));',
  183. '<?php $a === is_null($a ? $x : $y);',
  184. ],
  185. [
  186. '<?php null !== ($a ?? null);',
  187. '<?php !is_null($a ?? null);',
  188. ],
  189. ];
  190. }
  191. /**
  192. * @requires PHP 7.3
  193. * @dataProvider provideFix73Cases
  194. */
  195. public function testFix73(string $expected, string $input): void
  196. {
  197. $this->doTest($expected, $input);
  198. }
  199. public function provideFix73Cases(): array
  200. {
  201. return [
  202. [
  203. '<?php null === $x;',
  204. '<?php is_null($x, );',
  205. ],
  206. [
  207. '<?php null === $x;',
  208. '<?php is_null( $x , );',
  209. ],
  210. [
  211. '<?php null === a(null === a(null === a(null === b(), ), ), );',
  212. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b(), ), ), ), ), ), ), );',
  213. ],
  214. [
  215. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  216. '<?php if ((is_null($u, ) or $v) and ($w || is_null($x, )) xor (!is_null($y, ) and $z)) echo "foo"; ?>',
  217. ],
  218. // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
  219. [
  220. '<?php null === ($a ? $x : $y );',
  221. '<?php is_null($a ? $x : $y, );',
  222. ],
  223. [
  224. '<?php $a === (null === $x);',
  225. '<?php $a === is_null($x, );',
  226. ],
  227. [
  228. '<?php $a === (null === ($a ? $x : $y ));',
  229. '<?php $a === is_null($a ? $x : $y, );',
  230. ],
  231. ];
  232. }
  233. }