StrictParamFixerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\Strict;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Strict\StrictParamFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Strict\StrictParamFixer>
  22. */
  23. final class StrictParamFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php
  39. in_array(1, $a, true);
  40. in_array(1, $a, false);
  41. in_array(1, $a, $useStrict);',
  42. ];
  43. yield [
  44. '<?php class Foo
  45. {
  46. public function in_array($needle, $haystack) {}
  47. }',
  48. ];
  49. yield [
  50. '<?php
  51. in_array(1, $a, true);',
  52. '<?php
  53. in_array(1, $a);',
  54. ];
  55. yield [
  56. '<?php
  57. in_array(1, foo(), true);',
  58. '<?php
  59. in_array(1, foo());',
  60. ];
  61. yield [
  62. '<?php
  63. in_array(1, array(1, 2, 3), true);',
  64. '<?php
  65. in_array(1, array(1, 2, 3));',
  66. ];
  67. yield [
  68. '<?php
  69. in_array(1, [1, 2, 3], true);',
  70. '<?php
  71. in_array(1, [1, 2, 3]);',
  72. ];
  73. yield [
  74. '<?php
  75. in_array(in_array(1, [1, in_array(1, [1, 2, 3], true) ? 21 : 22, 3], true) ? 111 : 222, [1, in_array(1, [1, 2, 3], true) ? 21 : 22, 3], true);',
  76. '<?php
  77. in_array(in_array(1, [1, in_array(1, [1, 2, 3]) ? 21 : 22, 3]) ? 111 : 222, [1, in_array(1, [1, 2, 3]) ? 21 : 22, 3]);',
  78. ];
  79. yield [
  80. '<?php
  81. in_Array(1, $a, true);',
  82. '<?php
  83. in_Array(1, $a);',
  84. ];
  85. yield [
  86. '<?php
  87. base64_decode($foo, true);
  88. base64_decode($foo, false);
  89. base64_decode($foo, $useStrict);',
  90. ];
  91. yield [
  92. '<?php
  93. base64_decode($foo, true);',
  94. '<?php
  95. base64_decode($foo);',
  96. ];
  97. yield [
  98. '<?php
  99. array_search($foo, $bar, true);
  100. array_search($foo, $bar, false);
  101. array_search($foo, $bar, $useStrict);',
  102. ];
  103. yield [
  104. '<?php
  105. array_search($foo, $bar, true);',
  106. '<?php
  107. array_search($foo, $bar);',
  108. ];
  109. yield [
  110. '<?php
  111. array_keys($foo);
  112. array_keys($foo, $bar, true);
  113. array_keys($foo, $bar, false);
  114. array_keys($foo, $bar, $useStrict);',
  115. ];
  116. yield [
  117. '<?php
  118. array_keys($foo, $bar, true);',
  119. '<?php
  120. array_keys($foo, $bar);',
  121. ];
  122. yield [
  123. '<?php
  124. mb_detect_encoding($foo, $bar, true);
  125. mb_detect_encoding($foo, $bar, false);
  126. mb_detect_encoding($foo, $bar, $useStrict);',
  127. ];
  128. yield [
  129. '<?php
  130. mb_detect_encoding($foo, mb_detect_order(), true);',
  131. '<?php
  132. mb_detect_encoding($foo);',
  133. ];
  134. yield [
  135. '<?php
  136. use function in_array;
  137. class Foo
  138. {
  139. public function __construct($foo, $bar) {}
  140. }',
  141. ];
  142. yield [
  143. '<?php
  144. namespace Foo {
  145. array_keys($foo, $bar, true);
  146. }
  147. namespace Bar {
  148. use function Foo\LoremIpsum;
  149. array_keys($foo, $bar, true);
  150. }',
  151. '<?php
  152. namespace Foo {
  153. array_keys($foo, $bar);
  154. }
  155. namespace Bar {
  156. use function Foo\LoremIpsum;
  157. array_keys($foo, $bar);
  158. }',
  159. ];
  160. yield [
  161. '<?php
  162. use function \base64_decode;
  163. foo($bar);',
  164. ];
  165. yield [
  166. '<?php
  167. use function Baz\base64_decode;
  168. foo($bar);',
  169. ];
  170. yield [
  171. '<?php
  172. in_array(1, foo(), true /* 1 *//* 2 *//* 3 */);',
  173. '<?php
  174. in_array(1, foo() /* 1 *//* 2 *//* 3 */);',
  175. ];
  176. yield [
  177. '<?php in_array($b, $c, true, );',
  178. '<?php in_array($b, $c, );',
  179. ];
  180. yield [
  181. '<?php in_array($b, $c/* 0 *//* 1 */, true,/* 2 *//* 3 */);',
  182. '<?php in_array($b, $c/* 0 *//* 1 */,/* 2 *//* 3 */);',
  183. ];
  184. }
  185. }