IsNullFixerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 namespace Foo; function &is_null($x) { return null === $x; }'],
  65. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  66. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  67. ['<?php "test" . "is_null" . "in concatenation";'],
  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);', '<?php $x = ! \\is_null( json_decode($x) );'],
  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. ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !\\is_null(json_decode($x)).".dist";'],
  80. [$multiLinePatternFixed, $multiLinePatternToFix],
  81. [
  82. '<?php $x = /**/null === /**/ /** x*//**//** */json_decode($x)/***//*xx*/;',
  83. '<?php $x = /**/is_null/**/ /** x*/(/**//** */json_decode($x)/***/)/*xx*/;',
  84. ],
  85. [
  86. '<?php $x = null === (null === $x ? z(null === $y) : z(null === $z));',
  87. '<?php $x = is_null(is_null($x) ? z(is_null($y)) : z(is_null($z)));',
  88. ],
  89. [
  90. '<?php $x = null === ($x = array());',
  91. '<?php $x = is_null($x = array());',
  92. ],
  93. [
  94. '<?php null === a(null === a(null === a(null === b())));',
  95. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
  96. ],
  97. [
  98. '<?php $d= null === ($a)/*=?*/?>',
  99. "<?php \$d= is_null(\n(\$a)/*=?*/\n)?>",
  100. ],
  101. [
  102. '<?php is_null()?>',
  103. ],
  104. // edge cases: is_null wrapped into a binary operations
  105. [
  106. '<?php $result = (false === (null === $a)); ?>',
  107. '<?php $result = (false === is_null($a)); ?>',
  108. ],
  109. [
  110. '<?php $result = ((null === $a) === false); ?>',
  111. '<?php $result = (is_null($a) === false); ?>',
  112. ],
  113. [
  114. '<?php $result = (false !== (null === $a)); ?>',
  115. '<?php $result = (false !== is_null($a)); ?>',
  116. ],
  117. [
  118. '<?php $result = ((null === $a) !== false); ?>',
  119. '<?php $result = (is_null($a) !== false); ?>',
  120. ],
  121. [
  122. '<?php $result = (false == (null === $a)); ?>',
  123. '<?php $result = (false == is_null($a)); ?>',
  124. ],
  125. [
  126. '<?php $result = ((null === $a) == false); ?>',
  127. '<?php $result = (is_null($a) == false); ?>',
  128. ],
  129. [
  130. '<?php $result = (false != (null === $a)); ?>',
  131. '<?php $result = (false != is_null($a)); ?>',
  132. ],
  133. [
  134. '<?php $result = ((null === $a) != false); ?>',
  135. '<?php $result = (is_null($a) != false); ?>',
  136. ],
  137. [
  138. '<?php $result = (false <> (null === $a)); ?>',
  139. '<?php $result = (false <> is_null($a)); ?>',
  140. ],
  141. [
  142. '<?php $result = ((null === $a) <> false); ?>',
  143. '<?php $result = (is_null($a) <> false); ?>',
  144. ],
  145. [
  146. '<?php if (null === $x) echo "foo"; ?>',
  147. '<?php if (is_null($x)) echo "foo"; ?>',
  148. ],
  149. // check with logical operator
  150. [
  151. '<?php if (null === $x && $y) echo "foo"; ?>',
  152. '<?php if (is_null($x) && $y) echo "foo"; ?>',
  153. ],
  154. [
  155. '<?php if (null === $x || $y) echo "foo"; ?>',
  156. '<?php if (is_null($x) || $y) echo "foo"; ?>',
  157. ],
  158. [
  159. '<?php if (null === $x xor $y) echo "foo"; ?>',
  160. '<?php if (is_null($x) xor $y) echo "foo"; ?>',
  161. ],
  162. [
  163. '<?php if (null === $x and $y) echo "foo"; ?>',
  164. '<?php if (is_null($x) and $y) echo "foo"; ?>',
  165. ],
  166. [
  167. '<?php if (null === $x or $y) echo "foo"; ?>',
  168. '<?php if (is_null($x) or $y) echo "foo"; ?>',
  169. ],
  170. [
  171. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  172. '<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
  173. ],
  174. // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
  175. [
  176. '<?php null === ($a ? $x : $y);',
  177. '<?php is_null($a ? $x : $y);',
  178. ],
  179. [
  180. '<?php $a === (null === $x);',
  181. '<?php $a === is_null($x);',
  182. ],
  183. [
  184. '<?php $a === (null === ($a ? $x : $y));',
  185. '<?php $a === is_null($a ? $x : $y);',
  186. ],
  187. ];
  188. }
  189. /**
  190. * @requires PHP 7.0
  191. */
  192. public function testFix70()
  193. {
  194. $this->doTest(
  195. '<?php null !== ($a ?? null);',
  196. '<?php !is_null($a ?? null);'
  197. );
  198. }
  199. /**
  200. * @param string $expected
  201. * @param string $input
  202. *
  203. * @requires PHP 7.3
  204. * @dataProvider provideFix73Cases
  205. */
  206. public function testFix73($expected, $input)
  207. {
  208. $this->doTest($expected, $input);
  209. }
  210. public function provideFix73Cases()
  211. {
  212. return [
  213. [
  214. '<?php null === $x;',
  215. '<?php is_null($x, );',
  216. ],
  217. [
  218. '<?php null === $x;',
  219. '<?php is_null( $x , );',
  220. ],
  221. [
  222. '<?php null === a(null === a(null === a(null === b(), ), ), );',
  223. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b(), ), ), ), ), ), ), );',
  224. ],
  225. [
  226. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  227. '<?php if ((is_null($u, ) or $v) and ($w || is_null($x, )) xor (!is_null($y, ) and $z)) echo "foo"; ?>',
  228. ],
  229. // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
  230. [
  231. '<?php null === ($a ? $x : $y );',
  232. '<?php is_null($a ? $x : $y, );',
  233. ],
  234. [
  235. '<?php $a === (null === $x);',
  236. '<?php $a === is_null($x, );',
  237. ],
  238. [
  239. '<?php $a === (null === ($a ? $x : $y ));',
  240. '<?php $a === is_null($a ? $x : $y, );',
  241. ],
  242. ];
  243. }
  244. }