* 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\LanguageConstruct; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Gert de Pagter * * @internal * * @covers \PhpCsFixer\Fixer\LanguageConstruct\NoUnsetOnPropertyFixer */ final class NoUnsetOnPropertyFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } public function provideFixCases(): \Generator { yield from [ 'It replaces an unset on a property with = null' => [ 'bar = null;', 'bar);', ], 'It replaces an unset on a property with = null II' => [ 'bar = null ;', 'bar );', ], 'It replaces an unset on a static property with = null' => [ ' [ 'a; unset($foo);', ], 'It replaces multiple unsets on variables with = null' => [ 'bar = null; $bar->foo = null; $bar->baz = null; $a->ba = null;', 'bar, $bar->foo, $bar->baz, $a->ba);', ], 'It replaces multiple unsets, but not those that arent properties' => [ 'bar = null; $bar->foo = null; unset($bar);', 'bar, $bar->foo, $bar);', ], 'It replaces multiple unsets, but not those that arent properties in multiple places' => [ 'foo = null; unset($bar);', 'foo, $bar);', ], 'It replaces $this -> and self:: replacements' => [ 'bar = null; self::$foo = null; unset($bar);', 'bar, self::$foo, $bar);', ], 'It does not replace unsets on arrays' => [ 'foo[0]);', ], 'It works in a more complex unset' => [ 'foo[0]); self::$foo = null; \Test\Baz::$fooBar = null; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b);', 'foo[0], self::$foo, \Test\Baz::$fooBar, $bar->foo[0], $this->foo, $a, $b);', ], 'It works with consecutive unsets' => [ 'bar = null; unset($foo); unset($bar); unset($baz); $this->ab = null;', 'bar, $foo, $bar, $baz, $this->ab);', ], 'It works when around messy whitespace' => [ 'b = null; $this->a = null; unset($b); ', 'b); unset($this->a, $b); ', ], 'It works with weirdly placed comments' => [ 'foo[0]); self::$foo = null/*baz*/; /*ello*/\Test\Baz::$fooBar = null/*comment*/; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b); unset/*foo*/(/*bar*/$bar);', 'foo[0], self::$foo/*baz*/, /*ello*/\Test\Baz::$fooBar/*comment*/, $bar->foo[0], $this->foo, $a, $b); unset/*foo*/(/*bar*/$bar);', ], 'It does not mess with consecutive unsets' => [ 'a = null;', 'a);', ], 'It does not replace function call with class constant inside' => [ ' [ 'property[array_search(\Types::TYPE_RANDOM, $this->property)]);', ], ]; if (\PHP_VERSION_ID < 80000) { yield 'It does not replace unsets on arrays with special notation' => [ 'foo{0});', ]; } yield 'It does not break complex expressions' => [ 'a); ', ]; } /** * @dataProvider provideFixPre80Cases * @requires PHP <8.0 */ public function testFixPre80(string $expected, string $input = null): void { $this->doTest($expected, $input); } public function provideFixPre80Cases(): \Generator { yield 'It does not break curly access expressions' => [ 'doTest($expected, $input); } public function provideFix73Cases(): \Generator { yield from [ 'It replaces an unset on a property with = null' => [ 'bar = null;', 'bar,);', ], 'It replaces multiple unsets, but not those that arent properties' => [ 'bar = null; $bar->foo = null; unset($bar,);', 'bar, $bar->foo, $bar,);', ], 'It replaces an unset on a static property with = null' => [ ' [ 'a; unset($foo,);', ], 'It replaces multiple unsets on variables with = null' => [ 'bar = null; $bar->foo = null; $bar->baz = null; $a->ba = null;', 'bar, $bar->foo, $bar->baz, $a->ba,);', ], 'It replaces multiple unsets, but not those that arent properties in multiple places' => [ 'foo = null; unset($bar,);', 'foo, $bar,);', ], 'It replaces $this -> and self:: replacements' => [ 'bar = null; self::$foo = null; unset($bar,);', 'bar, self::$foo, $bar,);', ], 'It does not replace unsets on arrays' => [ 'foo[0],);', ], 'It works in a more complex unset' => [ 'foo[0]); self::$foo = null; \Test\Baz::$fooBar = null; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b,);', 'foo[0], self::$foo, \Test\Baz::$fooBar, $bar->foo[0], $this->foo, $a, $b,);', ], 'It works with consecutive unsets' => [ 'bar = null; unset($foo); unset($bar); unset($baz); $this->ab = null;', 'bar, $foo, $bar, $baz, $this->ab,);', ], 'It works when around messy whitespace' => [ 'b = null; $this->a = null; unset($b,); ', 'b,); unset($this->a, $b,); ', ], 'It works with weirdly placed comments' => [ 'foo[0]); self::$foo = null/*baz*/; /*ello*/\Test\Baz::$fooBar = null/*comment*/; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b,); unset/*foo*/(/*bar*/$bar,);', 'foo[0], self::$foo/*baz*/, /*ello*/\Test\Baz::$fooBar/*comment*/, $bar->foo[0], $this->foo, $a, $b,); unset/*foo*/(/*bar*/$bar,);', ], 'It does not mess with consecutive unsets' => [ 'a = null;', 'a,);', ], 'It does not replace function call with class constant inside' => [ ' [ 'property[array_search(\Types::TYPE_RANDOM, $this->property)],);', ], [ 'bar = null ;', 'bar, );', ], [ 'bar = null ;', 'bar ,);', ], [ 'bar = null ;', 'bar , );', ], ]; if (\PHP_VERSION_ID < 80000) { yield 'It does not replace unsets on arrays with special notation' => [ 'foo{0},);', ]; } } }