* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\StringNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\StringNotation\StringLengthToEmptyFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\StringLengthToEmptyFixer> */ final class StringLengthToEmptyFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixCases(): iterable { yield [ ' [ '', '', ]; yield 'nested' => [ '= strlen($b);', ]; yield [ ' strlen($b);', ]; yield [ '= strlen($b);', ]; yield [ ' strlen($b);', ]; yield [ '= 0;', ]; yield [ ' 0;', ]; yield [ '= 1;', ]; yield [ ' 1;', ]; yield [ 'a !== \'\';', 'a) >= 1;', ]; yield [ '$a[$a++](1) /* 1 */ === \'\';', '$a[$a++](1) /* 1 */ ) < 1;', ]; yield [ 'foo(++$i, static function () use ($z){ return $z + 1;});', 'foo(++$i, static function () use ($z){ return $z + 1;}));', ]; yield [ ' 0) { echo 1; }', ]; yield 'do not fix' => [ '> $c; $a10 = 0 === strlen($b) << $c; $a01n = strlen($b) === 0 ** $c; $a03n = strlen($b) === 0 % $c; $a04n = strlen($b) === 0 / $c; $a05n = strlen($b) === 0 * $c; $a06n = strlen($b) === 0 + $c; $a07n = strlen($b) === 0 - $c; $a08n = strlen($b) === 0 . $c; $a09n = strlen($b) === 0 >> $c; $a10n = strlen($b) === 0 << $c; $b = "a"; $c = 0 === strlen($b) - 1; var_dump($c); $c = "" === $b - 1; var_dump($c); //----------------------------------- // type juggle $d = false; $e = 0 === strlen($d) ? -1 : 0; var_dump($e); $e = "" === $d ? -1 : 0; var_dump($e); //----------------------------------- // wrong argument count $f = strlen(1,2); $g = \strlen(1,2,3); //----------------------------------- // others $h = 0 === (string) strlen($b); $i = 0 === @strlen($b); $j = 0 === !strlen($b); $jj = 2 === strlen($b); $jk = __DIR__ === strlen($b); $jl = \'X\' !== strlen($b); $jj = strlen($b) === 2; $jk = strlen($b) === __DIR__; $jl = strlen($b) !== \'X\'; //----------------------------------- // not global calls $k = 0 === $a->strlen($b); $l = 0 === Foo::strlen($b); //----------------------------------- // comments // $a = 0 === strlen($b); # $a = 0 === strlen($b); /* $a = 0 === strlen($b); */ /** $a = 0 === strlen($b); */ ', ]; // cases where `(` and `)` must be kept yield [ ' 0;', ]; yield [ '