ModernizeStrposFixerTest.php 5.5 KB

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