123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?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\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<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- $allMethodNames = \Closure::bind(static fn (): array => MagicMethodCasingFixer::$magicNames, null, MagicMethodCasingFixer::class)();
- // '__callStatic'
- yield 'method declaration for "__callstatic".' => [
- '<?php class Foo {public static function __callStatic($a, $b){}}',
- '<?php class Foo {public static function __CALLStatic($a, $b){}}',
- ];
- yield 'static call to "__callstatic".' => [
- '<?php Foo::__callStatic() ?>',
- '<?php Foo::__callstatic() ?>',
- ];
- unset($allMethodNames['__callstatic']);
- // static version of '__set_state'
- yield 'method declaration for "__set_state".' => [
- '<?php class Foo {public static function __set_state($a){}}',
- '<?php class Foo {public static function __set_STATE($a){}}',
- ];
- yield 'static call to "__set_state".' => [
- '<?php Foo::__set_state() ?>',
- '<?php Foo::__set_STATE() ?>',
- ];
- // '__clone'
- yield 'method declaration for "__clone".' => [
- '<?php class Foo {public function __clone(){}}',
- '<?php class Foo {public function __CLONE(){}}',
- ];
- unset($allMethodNames['__clone'], $allMethodNames['__set_state']);
- // two arguments
- $methodNames = ['__call', '__set'];
- foreach ($methodNames as $name) {
- unset($allMethodNames[$name]);
- yield \sprintf('method declaration for "%s".', $name) => [
- \sprintf('<?php class Foo {public function %s($a, $b){}}', $name),
- \sprintf('<?php class Foo {public function %s($a, $b){}}', strtoupper($name)),
- ];
- }
- foreach ($methodNames as $name) {
- yield \sprintf('method call "%s".', $name) => [
- \sprintf('<?php $a->%s($a, $b);', $name),
- \sprintf('<?php $a->%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('<?php class Foo {public function %s($a){}}', $name),
- \sprintf('<?php class Foo {public function %s($a){}}', strtoupper($name)),
- ];
- }
- foreach ($methodNames as $name) {
- yield \sprintf('method call "%s".', $name) => [
- \sprintf('<?php $a->%s($a);', $name),
- \sprintf('<?php $a->%s($a);', strtoupper($name)),
- ];
- }
- // no argument
- foreach ($allMethodNames as $name) {
- yield \sprintf('method declaration for "%s".', $name) => [
- \sprintf('<?php class Foo {public function %s(){}}', $name),
- \sprintf('<?php class Foo {public function %s(){}}', strtoupper($name)),
- ];
- }
- foreach ($allMethodNames as $name) {
- yield \sprintf('method call "%s".', $name) => [
- \sprintf('<?php $a->%s();', $name),
- \sprintf('<?php $a->%s();', strtoupper($name)),
- ];
- }
- yield 'method declaration in interface' => [
- '<?php interface Foo {public function __toString();}',
- '<?php interface Foo {public function __tostring();}',
- ];
- yield 'method declaration in trait' => [
- '<?php trait Foo {public function __toString(){}}',
- '<?php trait Foo {public function __tostring(){}}',
- ];
- yield '(un)serialize' => [
- '<?php
- class Foo extends Bar
- {
- public function __serialize() {
- $this->__serialize();
- }
- public function __unserialize($payload) {
- $this->__unserialize($this->$a);
- }
- }
- ',
- '<?php
- class Foo extends Bar
- {
- public function __SERIALIZE() {
- $this->__SERIALIZE();
- }
- public function __unSERIALIZE($payload) {
- $this->__unSERIALIZE($this->$a);
- }
- }
- ',
- ];
- yield 'PHP 7 syntax' => [
- '<?php
- function __TOSTRING(){} // do not fix
- trait FooTrait
- {
- public function __invoke($a){} // fix
- }
- function __GET($a){} // do not fix
- interface Foo
- {
- public function __sleep(); // fix
- }
- final class Foo
- {
- private function __construct($a, $b, $c, $d = null, $e = 1) // fix
- {
- }
- public function __isset($a) // fix
- {
- return $b->__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
- ',
- '<?php
- function __TOSTRING(){} // do not fix
- trait FooTrait
- {
- public function __INVOKE($a){} // fix
- }
- function __GET($a){} // do not fix
- interface Foo
- {
- public function __SlEeP(); // fix
- }
- final class Foo
- {
- private function __consTRUCT($a, $b, $c, $d = null, $e = 1) // fix
- {
- }
- public function __ISSET($a) // fix
- {
- return $b->__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 [
- '<?php $foo->__invoke(1, );',
- '<?php $foo->__INVOKE(1, );',
- ];
- yield [
- '<?php
- __Tostring();',
- ];
- yield [
- '<?php
- function __Tostring() {}',
- ];
- yield [
- '<?php
- #->__sleep()
- /** ->__sleep() */
- echo $a->__sleep;
- ',
- ];
- yield [
- '<?php
- class B
- {
- public function _not_magic()
- {
- }
- }
- ',
- ];
- yield [
- '<?php
- function __alsoNotMagic()
- {
- }
- ',
- ];
- yield [
- '<?php
- function __()
- {
- }
- ',
- ];
- yield [
- '<?php
- function a()
- {
- }
- ',
- ];
- yield [
- '<?php
- $a->__not_magic();
- ',
- ];
- yield [
- '<?php
- $a->a();
- ',
- ];
- yield [
- '<?php A\B\__callstatic(); echo $a->b;',
- ];
- }
- /**
- * @requires PHP 8.0
- */
- public function testFix80(): void
- {
- $this->doTest(
- '<?php $foo?->__invoke(1, );',
- '<?php $foo?->__INVOKE(1, );'
- );
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string, string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'static call to "__set_state".' => [
- '<?php $f = Foo::__set_state(...);',
- '<?php $f = Foo::__set_STATE(...);',
- ];
- yield 'isset' => [
- '<?php $a->__isset(...);',
- '<?php $a->__ISSET(...);',
- ];
- yield 'enum' => [
- '<?php
- enum Foo
- {
- public static function __callStatic(string $method, array $parameters){ echo $method;}
- }
- Foo::test();',
- '<?php
- enum Foo
- {
- public static function __CALLStatic(string $method, array $parameters){ echo $method;}
- }
- Foo::test();',
- ];
- }
- }
|