NoUnreachableDefaultArgumentValueFixerTest.php 7.4 KB

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