ModernizeStrposFixerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\Alias;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Alexander M. Turek <me@derrabus.de>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Alias\ModernizeStrposFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\ModernizeStrposFixer>
  22. */
  23. final class ModernizeStrposFixerTest 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<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'yoda ===' => [
  38. '<?php if ( str_starts_with($haystack1, $needle)) {}',
  39. '<?php if (0 === strpos($haystack1, $needle)) {}',
  40. ];
  41. yield 'not zero yoda !==' => [
  42. '<?php if ( !str_starts_with($haystack2, $needle)) {}',
  43. '<?php if (0 !== strpos($haystack2, $needle)) {}',
  44. ];
  45. yield 'false yoda ===' => [
  46. '<?php if ( !str_contains($haystack, $needle)) {}',
  47. '<?php if (false === strpos($haystack, $needle)) {}',
  48. ];
  49. yield [
  50. '<?php if (str_starts_with($haystack3, $needle) ) {}',
  51. '<?php if (strpos($haystack3, $needle) === 0) {}',
  52. ];
  53. yield 'casing call' => [
  54. '<?php if (str_starts_with($haystack4, $needle) ) {}',
  55. '<?php if (STRPOS($haystack4, $needle) === 0) {}',
  56. ];
  57. yield 'leading namespace' => [
  58. '<?php if (\str_starts_with($haystack5, $needle) ) {}',
  59. '<?php if (\strpos($haystack5, $needle) === 0) {}',
  60. ];
  61. yield 'leading namespace with yoda' => [
  62. '<?php if ( \str_starts_with($haystack5, $needle)) {}',
  63. '<?php if (0 === \strpos($haystack5, $needle)) {}',
  64. ];
  65. yield [
  66. '<?php if (!str_starts_with($haystack6, $needle) ) {}',
  67. '<?php if (strpos($haystack6, $needle) !== 0) {}',
  68. ];
  69. yield [
  70. '<?php if (!\str_starts_with($haystack6, $needle) ) {}',
  71. '<?php if (\strpos($haystack6, $needle) !== 0) {}',
  72. ];
  73. yield [
  74. '<?php if ( !\str_starts_with($haystack6, $needle)) {}',
  75. '<?php if (0 !== \strpos($haystack6, $needle)) {}',
  76. ];
  77. yield 'casing operand' => [
  78. '<?php if (str_contains($haystack7, $needle) ) {}',
  79. '<?php if (strpos($haystack7, $needle) !== FALSE) {}',
  80. ];
  81. yield [
  82. '<?php if (!str_contains($haystack8, $needle) ) {}',
  83. '<?php if (strpos($haystack8, $needle) === false) {}',
  84. ];
  85. yield [
  86. '<?php if ( !str_starts_with($haystack9, $needle)) {}',
  87. '<?php if (0 !== strpos($haystack9, $needle)) {}',
  88. ];
  89. yield [
  90. '<?php $a = !str_starts_with($haystack9a, $needle) ;',
  91. '<?php $a = strpos($haystack9a, $needle) !== 0;',
  92. ];
  93. yield 'comments inside, no spacing' => [
  94. '<?php if (/* foo *//* bar */str_contains($haystack10,$a)) {}',
  95. '<?php if (/* foo */false/* bar */!==strpos($haystack10,$a)) {}',
  96. ];
  97. yield [
  98. '<?php $a = !str_contains($haystack11, $needle)?>',
  99. '<?php $a = false === strpos($haystack11, $needle)?>',
  100. ];
  101. yield [
  102. '<?php $a = $input && str_contains($input, $method) ? $input : null;',
  103. '<?php $a = $input && strpos($input, $method) !== FALSE ? $input : null;',
  104. ];
  105. // do not fix
  106. yield [
  107. '<?php
  108. $x = 1;
  109. $x = "strpos";
  110. // if (false === strpos($haystack12, $needle)) {}
  111. /** if (false === strpos($haystack13, $needle)) {} */
  112. ',
  113. ];
  114. yield 'different namespace' => [
  115. '<?php if (a\strpos($haystack14, $needle) === 0) {}',
  116. ];
  117. yield 'different namespace with yoda' => [
  118. '<?php if (0 === a\strpos($haystack14, $needle)) {}',
  119. ];
  120. yield 'non condition (hardcoded)' => [
  121. '<?php $x = strpos(\'foo\', \'f\');',
  122. ];
  123. yield 'non condition' => [
  124. '<?php $x = strpos($haystack15, $needle) ?>',
  125. ];
  126. yield 'none zero int' => [
  127. '<?php if (1 !== strpos($haystack16, $needle)) {}',
  128. ];
  129. yield 'greater condition' => [
  130. '<?php if (strpos($haystack17, $needle) > 0) {}',
  131. ];
  132. yield 'lesser condition' => [
  133. '<?php if (0 < strpos($haystack18, $needle)) {}',
  134. ];
  135. yield 'no argument' => [
  136. '<?php $z = strpos();',
  137. ];
  138. yield 'one argument' => [
  139. '<?php if (0 === strpos($haystack1)) {}',
  140. ];
  141. yield '3 arguments' => [
  142. '<?php if (0 === strpos($haystack1, $a, $b)) {}',
  143. ];
  144. yield 'higher precedence 1' => [
  145. '<?php if (4 + 0 !== strpos($haystack9, $needle)) {}',
  146. ];
  147. yield 'higher precedence 2' => [
  148. '<?php if (!false === strpos($haystack, $needle)) {}',
  149. ];
  150. yield 'higher precedence 3' => [
  151. '<?php $a = strpos($haystack, $needle) === 0 + 1;',
  152. ];
  153. yield 'higher precedence 4' => [
  154. '<?php $a = strpos($haystack, $needle) === 0 > $b;',
  155. ];
  156. }
  157. }