NoUnreachableDefaultArgumentValueFixerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. 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. '<?php function a($a = 1, ...$b) {}',
  157. ],
  158. [
  159. '<?php function a($a = 1, \SplFileInfo ...$b) {}',
  160. ],
  161. [
  162. '<?php function foo (?Foo $bar, $baz) {}',
  163. '<?php function foo (?Foo $bar = null, $baz) {}',
  164. ],
  165. [
  166. '<?php function foo (?Foo $bar = null, ?Baz $baz = null) {}',
  167. ],
  168. [
  169. '<?php $fn = fn ($a, $b) => null;',
  170. '<?php $fn = fn ($a = 1, $b) => null;',
  171. ],
  172. ];
  173. }
  174. /**
  175. * @dataProvider provideFix80Cases
  176. *
  177. * @requires PHP 8.0
  178. */
  179. public function testFix80(string $expected, ?string $input = null): void
  180. {
  181. $this->doTest($expected, $input);
  182. }
  183. public static function provideFix80Cases(): iterable
  184. {
  185. yield 'handle trailing comma' => [
  186. '<?php function foo($x, $y = 42, $z = 42 ) {}',
  187. ];
  188. yield 'handle attributes without arguments' => [
  189. '<?php function foo(
  190. #[Attribute1] $x,
  191. #[Attribute2] $y,
  192. #[Attribute3] $z
  193. ) {}',
  194. '<?php function foo(
  195. #[Attribute1] $x,
  196. #[Attribute2] $y = 42,
  197. #[Attribute3] $z
  198. ) {}',
  199. ];
  200. yield 'handle attributes with arguments' => [
  201. '<?php function foo(
  202. #[Attribute1(1, 2)] $x,
  203. #[Attribute2(3, 4)] $y,
  204. #[Attribute3(5, 6)] $z
  205. ) {}',
  206. '<?php function foo(
  207. #[Attribute1(1, 2)] $x,
  208. #[Attribute2(3, 4)] $y = 42,
  209. #[Attribute3(5, 6)] $z
  210. ) {}',
  211. ];
  212. }
  213. /**
  214. * @dataProvider provideFix81Cases
  215. *
  216. * @requires PHP 8.1
  217. */
  218. public function testFix81(string $expected, ?string $input = null): void
  219. {
  220. $this->doTest($expected, $input);
  221. }
  222. public static function provideFix81Cases(): iterable
  223. {
  224. yield 'do not crash' => [
  225. '<?php strlen( ... );',
  226. ];
  227. }
  228. }