MagicMethodCasingFixerTest.php 10 KB

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