* 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\Casing; use PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer> */ final class MagicMethodCasingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixCases(): iterable { $fixerReflection = new \ReflectionClass(MagicMethodCasingFixer::class); $property = $fixerReflection->getProperty('magicNames'); $property->setAccessible(true); $allMethodNames = $property->getValue(); // '__callStatic' yield 'method declaration for "__callstatic".' => [ ' [ '', '', ]; unset($allMethodNames['__callstatic']); // static version of '__set_state' yield 'method declaration for "__set_state".' => [ ' [ '', '', ]; // '__clone' yield 'method declaration for "__clone".' => [ ' [ \sprintf(' [ \sprintf('%s($a, $b);', $name), \sprintf('%s($a, $b);', strtoupper($name)), ]; } // single argument $methodNames = ['__get', '__isset', '__unset', '__unserialize']; foreach ($methodNames as $name) { unset($allMethodNames[$name]); yield \sprintf('method declaration for "%s".', $name) => [ \sprintf(' [ \sprintf('%s($a);', $name), \sprintf('%s($a);', strtoupper($name)), ]; } // no argument foreach ($allMethodNames as $name) { yield \sprintf('method declaration for "%s".', $name) => [ \sprintf(' [ \sprintf('%s();', $name), \sprintf('%s();', strtoupper($name)), ]; } yield 'method declaration in interface' => [ ' [ ' [ '__serialize(); } public function __unserialize($payload) { $this->__unserialize($this->$a); } } ', '__SERIALIZE(); } public function __unSERIALIZE($payload) { $this->__unSERIALIZE($this->$a); } } ', ]; yield 'PHP 7 syntax' => [ '__isset($b); // fix } private function bar() { new class { public function __unset($a) // fix { $b = null === $a ? $b->__unset($a) // fix : $a->__unset($a) // fix ; return $b; } }; } } function __ISSET($bar){} // do not fix $a->__unset($foo); // fix ', '__IsseT($b); // fix } private function bar() { new class { public function __UnSet($a) // fix { $b = null === $a ? $b->__UnSet($a) // fix : $a->__UnSet($a) // fix ; return $b; } }; } } function __ISSET($bar){} // do not fix $a->__UnSet($foo); // fix ', ]; yield [ '__invoke(1, );', '__INVOKE(1, );', ]; yield [ '__sleep() /** ->__sleep() */ echo $a->__sleep; ', ]; yield [ '__not_magic(); ', ]; yield [ 'a(); ', ]; yield [ 'b;', ]; } /** * @requires PHP 8.0 */ public function testFix80(): void { $this->doTest( '__invoke(1, );', '__INVOKE(1, );' ); } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix81Cases(): iterable { yield 'static call to "__set_state".' => [ ' [ '__isset(...);', '__ISSET(...);', ]; yield 'enum' => [ '