NativeFunctionTypeDeclarationCasingFixerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(): array
  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. }
  131. /**
  132. * @dataProvider provideFix80Cases
  133. *
  134. * @requires PHP 8.0
  135. */
  136. public function testFix80(string $expected, string $input): void
  137. {
  138. $this->doTest($expected, $input);
  139. }
  140. public static function provideFix80Cases(): iterable
  141. {
  142. yield [
  143. '<?php class T { public function Foo(object $A): static {}}',
  144. '<?php class T { public function Foo(object $A): StatiC {}}',
  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(mixed $A): mixed {}}',
  152. '<?php class T { public function Foo(Mixed $A): MIXED {}}',
  153. ];
  154. yield [
  155. '<?php function foo(int|bool $x) {}',
  156. '<?php function foo(INT|BOOL $x) {}',
  157. ];
  158. yield [
  159. '<?php function foo(int | bool $x) {}',
  160. '<?php function foo(INT | BOOL $x) {}',
  161. ];
  162. yield [
  163. '<?php function foo(): int|bool {}',
  164. '<?php function foo(): INT|BOOL {}',
  165. ];
  166. yield 'return type string|false' => [
  167. '<?php function foo(): string|false {}',
  168. '<?php function foo(): string|FALSE {}',
  169. ];
  170. yield 'return type string|null' => [
  171. '<?php function foo(): string|null {}',
  172. '<?php function foo(): string|NULL {}',
  173. ];
  174. }
  175. /**
  176. * @dataProvider provideFix81Cases
  177. *
  178. * @requires PHP 8.1
  179. */
  180. public function testFix81(string $expected, string $input): void
  181. {
  182. $this->doTest($expected, $input);
  183. }
  184. public static function provideFix81Cases(): iterable
  185. {
  186. yield 'return type `never`' => [
  187. '<?php class T { public function Foo(object $A): never {die;}}',
  188. '<?php class T { public function Foo(object $A): NEVER {die;}}',
  189. ];
  190. }
  191. /**
  192. * @dataProvider provideFix82Cases
  193. *
  194. * @requires PHP 8.2
  195. */
  196. public function testFix82(string $expected, string $input): void
  197. {
  198. $this->doTest($expected, $input);
  199. }
  200. public static function provideFix82Cases(): iterable
  201. {
  202. foreach (['true', 'false', 'null'] as $type) {
  203. yield sprintf('standalone type `%s` in class method', $type) => [
  204. sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', $type),
  205. sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', strtoupper($type)),
  206. ];
  207. yield sprintf('standalone type `%s` in function', $type) => [
  208. sprintf('<?php function Foo(%s $A): %1$s {return $A;}', $type),
  209. sprintf('<?php function Foo(%s $A): %1$s {return $A;}', strtoupper($type)),
  210. ];
  211. yield sprintf('standalone type `%s` in closure', $type) => [
  212. sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', $type),
  213. sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', strtoupper($type)),
  214. ];
  215. }
  216. }
  217. }