NoUnreachableDefaultArgumentValueFixerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\FunctionNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer
  18. */
  19. final class NoUnreachableDefaultArgumentValueFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @dataProvider provideFixCases
  23. */
  24. public function testFix(string $expected, ?string $input = null): void
  25. {
  26. $this->doTest($expected, $input);
  27. }
  28. public function provideFixCases(): array
  29. {
  30. return [
  31. [
  32. '<?php function bFunction($foo, $bar) {}',
  33. '<?php function bFunction($foo = null, $bar) {}',
  34. ],
  35. [
  36. '<?php function bFunction($foo, $bar) {}',
  37. '<?php function bFunction($foo = "two words", $bar) {}',
  38. ],
  39. [
  40. '<?php function cFunction($foo, $bar, $baz) {}',
  41. '<?php function cFunction($foo = false, $bar = "bar", $baz) {}',
  42. ],
  43. [
  44. '<?php function dFunction($foo, $bar, $baz) {}',
  45. '<?php function dFunction($foo = false, $bar, $baz) {}',
  46. ],
  47. [
  48. '<?php function foo (Foo $bar = null, $baz) {}',
  49. ],
  50. [
  51. '<?php function eFunction($foo, $bar, \SplFileInfo $baz, $x = "default") {}',
  52. '<?php function eFunction($foo, $bar = "removedDefault", \SplFileInfo $baz, $x = "default") {}',
  53. ],
  54. [
  55. <<<'EOT'
  56. <?php
  57. function eFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
  58. function fFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
  59. EOT
  60. ,
  61. <<<'EOT'
  62. <?php
  63. function eFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
  64. function fFunction($foo, $bar = 'removedValue', \SplFileInfo $baz, $x = 'default') {};
  65. EOT
  66. ],
  67. [
  68. '<?php function foo ($bar /* a */ /* b */ , $c) {}',
  69. '<?php function foo ($bar /* a */ = /* b */ 1, $c) {}',
  70. ],
  71. [
  72. '<?php function hFunction($foo,$bar,\SplFileInfo $baz,$x = 5) {};',
  73. '<?php function hFunction($foo,$bar="removedValue",\SplFileInfo $baz,$x = 5) {};',
  74. ],
  75. [
  76. '<?php function eFunction($foo, $bar, \SplFileInfo $baz = null, $x) {}',
  77. '<?php function eFunction($foo = PHP_EOL, $bar, \SplFileInfo $baz = null, $x) {}',
  78. ],
  79. [
  80. '<?php function eFunction($foo, $bar) {}',
  81. '<?php function eFunction($foo = null, $bar) {}',
  82. ],
  83. [
  84. <<<'EOT'
  85. <?php
  86. function foo(
  87. $a, // test
  88. $b, /* test */
  89. $c, // abc
  90. $d
  91. ) {}
  92. EOT
  93. ,
  94. <<<'EOT'
  95. <?php
  96. function foo(
  97. $a = 1, // test
  98. $b = 2, /* test */
  99. $c = null, // abc
  100. $d
  101. ) {}
  102. EOT
  103. ],
  104. [
  105. '<?php function foo($foo, $bar) {}',
  106. '<?php function foo($foo = array(array(1)), $bar) {}',
  107. ],
  108. [
  109. '<?php function a($a, $b) {}',
  110. '<?php function a($a = array("a" => "b", "c" => "d"), $b) {}',
  111. ],
  112. [
  113. '<?php function a($a, $b) {}',
  114. '<?php function a($a = ["a" => "b", "c" => "d"], $b) {}',
  115. ],
  116. [
  117. '<?php function a($a, $b) {}',
  118. '<?php function a($a = NULL, $b) {}',
  119. ],
  120. [
  121. '<?php function a(\SplFileInfo $a = Null, $b) {}',
  122. ],
  123. [
  124. '<?php function a(array $a = null, $b) {}',
  125. ],
  126. [
  127. '<?php function a(callable $a = null, $b) {}',
  128. ],
  129. [
  130. '<?php function a(\SplFileInfo &$a = Null, $b) {}',
  131. ],
  132. [
  133. '<?php function a(&$a, $b) {}',
  134. '<?php function a(&$a = null, $b) {}',
  135. ],
  136. [
  137. '<?php $fnc = function ($a, $b = 1) use ($c) {};',
  138. ],
  139. [
  140. '<?php $fnc = function ($a, $b) use ($c) {};',
  141. '<?php $fnc = function ($a = 1, $b) use ($c) {};',
  142. ],
  143. [
  144. '<?php function bFunction($foo#
  145. #
  146. #
  147. ,#
  148. $bar) {}',
  149. '<?php function bFunction($foo#
  150. =#
  151. null#
  152. ,#
  153. $bar) {}',
  154. ],
  155. ];
  156. }
  157. /**
  158. * @dataProvider provideFix56Cases
  159. */
  160. public function testFix56(string $expected, ?string $input = null): void
  161. {
  162. $this->doTest($expected, $input);
  163. }
  164. public function provideFix56Cases(): array
  165. {
  166. return [
  167. [
  168. '<?php function a($a = 1, ...$b) {}',
  169. ],
  170. [
  171. '<?php function a($a = 1, \SplFileInfo ...$b) {}',
  172. ],
  173. ];
  174. }
  175. /**
  176. * @dataProvider provideFix71Cases
  177. *
  178. * @requires PHP 7.1
  179. */
  180. public function testFix71(string $expected, ?string $input = null): void
  181. {
  182. $this->doTest($expected, $input);
  183. }
  184. public function provideFix71Cases(): array
  185. {
  186. return [
  187. [
  188. '<?php function foo (?Foo $bar, $baz) {}',
  189. '<?php function foo (?Foo $bar = null, $baz) {}',
  190. ],
  191. [
  192. '<?php function foo (?Foo $bar = null, ?Baz $baz = null) {}',
  193. ],
  194. ];
  195. }
  196. /**
  197. * @dataProvider provideFix74Cases
  198. * @requires PHP 7.4
  199. */
  200. public function testFix74(string $expected, ?string $input = null): void
  201. {
  202. $this->doTest($expected, $input);
  203. }
  204. public function provideFix74Cases(): array
  205. {
  206. return [
  207. [
  208. '<?php $fn = fn ($a, $b) => null;',
  209. '<?php $fn = fn ($a = 1, $b) => null;',
  210. ],
  211. ];
  212. }
  213. }