CompactNullableTypehintFixerTest.php 5.3 KB

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