ClassReferenceNameCasingFixerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer>
  20. */
  21. final class ClassReferenceNameCasingFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php
  37. $a = new Exception;
  38. $b = new \Exception;
  39. $c = new Exception();
  40. $d = new \Exception();
  41. $e = "a".Exception::class;
  42. $f = "a".\Exception::class;
  43. $g .= "exception";
  44. echo \Exception::class;
  45. print(Exception::class);
  46. // $a = new exception();
  47. /** $a = new exception(); */
  48. ',
  49. '<?php
  50. $a = new exception;
  51. $b = new \exception;
  52. $c = new exception();
  53. $d = new \exception();
  54. $e = "a".exception::class;
  55. $f = "a".\exception::class;
  56. $g .= "exception";
  57. echo \exception::class;
  58. print(exception::class);
  59. // $a = new exception();
  60. /** $a = new exception(); */
  61. ',
  62. ];
  63. yield [
  64. '<?php namespace Foo {
  65. $a = new exception;
  66. $b = new \Exception;
  67. }',
  68. '<?php namespace Foo {
  69. $a = new exception;
  70. $b = new \exception;
  71. }',
  72. ];
  73. yield [
  74. '<?php namespace Foo;
  75. $a = new exception;
  76. $b = new \Exception;
  77. ',
  78. '<?php namespace Foo;
  79. $a = new exception;
  80. $b = new \EXCEPTION;
  81. ',
  82. ];
  83. yield [
  84. '<?php
  85. $a = exception();
  86. $b = new A\exception;
  87. $c = new A\B\C\exception;
  88. $a1 = \exception();
  89. $b1 = new \A\exception;
  90. $c1 = new \A\B\C\exception;
  91. ',
  92. ];
  93. yield [
  94. '<?php class Foo extends Exception {};',
  95. '<?php class Foo extends exception {};',
  96. ];
  97. yield [
  98. '<?php class exception {}; new foO();',
  99. ];
  100. yield [
  101. '<?php interface exception {};',
  102. ];
  103. yield [
  104. '<?php trait exception {};',
  105. ];
  106. yield [
  107. '<?php function exception() {};',
  108. ];
  109. yield [
  110. '<?php const exception = "abc";',
  111. ];
  112. yield [
  113. '<?php $a = Foo::exception;',
  114. ];
  115. yield [
  116. '<?php $a = $foo->exception;',
  117. ];
  118. yield [
  119. '<?php use Foo as exception;',
  120. ];
  121. yield [
  122. '<?php class Foo { use exception; }',
  123. ];
  124. yield [
  125. '<?php $foo = ["const" => exception];',
  126. ];
  127. yield [
  128. '<?php
  129. namespace {
  130. $a = new Exception;
  131. $b = new \Exception;
  132. }
  133. namespace Bar {
  134. $a = new exception;
  135. $b = new \Exception;
  136. }
  137. namespace Foo {
  138. $a = new exception;
  139. $b = new \Exception;
  140. $c = new foO();
  141. }',
  142. '<?php
  143. namespace {
  144. $a = new exception;
  145. $b = new \exception;
  146. }
  147. namespace Bar {
  148. $a = new exception;
  149. $b = new \exception;
  150. }
  151. namespace Foo {
  152. $a = new exception;
  153. $b = new \exception;
  154. $c = new foO();
  155. }',
  156. ];
  157. yield [
  158. '<?php use Exception as baR;',
  159. '<?php use exception as baR;',
  160. ];
  161. yield [
  162. '<?php try { foo(); } catch(\LogicException $e) {}',
  163. '<?php try { foo(); } catch(\logicexception $e) {}',
  164. ];
  165. yield [
  166. '<?php try { foo(); } catch(LogicException $e) {}',
  167. '<?php try { foo(); } catch(logicexception $e) {}',
  168. ];
  169. yield [
  170. '<?php
  171. Closure::bind(fn () => null, null, new class {});
  172. \Closure::bind(fn () => null, null, new class {});
  173. Foo\Bar::bind(fn () => null, null, new class {});
  174. \A\B\Bar::bind(fn () => null, null, new class {});
  175. ',
  176. '<?php
  177. CLOSURE::bind(fn () => null, null, new class {});
  178. \CLOSURE::bind(fn () => null, null, new class {});
  179. Foo\Bar::bind(fn () => null, null, new class {});
  180. \A\B\Bar::bind(fn () => null, null, new class {});
  181. ',
  182. ];
  183. yield [
  184. "<?php
  185. declare(strict_types=1);
  186. use Sonata\\Exporter\\Writer\\EXCEPTION;
  187. \$services->set('sonata.exporter.writer.xml', EXCEPTION::class);
  188. ",
  189. ];
  190. yield [
  191. '<?php
  192. const error = 1;
  193. foo(error);
  194. $b2 = $a[error];
  195. $b21 = [1,error];
  196. $b22 = [error,2];
  197. $b23 = [1,error,2];
  198. $b24 = [1,error,2,];
  199. $b3 = [error];
  200. $b4 = $a->{error};
  201. ',
  202. ];
  203. yield [
  204. '<?php
  205. foo(\error);
  206. $b2 = $a[\error];
  207. $b21 = [1,\error];
  208. $b22 = [\error,2];
  209. $b23 = [1,\error,2];
  210. $b24 = [1,\error,2,];
  211. $b3 = [\error];
  212. $b4 = $a->{\error};
  213. ',
  214. ];
  215. yield ['<?php echo error ?><?php echo error;'];
  216. }
  217. /**
  218. * @dataProvider provideFix80Cases
  219. *
  220. * @requires PHP 8.0
  221. */
  222. public function testFix80(string $expected, ?string $input = null): void
  223. {
  224. $this->doTest($expected, $input);
  225. }
  226. /**
  227. * @return iterable<array{0: string, 1?: string}>
  228. */
  229. public static function provideFix80Cases(): iterable
  230. {
  231. yield [
  232. '<?php if ($var?->exception instanceof Exception) {};',
  233. ];
  234. }
  235. /**
  236. * @dataProvider provideFix81Cases
  237. *
  238. * @requires PHP 8.1
  239. */
  240. public function testFix81(string $expected, ?string $input = null): void
  241. {
  242. $this->doTest($expected, $input);
  243. }
  244. /**
  245. * @return iterable<int|string, array{0: string, 1?: string}>
  246. */
  247. public static function provideFix81Cases(): iterable
  248. {
  249. yield [
  250. '<?php enum exception {}',
  251. ];
  252. yield [
  253. '<?php enum Foo {
  254. case exception;
  255. }',
  256. ];
  257. yield 'multiple type catch with variable' => [
  258. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}',
  259. '<?php try { foo(); } catch(\INVALIDARGUMENTEXCEPTION|\logicexception $e) {}',
  260. ];
  261. yield 'multiple type catch without variable 3' => [
  262. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}',
  263. '<?php try { foo(); } catch(\INVALIDARGUMENTEXCEPTION|\logicexception) {}',
  264. ];
  265. }
  266. }