123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\LanguageConstruct;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Vladimir Reznichenko <kalessil@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer>
- */
- final class IsNullFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- $multiLinePatternToFix = <<<'FIX'
- <?php $x =
- is_null
- (
- json_decode
- (
- $x
- )
- )
- ;
- FIX;
- $multiLinePatternFixed = <<<'FIXED'
- <?php $x =
- null === json_decode
- (
- $x
- )
- ;
- FIXED;
- yield ['<?php $x = "is_null";'];
- yield ['<?php $x = ClassA::is_null(json_decode($x));'];
- yield ['<?php $x = ScopeA\is_null(json_decode($x));'];
- yield ['<?php $x = namespace\is_null(json_decode($x));'];
- yield ['<?php $x = $object->is_null(json_decode($x));'];
- yield ['<?php $x = new \is_null(json_decode($x));'];
- yield ['<?php $x = new is_null(json_decode($x));'];
- yield ['<?php $x = new ScopeB\is_null(json_decode($x));'];
- yield ['<?php is_nullSmth(json_decode($x));'];
- yield ['<?php smth_is_null(json_decode($x));'];
- yield ['<?php namespace Foo; function &is_null($x) { return null === $x; }'];
- yield ['<?php "SELECT ... is_null(json_decode($x)) ...";'];
- yield ['<?php "test" . "is_null" . "in concatenation";'];
- yield ['<?php $x = null === json_decode($x);', '<?php $x = is_null(json_decode($x));'];
- yield ['<?php $x = null !== json_decode($x);', '<?php $x = !is_null(json_decode($x));'];
- yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null(json_decode($x));'];
- yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null( json_decode($x) );'];
- yield ['<?php $x = null === json_decode($x);', '<?php $x = \is_null(json_decode($x));'];
- yield ['<?php $x = null !== json_decode($x);', '<?php $x = !\is_null(json_decode($x));'];
- yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! \is_null(json_decode($x));'];
- yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! \is_null( json_decode($x) );'];
- yield ['<?php $x = null === json_decode($x).".dist";', '<?php $x = is_null(json_decode($x)).".dist";'];
- yield ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !is_null(json_decode($x)).".dist";'];
- yield ['<?php $x = null === json_decode($x).".dist";', '<?php $x = \is_null(json_decode($x)).".dist";'];
- yield ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !\is_null(json_decode($x)).".dist";'];
- yield [$multiLinePatternFixed, $multiLinePatternToFix];
- yield [
- '<?php $x = /**/null === /**/ /** x*//**//** */json_decode($x)/***//*xx*/;',
- '<?php $x = /**/is_null/**/ /** x*/(/**//** */json_decode($x)/***/)/*xx*/;',
- ];
- yield [
- '<?php $x = null === (null === $x ? z(null === $y) : z(null === $z));',
- '<?php $x = is_null(is_null($x) ? z(is_null($y)) : z(is_null($z)));',
- ];
- yield [
- '<?php $x = null === ($x = array());',
- '<?php $x = is_null($x = array());',
- ];
- yield [
- '<?php null === a(null === a(null === a(null === b())));',
- '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
- ];
- yield [
- '<?php $d= null === ($a)/*=?*/?>',
- "<?php \$d= is_null(\n(\$a)/*=?*/\n)?>",
- ];
- yield [
- '<?php is_null()?>',
- ];
- // edge cases: is_null wrapped into a binary operations
- yield [
- '<?php $result = (false === (null === $a)); ?>',
- '<?php $result = (false === is_null($a)); ?>',
- ];
- yield [
- '<?php $result = ((null === $a) === false); ?>',
- '<?php $result = (is_null($a) === false); ?>',
- ];
- yield [
- '<?php $result = (false !== (null === $a)); ?>',
- '<?php $result = (false !== is_null($a)); ?>',
- ];
- yield [
- '<?php $result = ((null === $a) !== false); ?>',
- '<?php $result = (is_null($a) !== false); ?>',
- ];
- yield [
- '<?php $result = (false == (null === $a)); ?>',
- '<?php $result = (false == is_null($a)); ?>',
- ];
- yield [
- '<?php $result = ((null === $a) == false); ?>',
- '<?php $result = (is_null($a) == false); ?>',
- ];
- yield [
- '<?php $result = (false != (null === $a)); ?>',
- '<?php $result = (false != is_null($a)); ?>',
- ];
- yield [
- '<?php $result = ((null === $a) != false); ?>',
- '<?php $result = (is_null($a) != false); ?>',
- ];
- yield [
- '<?php $result = (false <> (null === $a)); ?>',
- '<?php $result = (false <> is_null($a)); ?>',
- ];
- yield [
- '<?php $result = ((null === $a) <> false); ?>',
- '<?php $result = (is_null($a) <> false); ?>',
- ];
- yield [
- '<?php if (null === $x) echo "foo"; ?>',
- '<?php if (is_null($x)) echo "foo"; ?>',
- ];
- // check with logical operator
- yield [
- '<?php if (null === $x && $y) echo "foo"; ?>',
- '<?php if (is_null($x) && $y) echo "foo"; ?>',
- ];
- yield [
- '<?php if (null === $x || $y) echo "foo"; ?>',
- '<?php if (is_null($x) || $y) echo "foo"; ?>',
- ];
- yield [
- '<?php if (null === $x xor $y) echo "foo"; ?>',
- '<?php if (is_null($x) xor $y) echo "foo"; ?>',
- ];
- yield [
- '<?php if (null === $x and $y) echo "foo"; ?>',
- '<?php if (is_null($x) and $y) echo "foo"; ?>',
- ];
- yield [
- '<?php if (null === $x or $y) echo "foo"; ?>',
- '<?php if (is_null($x) or $y) echo "foo"; ?>',
- ];
- yield [
- '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
- '<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
- ];
- // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
- yield [
- '<?php null === ($a ? $x : $y);',
- '<?php is_null($a ? $x : $y);',
- ];
- yield [
- '<?php $a === (null === $x);',
- '<?php $a === is_null($x);',
- ];
- yield [
- '<?php $a === (null === ($a ? $x : $y));',
- '<?php $a === is_null($a ? $x : $y);',
- ];
- yield [
- '<?php null !== ($a ?? null);',
- '<?php !is_null($a ?? null);',
- ];
- yield [
- '<?php null === $x;',
- '<?php is_null($x, );',
- ];
- yield [
- '<?php null === $x;',
- '<?php is_null( $x , );',
- ];
- yield [
- '<?php null === a(null === a(null === a(null === b(), ), ), );',
- '<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b(), ), ), ), ), ), ), );',
- ];
- yield [
- '<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
- '<?php if ((is_null($u, ) or $v) and ($w || is_null($x, )) xor (!is_null($y, ) and $z)) echo "foo"; ?>',
- ];
- // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
- yield [
- '<?php null === ($a ? $x : $y );',
- '<?php is_null($a ? $x : $y, );',
- ];
- yield [
- '<?php $a === (null === $x);',
- '<?php $a === is_null($x, );',
- ];
- yield [
- '<?php $a === (null === ($a ? $x : $y ));',
- '<?php $a === is_null($a ? $x : $y, );',
- ];
- yield [
- '<?php $a === (int) (null === $x) + (int) (null !== $y);',
- '<?php $a === (int) is_null($x) + (int) !is_null($y);',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'first-class callable' => [
- '<?php array_filter([], is_null(...));',
- ];
- yield 'first-class callable with leading slash' => [
- '<?php array_filter([], \is_null(...));',
- ];
- }
- }
|