IsNullFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\Fixer\LanguageConstruct\IsNullFixer;
  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. public function testConfigurationWrongOption()
  24. {
  25. $fixer = new IsNullFixer();
  26. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  27. $this->expectExceptionMessage('[is_null] Invalid configuration: The option "yoda" does not exist.');
  28. $fixer->configure(['yoda' => true]);
  29. }
  30. /**
  31. * @group legacy
  32. * @expectedDeprecation Option "use_yoda_style" for rule "is_null" is deprecated and will be removed in version 3.0. Use "yoda_style" fixer instead.
  33. */
  34. public function testConfigurationWrongValue()
  35. {
  36. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  37. $this->expectExceptionMessageMatches('#^\[is_null\] Invalid configuration: The option "use_yoda_style" with value -1 is expected to be of type "bool", but is of type "(int|integer)"\.$#');
  38. $this->fixer->configure(['use_yoda_style' => -1]);
  39. }
  40. /**
  41. * @group legacy
  42. * @expectedDeprecation Option "use_yoda_style" for rule "is_null" is deprecated and will be removed in version 3.0. Use "yoda_style" fixer instead.
  43. */
  44. public function testCorrectConfiguration()
  45. {
  46. $this->fixer->configure(['use_yoda_style' => false]);
  47. $reflectionProperty = new \ReflectionProperty($this->fixer, 'configuration');
  48. $reflectionProperty->setAccessible(true);
  49. $configuration = $reflectionProperty->getValue($this->fixer);
  50. static::assertFalse($configuration['use_yoda_style']);
  51. }
  52. /**
  53. * @dataProvider provideFixCases
  54. *
  55. * @param string $expected
  56. * @param null|string $input
  57. */
  58. public function testFix($expected, $input = null)
  59. {
  60. $this->doTest($expected, $input);
  61. }
  62. public function provideFixCases()
  63. {
  64. $multiLinePatternToFix = <<<'FIX'
  65. <?php $x =
  66. is_null
  67. (
  68. json_decode
  69. (
  70. $x
  71. )
  72. )
  73. ;
  74. FIX;
  75. $multiLinePatternFixed = <<<'FIXED'
  76. <?php $x =
  77. null === json_decode
  78. (
  79. $x
  80. )
  81. ;
  82. FIXED;
  83. return [
  84. ['<?php $x = "is_null";'],
  85. ['<?php $x = ClassA::is_null(json_decode($x));'],
  86. ['<?php $x = ScopeA\\is_null(json_decode($x));'],
  87. ['<?php $x = namespace\\is_null(json_decode($x));'],
  88. ['<?php $x = $object->is_null(json_decode($x));'],
  89. ['<?php $x = new \\is_null(json_decode($x));'],
  90. ['<?php $x = new is_null(json_decode($x));'],
  91. ['<?php $x = new ScopeB\\is_null(json_decode($x));'],
  92. ['<?php is_nullSmth(json_decode($x));'],
  93. ['<?php smth_is_null(json_decode($x));'],
  94. ['<?php namespace Foo; function &is_null($x) { return null === $x; }'],
  95. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  96. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  97. ['<?php "test" . "is_null" . "in concatenation";'],
  98. ['<?php $x = null === json_decode($x);', '<?php $x = is_null(json_decode($x));'],
  99. ['<?php $x = null !== json_decode($x);', '<?php $x = !is_null(json_decode($x));'],
  100. ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null(json_decode($x));'],
  101. ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null( json_decode($x) );'],
  102. ['<?php $x = null === json_decode($x);', '<?php $x = \\is_null(json_decode($x));'],
  103. ['<?php $x = null !== json_decode($x);', '<?php $x = !\\is_null(json_decode($x));'],
  104. ['<?php $x = null !== json_decode($x);', '<?php $x = ! \\is_null(json_decode($x));'],
  105. ['<?php $x = null !== json_decode($x);', '<?php $x = ! \\is_null( json_decode($x) );'],
  106. ['<?php $x = null === json_decode($x).".dist";', '<?php $x = is_null(json_decode($x)).".dist";'],
  107. ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !is_null(json_decode($x)).".dist";'],
  108. ['<?php $x = null === json_decode($x).".dist";', '<?php $x = \\is_null(json_decode($x)).".dist";'],
  109. ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !\\is_null(json_decode($x)).".dist";'],
  110. [$multiLinePatternFixed, $multiLinePatternToFix],
  111. [
  112. '<?php $x = /**/null === /**/ /** x*//**//** */json_decode($x)/***//*xx*/;',
  113. '<?php $x = /**/is_null/**/ /** x*/(/**//** */json_decode($x)/***/)/*xx*/;',
  114. ],
  115. [
  116. '<?php $x = null === (null === $x ? z(null === $y) : z(null === $z));',
  117. '<?php $x = is_null(is_null($x) ? z(is_null($y)) : z(is_null($z)));',
  118. ],
  119. [
  120. '<?php $x = null === ($x = array());',
  121. '<?php $x = is_null($x = array());',
  122. ],
  123. [
  124. '<?php null === a(null === a(null === a(null === b())));',
  125. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
  126. ],
  127. [
  128. '<?php $d= null === ($a)/*=?*/?>',
  129. "<?php \$d= is_null(\n(\$a)/*=?*/\n)?>",
  130. ],
  131. [
  132. '<?php is_null()?>',
  133. ],
  134. // edge cases: is_null wrapped into a binary operations
  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 $result = (false !== (null === $a)); ?>',
  145. '<?php $result = (false !== is_null($a)); ?>',
  146. ],
  147. [
  148. '<?php $result = ((null === $a) !== false); ?>',
  149. '<?php $result = (is_null($a) !== false); ?>',
  150. ],
  151. [
  152. '<?php $result = (false == (null === $a)); ?>',
  153. '<?php $result = (false == is_null($a)); ?>',
  154. ],
  155. [
  156. '<?php $result = ((null === $a) == false); ?>',
  157. '<?php $result = (is_null($a) == false); ?>',
  158. ],
  159. [
  160. '<?php $result = (false != (null === $a)); ?>',
  161. '<?php $result = (false != is_null($a)); ?>',
  162. ],
  163. [
  164. '<?php $result = ((null === $a) != false); ?>',
  165. '<?php $result = (is_null($a) != false); ?>',
  166. ],
  167. [
  168. '<?php $result = (false <> (null === $a)); ?>',
  169. '<?php $result = (false <> is_null($a)); ?>',
  170. ],
  171. [
  172. '<?php $result = ((null === $a) <> false); ?>',
  173. '<?php $result = (is_null($a) <> false); ?>',
  174. ],
  175. [
  176. '<?php if (null === $x) echo "foo"; ?>',
  177. '<?php if (is_null($x)) echo "foo"; ?>',
  178. ],
  179. // check with logical operator
  180. [
  181. '<?php if (null === $x && $y) echo "foo"; ?>',
  182. '<?php if (is_null($x) && $y) echo "foo"; ?>',
  183. ],
  184. [
  185. '<?php if (null === $x || $y) echo "foo"; ?>',
  186. '<?php if (is_null($x) || $y) echo "foo"; ?>',
  187. ],
  188. [
  189. '<?php if (null === $x xor $y) echo "foo"; ?>',
  190. '<?php if (is_null($x) xor $y) echo "foo"; ?>',
  191. ],
  192. [
  193. '<?php if (null === $x and $y) echo "foo"; ?>',
  194. '<?php if (is_null($x) and $y) echo "foo"; ?>',
  195. ],
  196. [
  197. '<?php if (null === $x or $y) echo "foo"; ?>',
  198. '<?php if (is_null($x) or $y) echo "foo"; ?>',
  199. ],
  200. [
  201. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  202. '<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
  203. ],
  204. // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
  205. [
  206. '<?php null === ($a ? $x : $y);',
  207. '<?php is_null($a ? $x : $y);',
  208. ],
  209. [
  210. '<?php $a === (null === $x);',
  211. '<?php $a === is_null($x);',
  212. ],
  213. [
  214. '<?php $a === (null === ($a ? $x : $y));',
  215. '<?php $a === is_null($a ? $x : $y);',
  216. ],
  217. ];
  218. }
  219. /**
  220. * @group legacy
  221. * @expectedDeprecation Option "use_yoda_style" for rule "is_null" is deprecated and will be removed in version 3.0. Use "yoda_style" fixer instead.
  222. *
  223. * @dataProvider provideNonYodaFixCases
  224. *
  225. * @param string $expected
  226. * @param null|string $input
  227. */
  228. public function testNonYodaFix($expected, $input)
  229. {
  230. $this->fixer->configure(['use_yoda_style' => false]);
  231. $this->doTest($expected, $input);
  232. }
  233. public function provideNonYodaFixCases()
  234. {
  235. return [
  236. [
  237. '<?php $x = $y === null;', '<?php $x = is_null($y);',
  238. ],
  239. [
  240. '<?php $b = a(a(a(b() === null) === null) === null) === null;',
  241. '<?php $b = \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
  242. ],
  243. [
  244. '<?php if ($x === null && $y) echo "foo";',
  245. '<?php if (is_null($x) && $y) echo "foo";',
  246. ],
  247. [
  248. '<?php $x = ($x = array()) === null;',
  249. '<?php $x = is_null($x = array());',
  250. ],
  251. [
  252. '<?php while (($nextMaxId = $myTimeline->getNextMaxId()) === null);',
  253. '<?php while (is_null($nextMaxId = $myTimeline->getNextMaxId()));',
  254. ],
  255. ];
  256. }
  257. /**
  258. * @requires PHP 7.0
  259. */
  260. public function testFix70()
  261. {
  262. $this->doTest(
  263. '<?php null !== ($a ?? null);',
  264. '<?php !is_null($a ?? null);'
  265. );
  266. }
  267. /**
  268. * @param string $expected
  269. * @param string $input
  270. *
  271. * @requires PHP 7.3
  272. * @dataProvider provideFix73Cases
  273. */
  274. public function testFix73($expected, $input)
  275. {
  276. $this->doTest($expected, $input);
  277. }
  278. public function provideFix73Cases()
  279. {
  280. return [
  281. [
  282. '<?php null === $x;',
  283. '<?php is_null($x, );',
  284. ],
  285. [
  286. '<?php null === $x;',
  287. '<?php is_null( $x , );',
  288. ],
  289. [
  290. '<?php null === a(null === a(null === a(null === b(), ), ), );',
  291. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b(), ), ), ), ), ), ), );',
  292. ],
  293. [
  294. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  295. '<?php if ((is_null($u, ) or $v) and ($w || is_null($x, )) xor (!is_null($y, ) and $z)) echo "foo"; ?>',
  296. ],
  297. // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
  298. [
  299. '<?php null === ($a ? $x : $y );',
  300. '<?php is_null($a ? $x : $y, );',
  301. ],
  302. [
  303. '<?php $a === (null === $x);',
  304. '<?php $a === is_null($x, );',
  305. ],
  306. [
  307. '<?php $a === (null === ($a ? $x : $y ));',
  308. '<?php $a === is_null($a ? $x : $y, );',
  309. ],
  310. ];
  311. }
  312. }