SimplifiedNullReturnFixerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\ReturnNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Graham Campbell <graham@alt-three.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ReturnNotation\SimplifiedNullReturnFixer
  19. */
  20. final class SimplifiedNullReturnFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. return [
  35. // check correct statements aren't changed
  36. ['<?php return ;'],
  37. ['<?php return \'null\';'],
  38. ['<?php return false;'],
  39. ['<?php return (false );'],
  40. ['<?php return null === foo();'],
  41. ['<?php return array() == null ;'],
  42. // check we modified those that can be changed
  43. ['<?php return;', '<?php return null;'],
  44. ['<?php return;', '<?php return (null);'],
  45. ['<?php return;', '<?php return ( null );'],
  46. ['<?php return;', '<?php return ( (( null)));'],
  47. ['<?php return /* hello */;', '<?php return /* hello */ null ;'],
  48. ['<?php return;', '<?php return NULL;'],
  49. ['<?php return;', "<?php return\n(\nnull\n)\n;"],
  50. ];
  51. }
  52. /**
  53. * @param string $expected
  54. * @param null|string $input
  55. *
  56. * @dataProvider provideNullableReturnTypeCases
  57. * @requires PHP 7.1
  58. */
  59. public function test71ReturnTypes($expected, $input = null)
  60. {
  61. $this->doTest($expected, $input);
  62. }
  63. public function provideNullableReturnTypeCases()
  64. {
  65. return [
  66. ['<?php function foo(): ? /* C */ int { return null; }'],
  67. ['<?php function foo(): ?int { if (false) { return null; } }'],
  68. ['<?php function foo(): int { return null; }'],
  69. ['<?php function foo(): A\B\C { return null; }'],
  70. [
  71. '<?php function foo(): ?int { return null; } return;',
  72. '<?php function foo(): ?int { return null; } return null;',
  73. ],
  74. [
  75. '<?php function foo() { return; } function bar(): ?A\B\C\D { return null; } function baz() { return; }',
  76. '<?php function foo() { return null; } function bar(): ?A\B\C\D { return null; } function baz() { return null; }',
  77. ],
  78. [
  79. '<?php function foo(): ?int { $bar = function() { return; }; return null; }',
  80. '<?php function foo(): ?int { $bar = function() { return null; }; return null; }',
  81. ],
  82. [
  83. '<?php function foo(): void { return; }',
  84. '<?php function foo(): void { return null; }',
  85. ],
  86. ];
  87. }
  88. }