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