SimplifiedNullReturnFixerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\ReturnNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Graham Campbell <hello@gjcampbell.co.uk>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ReturnNotation\SimplifiedNullReturnFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ReturnNotation\SimplifiedNullReturnFixer>
  22. */
  23. final class SimplifiedNullReturnFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. // check correct statements aren't changed
  38. yield ['<?php return ;'];
  39. yield ['<?php return \'null\';'];
  40. yield ['<?php return false;'];
  41. yield ['<?php return (false );'];
  42. yield ['<?php return null === foo();'];
  43. yield ['<?php return array() == null ;'];
  44. // check we modified those that can be changed
  45. yield ['<?php return;', '<?php return null;'];
  46. yield ['<?php return;', '<?php return (null);'];
  47. yield ['<?php return;', '<?php return ( null );'];
  48. yield ['<?php return;', '<?php return ( (( null)));'];
  49. yield ['<?php return /* hello */;', '<?php return /* hello */ null ;'];
  50. yield ['<?php return;', '<?php return NULL;'];
  51. yield ['<?php return;', "<?php return\n(\nnull\n)\n;"];
  52. yield ['<?php function foo(): ? /* C */ int { return null; }'];
  53. yield ['<?php function foo(): ?int { if (false) { return null; } }'];
  54. yield ['<?php function foo(): int { return null; }'];
  55. yield ['<?php function foo(): A\B\C { return null; }'];
  56. yield [
  57. '<?php function foo(): ?int { return null; } return;',
  58. '<?php function foo(): ?int { return null; } return null;',
  59. ];
  60. yield [
  61. '<?php function foo() { return; } function bar(): ?A\B\C\D { return null; } function baz() { return; }',
  62. '<?php function foo() { return null; } function bar(): ?A\B\C\D { return null; } function baz() { return null; }',
  63. ];
  64. yield [
  65. '<?php function foo(): ?int { $bar = function() { return; }; return null; }',
  66. '<?php function foo(): ?int { $bar = function() { return null; }; return null; }',
  67. ];
  68. yield [
  69. '<?php function foo(): void { return; }',
  70. ];
  71. yield ['<?php return ?>', '<?php return null ?>'];
  72. yield ['<?php return [] ?>'];
  73. yield [
  74. '<?php
  75. return // hello
  76. ?>
  77. ',
  78. '<?php
  79. return null // hello
  80. ?>
  81. ',
  82. ];
  83. yield [
  84. '<?php
  85. return
  86. // hello
  87. ?>
  88. ',
  89. '<?php
  90. return null
  91. // hello
  92. ?>
  93. ',
  94. ];
  95. yield [
  96. '<?php
  97. return // hello
  98. ;
  99. ',
  100. '<?php
  101. return null // hello
  102. ;
  103. ',
  104. ];
  105. yield [
  106. '<?php
  107. return
  108. // hello
  109. ;
  110. ',
  111. '<?php
  112. return null
  113. // hello
  114. ;
  115. ',
  116. ];
  117. }
  118. /**
  119. * @requires PHP 8.0
  120. *
  121. * @dataProvider provideFix80Cases
  122. */
  123. public function testFix80(string $expected, ?string $input = null): void
  124. {
  125. $this->doTest($expected, $input);
  126. }
  127. /**
  128. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  129. */
  130. public static function provideFix80Cases(): iterable
  131. {
  132. yield [
  133. '<?php
  134. function test(): null|int
  135. {
  136. if (true) { return null; }
  137. return 42;
  138. }',
  139. ];
  140. yield [
  141. '<?php
  142. function test(): null|array
  143. {
  144. if (true) { return null; }
  145. return [];
  146. }',
  147. ];
  148. yield [
  149. '<?php
  150. function test(): array|null
  151. {
  152. if (true) { return null; }
  153. return [];
  154. }',
  155. ];
  156. }
  157. }