SimplifiedNullReturnFixerTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. final class SimplifiedNullReturnFixerTest 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. // check correct statements aren't changed
  33. yield ['<?php return ;'];
  34. yield ['<?php return \'null\';'];
  35. yield ['<?php return false;'];
  36. yield ['<?php return (false );'];
  37. yield ['<?php return null === foo();'];
  38. yield ['<?php return array() == null ;'];
  39. // check we modified those that can be changed
  40. yield ['<?php return;', '<?php return null;'];
  41. yield ['<?php return;', '<?php return (null);'];
  42. yield ['<?php return;', '<?php return ( null );'];
  43. yield ['<?php return;', '<?php return ( (( null)));'];
  44. yield ['<?php return /* hello */;', '<?php return /* hello */ null ;'];
  45. yield ['<?php return;', '<?php return NULL;'];
  46. yield ['<?php return;', "<?php return\n(\nnull\n)\n;"];
  47. yield ['<?php function foo(): ? /* C */ int { return null; }'];
  48. yield ['<?php function foo(): ?int { if (false) { return null; } }'];
  49. yield ['<?php function foo(): int { return null; }'];
  50. yield ['<?php function foo(): A\B\C { return null; }'];
  51. yield [
  52. '<?php function foo(): ?int { return null; } return;',
  53. '<?php function foo(): ?int { return null; } return null;',
  54. ];
  55. yield [
  56. '<?php function foo() { return; } function bar(): ?A\B\C\D { return null; } function baz() { return; }',
  57. '<?php function foo() { return null; } function bar(): ?A\B\C\D { return null; } function baz() { return null; }',
  58. ];
  59. yield [
  60. '<?php function foo(): ?int { $bar = function() { return; }; return null; }',
  61. '<?php function foo(): ?int { $bar = function() { return null; }; return null; }',
  62. ];
  63. yield [
  64. '<?php function foo(): void { return; }',
  65. ];
  66. }
  67. }