MagicMethodCasingFixerTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Casing;
  13. use PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer
  19. *
  20. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer>
  21. */
  22. final class MagicMethodCasingFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null): void
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. /**
  32. * @return iterable<int|string, array{0: string, 1?: string}>
  33. */
  34. public static function provideFixCases(): iterable
  35. {
  36. $allMethodNames = \Closure::bind(static fn (): array => MagicMethodCasingFixer::$magicNames, null, MagicMethodCasingFixer::class)();
  37. // '__callStatic'
  38. yield 'method declaration for "__callstatic".' => [
  39. '<?php class Foo {public static function __callStatic($a, $b){}}',
  40. '<?php class Foo {public static function __CALLStatic($a, $b){}}',
  41. ];
  42. yield 'static call to "__callstatic".' => [
  43. '<?php Foo::__callStatic() ?>',
  44. '<?php Foo::__callstatic() ?>',
  45. ];
  46. unset($allMethodNames['__callstatic']);
  47. // static version of '__set_state'
  48. yield 'method declaration for "__set_state".' => [
  49. '<?php class Foo {public static function __set_state($a){}}',
  50. '<?php class Foo {public static function __set_STATE($a){}}',
  51. ];
  52. yield 'static call to "__set_state".' => [
  53. '<?php Foo::__set_state() ?>',
  54. '<?php Foo::__set_STATE() ?>',
  55. ];
  56. // '__clone'
  57. yield 'method declaration for "__clone".' => [
  58. '<?php class Foo {public function __clone(){}}',
  59. '<?php class Foo {public function __CLONE(){}}',
  60. ];
  61. unset($allMethodNames['__clone'], $allMethodNames['__set_state']);
  62. // two arguments
  63. $methodNames = ['__call', '__set'];
  64. foreach ($methodNames as $name) {
  65. unset($allMethodNames[$name]);
  66. yield \sprintf('method declaration for "%s".', $name) => [
  67. \sprintf('<?php class Foo {public function %s($a, $b){}}', $name),
  68. \sprintf('<?php class Foo {public function %s($a, $b){}}', strtoupper($name)),
  69. ];
  70. }
  71. foreach ($methodNames as $name) {
  72. yield \sprintf('method call "%s".', $name) => [
  73. \sprintf('<?php $a->%s($a, $b);', $name),
  74. \sprintf('<?php $a->%s($a, $b);', strtoupper($name)),
  75. ];
  76. }
  77. // single argument
  78. $methodNames = ['__get', '__isset', '__unset', '__unserialize'];
  79. foreach ($methodNames as $name) {
  80. unset($allMethodNames[$name]);
  81. yield \sprintf('method declaration for "%s".', $name) => [
  82. \sprintf('<?php class Foo {public function %s($a){}}', $name),
  83. \sprintf('<?php class Foo {public function %s($a){}}', strtoupper($name)),
  84. ];
  85. }
  86. foreach ($methodNames as $name) {
  87. yield \sprintf('method call "%s".', $name) => [
  88. \sprintf('<?php $a->%s($a);', $name),
  89. \sprintf('<?php $a->%s($a);', strtoupper($name)),
  90. ];
  91. }
  92. // no argument
  93. foreach ($allMethodNames as $name) {
  94. yield \sprintf('method declaration for "%s".', $name) => [
  95. \sprintf('<?php class Foo {public function %s(){}}', $name),
  96. \sprintf('<?php class Foo {public function %s(){}}', strtoupper($name)),
  97. ];
  98. }
  99. foreach ($allMethodNames as $name) {
  100. yield \sprintf('method call "%s".', $name) => [
  101. \sprintf('<?php $a->%s();', $name),
  102. \sprintf('<?php $a->%s();', strtoupper($name)),
  103. ];
  104. }
  105. yield 'method declaration in interface' => [
  106. '<?php interface Foo {public function __toString();}',
  107. '<?php interface Foo {public function __tostring();}',
  108. ];
  109. yield 'method declaration in trait' => [
  110. '<?php trait Foo {public function __toString(){}}',
  111. '<?php trait Foo {public function __tostring(){}}',
  112. ];
  113. yield '(un)serialize' => [
  114. '<?php
  115. class Foo extends Bar
  116. {
  117. public function __serialize() {
  118. $this->__serialize();
  119. }
  120. public function __unserialize($payload) {
  121. $this->__unserialize($this->$a);
  122. }
  123. }
  124. ',
  125. '<?php
  126. class Foo extends Bar
  127. {
  128. public function __SERIALIZE() {
  129. $this->__SERIALIZE();
  130. }
  131. public function __unSERIALIZE($payload) {
  132. $this->__unSERIALIZE($this->$a);
  133. }
  134. }
  135. ',
  136. ];
  137. yield 'PHP 7 syntax' => [
  138. '<?php
  139. function __TOSTRING(){} // do not fix
  140. trait FooTrait
  141. {
  142. public function __invoke($a){} // fix
  143. }
  144. function __GET($a){} // do not fix
  145. interface Foo
  146. {
  147. public function __sleep(); // fix
  148. }
  149. final class Foo
  150. {
  151. private function __construct($a, $b, $c, $d = null, $e = 1) // fix
  152. {
  153. }
  154. public function __isset($a) // fix
  155. {
  156. return $b->__isset($b); // fix
  157. }
  158. private function bar()
  159. {
  160. new class {
  161. public function __unset($a) // fix
  162. {
  163. $b = null === $a
  164. ? $b->__unset($a) // fix
  165. : $a->__unset($a) // fix
  166. ;
  167. return $b;
  168. }
  169. };
  170. }
  171. }
  172. function __ISSET($bar){} // do not fix
  173. $a->__unset($foo); // fix
  174. ',
  175. '<?php
  176. function __TOSTRING(){} // do not fix
  177. trait FooTrait
  178. {
  179. public function __INVOKE($a){} // fix
  180. }
  181. function __GET($a){} // do not fix
  182. interface Foo
  183. {
  184. public function __SlEeP(); // fix
  185. }
  186. final class Foo
  187. {
  188. private function __consTRUCT($a, $b, $c, $d = null, $e = 1) // fix
  189. {
  190. }
  191. public function __ISSET($a) // fix
  192. {
  193. return $b->__IsseT($b); // fix
  194. }
  195. private function bar()
  196. {
  197. new class {
  198. public function __UnSet($a) // fix
  199. {
  200. $b = null === $a
  201. ? $b->__UnSet($a) // fix
  202. : $a->__UnSet($a) // fix
  203. ;
  204. return $b;
  205. }
  206. };
  207. }
  208. }
  209. function __ISSET($bar){} // do not fix
  210. $a->__UnSet($foo); // fix
  211. ',
  212. ];
  213. yield [
  214. '<?php $foo->__invoke(1, );',
  215. '<?php $foo->__INVOKE(1, );',
  216. ];
  217. yield [
  218. '<?php
  219. __Tostring();',
  220. ];
  221. yield [
  222. '<?php
  223. function __Tostring() {}',
  224. ];
  225. yield [
  226. '<?php
  227. #->__sleep()
  228. /** ->__sleep() */
  229. echo $a->__sleep;
  230. ',
  231. ];
  232. yield [
  233. '<?php
  234. class B
  235. {
  236. public function _not_magic()
  237. {
  238. }
  239. }
  240. ',
  241. ];
  242. yield [
  243. '<?php
  244. function __alsoNotMagic()
  245. {
  246. }
  247. ',
  248. ];
  249. yield [
  250. '<?php
  251. function __()
  252. {
  253. }
  254. ',
  255. ];
  256. yield [
  257. '<?php
  258. function a()
  259. {
  260. }
  261. ',
  262. ];
  263. yield [
  264. '<?php
  265. $a->__not_magic();
  266. ',
  267. ];
  268. yield [
  269. '<?php
  270. $a->a();
  271. ',
  272. ];
  273. yield [
  274. '<?php A\B\__callstatic(); echo $a->b;',
  275. ];
  276. }
  277. /**
  278. * @requires PHP 8.0
  279. */
  280. public function testFix80(): void
  281. {
  282. $this->doTest(
  283. '<?php $foo?->__invoke(1, );',
  284. '<?php $foo?->__INVOKE(1, );'
  285. );
  286. }
  287. /**
  288. * @dataProvider provideFix81Cases
  289. *
  290. * @requires PHP 8.1
  291. */
  292. public function testFix81(string $expected, ?string $input = null): void
  293. {
  294. $this->doTest($expected, $input);
  295. }
  296. /**
  297. * @return iterable<string, array{string, string}>
  298. */
  299. public static function provideFix81Cases(): iterable
  300. {
  301. yield 'static call to "__set_state".' => [
  302. '<?php $f = Foo::__set_state(...);',
  303. '<?php $f = Foo::__set_STATE(...);',
  304. ];
  305. yield 'isset' => [
  306. '<?php $a->__isset(...);',
  307. '<?php $a->__ISSET(...);',
  308. ];
  309. yield 'enum' => [
  310. '<?php
  311. enum Foo
  312. {
  313. public static function __callStatic(string $method, array $parameters){ echo $method;}
  314. }
  315. Foo::test();',
  316. '<?php
  317. enum Foo
  318. {
  319. public static function __CALLStatic(string $method, array $parameters){ echo $method;}
  320. }
  321. Foo::test();',
  322. ];
  323. }
  324. }