NoUnreachableDefaultArgumentValueFixerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer>
  20. */
  21. final class NoUnreachableDefaultArgumentValueFixerTest 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. /**
  31. * @return iterable<array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php function bFunction($foo, $bar) {}',
  37. '<?php function bFunction($foo = null, $bar) {}',
  38. ];
  39. yield [
  40. '<?php function bFunction($foo, $bar) {}',
  41. '<?php function bFunction($foo = "two words", $bar) {}',
  42. ];
  43. yield [
  44. '<?php function cFunction($foo, $bar, $baz) {}',
  45. '<?php function cFunction($foo = false, $bar = "bar", $baz) {}',
  46. ];
  47. yield [
  48. '<?php function dFunction($foo, $bar, $baz) {}',
  49. '<?php function dFunction($foo = false, $bar, $baz) {}',
  50. ];
  51. yield [
  52. '<?php function foo (Foo $bar = null, $baz) {}',
  53. ];
  54. yield [
  55. '<?php function eFunction($foo, $bar, \SplFileInfo $baz, $x = "default") {}',
  56. '<?php function eFunction($foo, $bar = "removedDefault", \SplFileInfo $baz, $x = "default") {}',
  57. ];
  58. yield [
  59. <<<'EOT'
  60. <?php
  61. function eFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
  62. function fFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
  63. EOT,
  64. <<<'EOT'
  65. <?php
  66. function eFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
  67. function fFunction($foo, $bar = 'removedValue', \SplFileInfo $baz, $x = 'default') {};
  68. EOT,
  69. ];
  70. yield [
  71. '<?php function foo ($bar /* a */ /* b */ , $c) {}',
  72. '<?php function foo ($bar /* a */ = /* b */ 1, $c) {}',
  73. ];
  74. yield [
  75. '<?php function hFunction($foo,$bar,\SplFileInfo $baz,$x = 5) {};',
  76. '<?php function hFunction($foo,$bar="removedValue",\SplFileInfo $baz,$x = 5) {};',
  77. ];
  78. yield [
  79. '<?php function eFunction($foo, $bar, \SplFileInfo $baz = null, $x) {}',
  80. '<?php function eFunction($foo = PHP_EOL, $bar, \SplFileInfo $baz = null, $x) {}',
  81. ];
  82. yield [
  83. '<?php function eFunction($foo, $bar) {}',
  84. '<?php function eFunction($foo = null, $bar) {}',
  85. ];
  86. yield [
  87. <<<'EOT'
  88. <?php
  89. function foo(
  90. $a, // test
  91. $b, /* test */
  92. $c, // abc
  93. $d
  94. ) {}
  95. EOT,
  96. <<<'EOT'
  97. <?php
  98. function foo(
  99. $a = 1, // test
  100. $b = 2, /* test */
  101. $c = null, // abc
  102. $d
  103. ) {}
  104. EOT,
  105. ];
  106. yield [
  107. '<?php function foo($foo, $bar) {}',
  108. '<?php function foo($foo = array(array(1)), $bar) {}',
  109. ];
  110. yield [
  111. '<?php function a($a, $b) {}',
  112. '<?php function a($a = array("a" => "b", "c" => "d"), $b) {}',
  113. ];
  114. yield [
  115. '<?php function a($a, $b) {}',
  116. '<?php function a($a = ["a" => "b", "c" => "d"], $b) {}',
  117. ];
  118. yield [
  119. '<?php function a($a, $b) {}',
  120. '<?php function a($a = NULL, $b) {}',
  121. ];
  122. yield [
  123. '<?php function a(\SplFileInfo $a = Null, $b) {}',
  124. ];
  125. yield [
  126. '<?php function a(array $a = null, $b) {}',
  127. ];
  128. yield [
  129. '<?php function a(callable $a = null, $b) {}',
  130. ];
  131. yield [
  132. '<?php function a(\SplFileInfo &$a = Null, $b) {}',
  133. ];
  134. yield [
  135. '<?php function a(&$a, $b) {}',
  136. '<?php function a(&$a = null, $b) {}',
  137. ];
  138. yield [
  139. '<?php $fnc = function ($a, $b = 1) use ($c) {};',
  140. ];
  141. yield [
  142. '<?php $fnc = function ($a, $b) use ($c) {};',
  143. '<?php $fnc = function ($a = 1, $b) use ($c) {};',
  144. ];
  145. yield [
  146. '<?php function bFunction($foo#
  147. #
  148. #
  149. ,#
  150. $bar) {}',
  151. '<?php function bFunction($foo#
  152. =#
  153. null#
  154. ,#
  155. $bar) {}',
  156. ];
  157. yield [
  158. '<?php function a($a = 1, ...$b) {}',
  159. ];
  160. yield [
  161. '<?php function a($a = 1, \SplFileInfo ...$b) {}',
  162. ];
  163. yield [
  164. '<?php function foo (?Foo $bar, $baz) {}',
  165. '<?php function foo (?Foo $bar = null, $baz) {}',
  166. ];
  167. yield [
  168. '<?php function foo (?Foo $bar = null, ?Baz $baz = null) {}',
  169. ];
  170. yield [
  171. '<?php $fn = fn ($a, $b) => null;',
  172. '<?php $fn = fn ($a = 1, $b) => null;',
  173. ];
  174. }
  175. /**
  176. * @dataProvider provideFix80Cases
  177. *
  178. * @requires PHP 8.0
  179. */
  180. public function testFix80(string $expected, ?string $input = null): void
  181. {
  182. $this->doTest($expected, $input);
  183. }
  184. /**
  185. * @return iterable<string, array{0: string, 1?: string}>
  186. */
  187. public static function provideFix80Cases(): iterable
  188. {
  189. yield 'handle trailing comma' => [
  190. '<?php function foo($x, $y = 42, $z = 42 ) {}',
  191. ];
  192. yield 'handle attributes without arguments' => [
  193. '<?php function foo(
  194. #[Attribute1] $x,
  195. #[Attribute2] $y,
  196. #[Attribute3] $z
  197. ) {}',
  198. '<?php function foo(
  199. #[Attribute1] $x,
  200. #[Attribute2] $y = 42,
  201. #[Attribute3] $z
  202. ) {}',
  203. ];
  204. yield 'handle attributes with arguments' => [
  205. '<?php function foo(
  206. #[Attribute1(1, 2)] $x,
  207. #[Attribute2(3, 4)] $y,
  208. #[Attribute3(5, 6)] $z
  209. ) {}',
  210. '<?php function foo(
  211. #[Attribute1(1, 2)] $x,
  212. #[Attribute2(3, 4)] $y = 42,
  213. #[Attribute3(5, 6)] $z
  214. ) {}',
  215. ];
  216. }
  217. /**
  218. * @dataProvider provideFix81Cases
  219. *
  220. * @requires PHP 8.1
  221. */
  222. public function testFix81(string $expected, ?string $input = null): void
  223. {
  224. $this->doTest($expected, $input);
  225. }
  226. /**
  227. * @return iterable<string, array{string}>
  228. */
  229. public static function provideFix81Cases(): iterable
  230. {
  231. yield 'do not crash' => [
  232. '<?php strlen( ... );',
  233. ];
  234. }
  235. }