IsNullFixerTest.php 9.7 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\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->expectExceptionMessage('[is_null] Invalid configuration: The option "use_yoda_style" with value -1 is expected to be of type "bool", but is of type "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. $configuration = $this->getObjectAttribute($this->fixer, 'configuration');
  48. $this->assertFalse($configuration['use_yoda_style']);
  49. }
  50. /**
  51. * @dataProvider provideFixCases
  52. *
  53. * @param string $expected
  54. * @param null|string $input
  55. */
  56. public function testFix($expected, $input = null)
  57. {
  58. $this->doTest($expected, $input);
  59. }
  60. public function provideFixCases()
  61. {
  62. $multiLinePatternToFix = <<<'FIX'
  63. <?php $x =
  64. is_null
  65. (
  66. json_decode
  67. (
  68. $x
  69. )
  70. )
  71. ;
  72. FIX;
  73. $multiLinePatternFixed = <<<'FIXED'
  74. <?php $x =
  75. null === json_decode
  76. (
  77. $x
  78. )
  79. ;
  80. FIXED;
  81. return [
  82. ['<?php $x = "is_null";'],
  83. ['<?php $x = ClassA::is_null(json_decode($x));'],
  84. ['<?php $x = ScopeA\\is_null(json_decode($x));'],
  85. ['<?php $x = namespace\\is_null(json_decode($x));'],
  86. ['<?php $x = $object->is_null(json_decode($x));'],
  87. ['<?php $x = new \\is_null(json_decode($x));'],
  88. ['<?php $x = new is_null(json_decode($x));'],
  89. ['<?php $x = new ScopeB\\is_null(json_decode($x));'],
  90. ['<?php is_nullSmth(json_decode($x));'],
  91. ['<?php smth_is_null(json_decode($x));'],
  92. ['<?php namespace Foo; function &is_null($x) { return null === $x; }'],
  93. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  94. ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
  95. ['<?php "test" . "is_null" . "in concatenation";'],
  96. ['<?php $x = null === json_decode($x);', '<?php $x = is_null(json_decode($x));'],
  97. ['<?php $x = null !== json_decode($x);', '<?php $x = !is_null(json_decode($x));'],
  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).".dist";', '<?php $x = is_null(json_decode($x)).".dist";'],
  105. ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !is_null(json_decode($x)).".dist";'],
  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. [$multiLinePatternFixed, $multiLinePatternToFix],
  109. [
  110. '<?php $x = /**/null === /**/ /** x*//**//** */json_decode($x)/***//*xx*/;',
  111. '<?php $x = /**/is_null/**/ /** x*/(/**//** */json_decode($x)/***/)/*xx*/;',
  112. ],
  113. [
  114. '<?php $x = null === (null === $x ? z(null === $y) : z(null === $z));',
  115. '<?php $x = is_null(is_null($x) ? z(is_null($y)) : z(is_null($z)));',
  116. ],
  117. [
  118. '<?php $x = null === ($x = array());',
  119. '<?php $x = is_null($x = array());',
  120. ],
  121. [
  122. '<?php null === a(null === a(null === a(null === b())));',
  123. '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
  124. ],
  125. [
  126. '<?php $d= null === ($a)/*=?*/?>',
  127. "<?php \$d= is_null(\n(\$a)/*=?*/\n)?>",
  128. ],
  129. [
  130. '<?php is_null()?>',
  131. ],
  132. // edge cases: is_null wrapped into a binary operations
  133. [
  134. '<?php $result = (false === (null === $a)); ?>',
  135. '<?php $result = (false === is_null($a)); ?>',
  136. ],
  137. [
  138. '<?php $result = ((null === $a) === false); ?>',
  139. '<?php $result = (is_null($a) === false); ?>',
  140. ],
  141. [
  142. '<?php $result = (false !== (null === $a)); ?>',
  143. '<?php $result = (false !== is_null($a)); ?>',
  144. ],
  145. [
  146. '<?php $result = ((null === $a) !== false); ?>',
  147. '<?php $result = (is_null($a) !== false); ?>',
  148. ],
  149. [
  150. '<?php $result = (false == (null === $a)); ?>',
  151. '<?php $result = (false == is_null($a)); ?>',
  152. ],
  153. [
  154. '<?php $result = ((null === $a) == false); ?>',
  155. '<?php $result = (is_null($a) == false); ?>',
  156. ],
  157. [
  158. '<?php $result = (false != (null === $a)); ?>',
  159. '<?php $result = (false != is_null($a)); ?>',
  160. ],
  161. [
  162. '<?php $result = ((null === $a) != false); ?>',
  163. '<?php $result = (is_null($a) != false); ?>',
  164. ],
  165. [
  166. '<?php $result = (false <> (null === $a)); ?>',
  167. '<?php $result = (false <> is_null($a)); ?>',
  168. ],
  169. [
  170. '<?php $result = ((null === $a) <> false); ?>',
  171. '<?php $result = (is_null($a) <> false); ?>',
  172. ],
  173. [
  174. '<?php if (null === $x) echo "foo"; ?>',
  175. '<?php if (is_null($x)) echo "foo"; ?>',
  176. ],
  177. // check with logical operator
  178. [
  179. '<?php if (null === $x && $y) echo "foo"; ?>',
  180. '<?php if (is_null($x) && $y) echo "foo"; ?>',
  181. ],
  182. [
  183. '<?php if (null === $x || $y) echo "foo"; ?>',
  184. '<?php if (is_null($x) || $y) echo "foo"; ?>',
  185. ],
  186. [
  187. '<?php if (null === $x xor $y) echo "foo"; ?>',
  188. '<?php if (is_null($x) xor $y) echo "foo"; ?>',
  189. ],
  190. [
  191. '<?php if (null === $x and $y) echo "foo"; ?>',
  192. '<?php if (is_null($x) and $y) echo "foo"; ?>',
  193. ],
  194. [
  195. '<?php if (null === $x or $y) echo "foo"; ?>',
  196. '<?php if (is_null($x) or $y) echo "foo"; ?>',
  197. ],
  198. [
  199. '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
  200. '<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
  201. ],
  202. ];
  203. }
  204. /**
  205. * @group legacy
  206. * @expectedDeprecation Option "use_yoda_style" for rule "is_null" is deprecated and will be removed in version 3.0. Use "yoda_style" fixer instead.
  207. *
  208. * @dataProvider provideNonYodaFixCases
  209. *
  210. * @param string $expected
  211. * @param null|string $input
  212. */
  213. public function testNonYodaFix($expected, $input)
  214. {
  215. $this->fixer->configure(['use_yoda_style' => false]);
  216. $this->doTest($expected, $input);
  217. }
  218. public function provideNonYodaFixCases()
  219. {
  220. return [
  221. [
  222. '<?php $x = $y === null;', '<?php $x = is_null($y);',
  223. ],
  224. [
  225. '<?php $b = a(a(a(b() === null) === null) === null) === null;',
  226. '<?php $b = \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
  227. ],
  228. [
  229. '<?php if ($x === null && $y) echo "foo";',
  230. '<?php if (is_null($x) && $y) echo "foo";',
  231. ],
  232. [
  233. '<?php $x = ($x = array()) === null;',
  234. '<?php $x = is_null($x = array());',
  235. ],
  236. [
  237. '<?php while (($nextMaxId = $myTimeline->getNextMaxId()) === null);',
  238. '<?php while (is_null($nextMaxId = $myTimeline->getNextMaxId()));',
  239. ],
  240. ];
  241. }
  242. }