MagicMethodCasingFixerTest.php 10 KB

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