NativeFunctionTypeDeclarationCasingFixerTest.php 7.5 KB

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