AbstractNullableTypeDeclarationFixerTestCase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Whitespace;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Jack Cherng <jfcherng@gmail.com>
  17. *
  18. * @template TFixer of AbstractFixer
  19. *
  20. * @internal
  21. *
  22. * @extends AbstractFixerTestCase<TFixer>
  23. */
  24. abstract class AbstractNullableTypeDeclarationFixerTestCase extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. yield [
  39. '<?php function foo(?int $param): ?int {}',
  40. ];
  41. yield [
  42. '<?php function foo(? /* foo */ int $param): ? /* foo */ int {}',
  43. ];
  44. yield [
  45. '<?php function foo(? /** foo */ int $param): ? /** foo */ int {}',
  46. ];
  47. yield [
  48. '<?php function foo(? // foo
  49. int $param): ? // foo
  50. int {}',
  51. ];
  52. yield [
  53. '<?php function foo(/**? int*/$param): ?int {}',
  54. '<?php function foo(/**? int*/$param): ? int {}',
  55. ];
  56. yield [
  57. '<?php function foo(?callable $param): ?callable {}',
  58. '<?php function foo(? callable $param): ? callable {}',
  59. ];
  60. yield [
  61. '<?php function foo(?array &$param): ?array {}',
  62. '<?php function foo(? array &$param): ? array {}',
  63. ];
  64. yield [
  65. '<?php function foo(?Bar $param): ?Bar {}',
  66. '<?php function foo(? Bar $param): ? Bar {}',
  67. ];
  68. yield [
  69. '<?php function foo(?\Bar $param): ?\Bar {}',
  70. '<?php function foo(? \Bar $param): ? \Bar {}',
  71. ];
  72. yield [
  73. '<?php function foo(?Bar\Baz $param): ?Bar\Baz {}',
  74. '<?php function foo(? Bar\Baz $param): ? Bar\Baz {}',
  75. ];
  76. yield [
  77. '<?php function foo(?Bar\Baz &$param): ?Bar\Baz {}',
  78. '<?php function foo(? Bar\Baz &$param): ? Bar\Baz {}',
  79. ];
  80. yield [
  81. '<?php $foo = function(?Bar\Baz $param): ?Bar\Baz {};',
  82. '<?php $foo = function(? Bar\Baz $param): ? Bar\Baz {};',
  83. ];
  84. yield [
  85. '<?php $foo = function(?Bar\Baz &$param): ?Bar\Baz {};',
  86. '<?php $foo = function(? Bar\Baz &$param): ? Bar\Baz {};',
  87. ];
  88. yield [
  89. '<?php class Test { public function foo(?Bar\Baz $param): ?Bar\Baz {} }',
  90. '<?php class Test { public function foo(? Bar\Baz $param): ? Bar\Baz {} }',
  91. ];
  92. yield [
  93. '<?php abstract class Test { abstract public function foo(?Bar\Baz $param); }',
  94. '<?php abstract class Test { abstract public function foo(? Bar\Baz $param); }',
  95. ];
  96. yield [
  97. '<?php $foo = function(?array $a,
  98. ?array $b): ?Bar\Baz {};',
  99. '<?php $foo = function(?
  100. array $a,
  101. ? array $b): ?
  102. Bar\Baz {};',
  103. ];
  104. yield [
  105. '<?php function foo(?array ...$param): ?array {}',
  106. '<?php function foo(? array ...$param): ? array {}',
  107. ];
  108. yield [
  109. '<?php class Foo { private ?string $foo; }',
  110. '<?php class Foo { private ? string $foo; }',
  111. ];
  112. yield [
  113. '<?php class Foo { protected ?string $foo; }',
  114. '<?php class Foo { protected ? string $foo; }',
  115. ];
  116. yield [
  117. '<?php class Foo { public ?string $foo; }',
  118. '<?php class Foo { public ? string $foo; }',
  119. ];
  120. yield [
  121. '<?php class Foo { var ?Foo\Bar $foo; }',
  122. '<?php class Foo { var ? Foo\Bar $foo; }',
  123. ];
  124. yield [
  125. '<?php $foo = fn(?Bar\Baz $param): ?Bar\Baz => null;',
  126. '<?php $foo = fn(? Bar\Baz $param): ? Bar\Baz => null;',
  127. ];
  128. yield [
  129. '<?php $foo = fn(?Bar\Baz &$param): ?Bar\Baz => null;',
  130. '<?php $foo = fn(? Bar\Baz &$param): ? Bar\Baz => null;',
  131. ];
  132. yield [
  133. '<?php $foo = fn(?array $a,
  134. ?array $b): ?Bar\Baz => null;',
  135. '<?php $foo = fn(?
  136. array $a,
  137. ? array $b): ?
  138. Bar\Baz => null;',
  139. ];
  140. }
  141. /**
  142. * @dataProvider provideFix80Cases
  143. *
  144. * @requires PHP 8.0
  145. */
  146. public function testFix80(string $expected, ?string $input = null): void
  147. {
  148. $this->doTest($expected, $input);
  149. }
  150. /**
  151. * @return iterable<string, array{string, string}>
  152. */
  153. public static function provideFix80Cases(): iterable
  154. {
  155. yield 'static return' => [
  156. "<?php\nclass Foo { public function bar(): ?static {} }\n",
  157. "<?php\nclass Foo { public function bar(): ? static {} }\n",
  158. ];
  159. }
  160. }