NativeFunctionTypeDeclarationCasingFixerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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\NativeFunctionTypeDeclarationCasingFixer
  18. */
  19. final class NativeFunctionTypeDeclarationCasingFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @dataProvider provideFixCases
  23. */
  24. public function testFix(string $expected, ?string $input = null): void
  25. {
  26. $this->doTest($expected, $input);
  27. }
  28. public static function provideFixCases(): iterable
  29. {
  30. yield [
  31. '<?php
  32. class Foo
  33. {
  34. private function Bar(array $bar) {
  35. return false;
  36. }
  37. }
  38. ',
  39. '<?php
  40. class Foo
  41. {
  42. private function Bar(ARRAY $bar) {
  43. return false;
  44. }
  45. }
  46. ',
  47. ];
  48. yield [
  49. '<?php
  50. interface Foo
  51. {
  52. public function Bar(array $bar);
  53. }
  54. ',
  55. '<?php
  56. interface Foo
  57. {
  58. public function Bar(ArrAY $bar);
  59. }
  60. ',
  61. ];
  62. yield [
  63. '<?php
  64. function Foo(/**/array/**/$bar) {
  65. return false;
  66. }
  67. ',
  68. '<?php
  69. function Foo(/**/ARRAY/**/$bar) {
  70. return false;
  71. }
  72. ',
  73. ];
  74. yield [
  75. '<?php
  76. class Bar { function Foo(array $a, callable $b, self $c) {} }
  77. ',
  78. '<?php
  79. class Bar { function Foo(ARRAY $a, CALLABLE $b, Self $c) {} }
  80. ',
  81. ];
  82. yield [
  83. '<?php
  84. function Foo(INTEGER $a) {}
  85. ',
  86. ];
  87. yield [
  88. '<?php function Foo(
  89. String\A $x,
  90. B\String\C $y
  91. ) {}',
  92. ];
  93. yield [
  94. '<?php final class Foo1 { final public function Foo(bool $A, float $B, int $C, string $D): int {} }',
  95. '<?php final class Foo1 { final public function Foo(BOOL $A, FLOAT $B, INT $C, STRING $D): INT {} }',
  96. ];
  97. yield [
  98. '<?php function Foo(bool $A, float $B, int $C, string $D): int {}',
  99. '<?php function Foo(BOOL $A, FLOAT $B, INT $C, STRING $D): INT {}',
  100. ];
  101. yield [
  102. '<?php function Foo(): Foo\A { return new Foo(); }',
  103. ];
  104. yield [
  105. '<?php trait XYZ { function Foo(iterable $A): void {} }',
  106. '<?php trait XYZ { function Foo(ITERABLE $A): VOID {} }',
  107. ];
  108. yield [
  109. '<?php function Foo(iterable $A): void {}',
  110. '<?php function Foo(ITERABLE $A): VOID {}',
  111. ];
  112. yield [
  113. '<?php function Foo(?int $A): void {}',
  114. '<?php function Foo(?INT $A): VOID {}',
  115. ];
  116. yield [
  117. '<?php function Foo(string $A): ?/* */int {}',
  118. '<?php function Foo(STRING $A): ?/* */INT {}',
  119. ];
  120. yield [
  121. '<?php function Foo(object $A): void {}',
  122. '<?php function Foo(OBJECT $A): VOID {}',
  123. ];
  124. yield [
  125. '<?php return function (callable $c) {};',
  126. '<?php return function (CALLABLE $c) {};',
  127. ];
  128. yield [
  129. '<?php return fn (callable $c): int => 1;',
  130. '<?php return fn (CALLABLE $c): INT => 1;',
  131. ];
  132. }
  133. /**
  134. * @dataProvider provideFix80Cases
  135. *
  136. * @requires PHP 8.0
  137. */
  138. public function testFix80(string $expected, string $input): void
  139. {
  140. $this->doTest($expected, $input);
  141. }
  142. public static function provideFix80Cases(): iterable
  143. {
  144. yield [
  145. '<?php class T { public function Foo(object $A): static {}}',
  146. '<?php class T { public function Foo(object $A): StatiC {}}',
  147. ];
  148. yield [
  149. '<?php class T { public function Foo(object $A): ?static {}}',
  150. '<?php class T { public function Foo(object $A): ?StatiC {}}',
  151. ];
  152. yield [
  153. '<?php class T { public function Foo(mixed $A): mixed {}}',
  154. '<?php class T { public function Foo(Mixed $A): MIXED {}}',
  155. ];
  156. yield 'mixed in arrow function' => [
  157. '<?php return fn (mixed $c): mixed => 1;',
  158. '<?php return fn (MiXeD $c): MIXED => 1;',
  159. ];
  160. yield [
  161. '<?php function foo(int|bool $x) {}',
  162. '<?php function foo(INT|BOOL $x) {}',
  163. ];
  164. yield [
  165. '<?php function foo(int | bool $x) {}',
  166. '<?php function foo(INT | BOOL $x) {}',
  167. ];
  168. yield [
  169. '<?php function foo(): int|bool {}',
  170. '<?php function foo(): INT|BOOL {}',
  171. ];
  172. yield 'return type string|false' => [
  173. '<?php function foo(): string|false {}',
  174. '<?php function foo(): string|FALSE {}',
  175. ];
  176. yield 'return type string|null' => [
  177. '<?php function foo(): string|null {}',
  178. '<?php function foo(): string|NULL {}',
  179. ];
  180. yield 'union types in arrow function' => [
  181. '<?php return fn (string|null $c): int|null => 1;',
  182. '<?php return fn (string|NULL $c): INT|NULL => 1;',
  183. ];
  184. }
  185. /**
  186. * @dataProvider provideFix81Cases
  187. *
  188. * @requires PHP 8.1
  189. */
  190. public function testFix81(string $expected, string $input): void
  191. {
  192. $this->doTest($expected, $input);
  193. }
  194. public static function provideFix81Cases(): iterable
  195. {
  196. yield 'return type `never`' => [
  197. '<?php class T { public function Foo(object $A): never {die;}}',
  198. '<?php class T { public function Foo(object $A): NEVER {die;}}',
  199. ];
  200. }
  201. /**
  202. * @dataProvider provideFix82Cases
  203. *
  204. * @requires PHP 8.2
  205. */
  206. public function testFix82(string $expected, string $input): void
  207. {
  208. $this->doTest($expected, $input);
  209. }
  210. public static function provideFix82Cases(): iterable
  211. {
  212. yield 'disjunctive normal form types in arrow function' => [
  213. '<?php return fn ((A&B)|C|null $c): (X&Y)|Z|null => 1;',
  214. '<?php return fn ((A&B)|C|Null $c): (X&Y)|Z|NULL => 1;',
  215. ];
  216. foreach (['true', 'false', 'null'] as $type) {
  217. yield sprintf('standalone type `%s` in class method', $type) => [
  218. sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', $type),
  219. sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', strtoupper($type)),
  220. ];
  221. yield sprintf('standalone type `%s` in function', $type) => [
  222. sprintf('<?php function Foo(%s $A): %1$s {return $A;}', $type),
  223. sprintf('<?php function Foo(%s $A): %1$s {return $A;}', strtoupper($type)),
  224. ];
  225. yield sprintf('standalone type `%s` in closure', $type) => [
  226. sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', $type),
  227. sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', strtoupper($type)),
  228. ];
  229. yield sprintf('standalone type `%s` in arrow function', $type) => [
  230. sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', $type),
  231. sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', strtoupper($type)),
  232. ];
  233. }
  234. }
  235. }