ModernizeStrposFixerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Fixer\Alias\ModernizeStrposFixer;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. /**
  17. * @author Alexander M. Turek <me@derrabus.de>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\Alias\ModernizeStrposFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\ModernizeStrposFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\ModernizeStrposFixer
  26. */
  27. final class ModernizeStrposFixerTest extends AbstractFixerTestCase
  28. {
  29. public function testConfigure(): void
  30. {
  31. $this->fixer->configure(['modernize_stripos' => true]);
  32. self::assertSame(
  33. ['modernize_stripos' => true],
  34. \Closure::bind(static fn (ModernizeStrposFixer $fixer): array => $fixer->configuration, null, ModernizeStrposFixer::class)($this->fixer)
  35. );
  36. }
  37. public function testInvalidConfiguration(): void
  38. {
  39. $this->expectException(InvalidFixerConfigurationException::class);
  40. $this->expectExceptionMessage('[modernize_strpos] Invalid configuration: The option "invalid" does not exist. Defined options are: "modernize_stripos".');
  41. $this->fixer->configure(['invalid' => true]);
  42. }
  43. /**
  44. * @param _AutogeneratedInputConfiguration $configuration
  45. *
  46. * @dataProvider provideFixCases
  47. */
  48. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  49. {
  50. $this->fixer->configure($configuration);
  51. $this->doTest($expected, $input);
  52. }
  53. /**
  54. * @return iterable<int|string, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
  55. */
  56. public static function provideFixCases(): iterable
  57. {
  58. yield 'yoda ===' => [
  59. '<?php if ( str_starts_with($haystack1, $needle)) {}',
  60. '<?php if (0 === strpos($haystack1, $needle)) {}',
  61. ];
  62. yield 'case insensitive yoda ===' => [
  63. '<?php if ( str_starts_with(strtolower($haystack1), strtolower($needle))) {}',
  64. '<?php if (0 === stripos($haystack1, $needle)) {}',
  65. ['modernize_stripos' => true],
  66. ];
  67. yield 'not zero yoda !==' => [
  68. '<?php if ( !str_starts_with($haystack2, $needle)) {}',
  69. '<?php if (0 !== strpos($haystack2, $needle)) {}',
  70. ];
  71. yield 'case insensitive not zero yoda !==' => [
  72. '<?php if ( !str_starts_with(strtolower($haystack2), strtolower($needle))) {}',
  73. '<?php if (0 !== stripos($haystack2, $needle)) {}',
  74. ['modernize_stripos' => true],
  75. ];
  76. yield 'false yoda ===' => [
  77. '<?php if ( !str_contains($haystack, $needle)) {}',
  78. '<?php if (false === strpos($haystack, $needle)) {}',
  79. ];
  80. yield 'case insensitive false yoda ===' => [
  81. '<?php if ( !str_contains(strtolower($haystack), strtolower($needle))) {}',
  82. '<?php if (false === stripos($haystack, $needle)) {}',
  83. ['modernize_stripos' => true],
  84. ];
  85. yield [
  86. '<?php if (str_starts_with($haystack3, $needle) ) {}',
  87. '<?php if (strpos($haystack3, $needle) === 0) {}',
  88. ];
  89. yield [
  90. '<?php if (str_starts_with(strtolower($haystack3), strtolower($needle)) ) {}',
  91. '<?php if (stripos($haystack3, $needle) === 0) {}',
  92. ['modernize_stripos' => true],
  93. ];
  94. yield 'casing call' => [
  95. '<?php if (str_starts_with($haystack4, $needle) ) {}',
  96. '<?php if (STRPOS($haystack4, $needle) === 0) {}',
  97. ];
  98. yield 'case insensitive casing call' => [
  99. '<?php if (str_starts_with(strtolower($haystack4), strtolower($needle)) ) {}',
  100. '<?php if (STRIPOS($haystack4, $needle) === 0) {}',
  101. ['modernize_stripos' => true],
  102. ];
  103. yield 'leading namespace' => [
  104. '<?php if (\str_starts_with($haystack5, $needle) ) {}',
  105. '<?php if (\strpos($haystack5, $needle) === 0) {}',
  106. ];
  107. yield 'case insensitive leading namespace' => [
  108. '<?php if (\str_starts_with(\strtolower($haystack5), \strtolower($needle)) ) {}',
  109. '<?php if (\stripos($haystack5, $needle) === 0) {}',
  110. ['modernize_stripos' => true],
  111. ];
  112. yield 'leading namespace with yoda' => [
  113. '<?php if ( \str_starts_with($haystack5, $needle)) {}',
  114. '<?php if (0 === \strpos($haystack5, $needle)) {}',
  115. ];
  116. yield 'case insensitive leading namespace with yoda' => [
  117. '<?php if ( \str_starts_with(\strtolower($haystack5), \strtolower($needle))) {}',
  118. '<?php if (0 === \stripos($haystack5, $needle)) {}',
  119. ['modernize_stripos' => true],
  120. ];
  121. yield [
  122. '<?php if (!str_starts_with($haystack6, $needle) ) {}',
  123. '<?php if (strpos($haystack6, $needle) !== 0) {}',
  124. ];
  125. yield [
  126. '<?php if (!str_starts_with(strtolower($haystack6), strtolower($needle)) ) {}',
  127. '<?php if (stripos($haystack6, $needle) !== 0) {}',
  128. ['modernize_stripos' => true],
  129. ];
  130. yield [
  131. '<?php if (!\str_starts_with($haystack6, $needle) ) {}',
  132. '<?php if (\strpos($haystack6, $needle) !== 0) {}',
  133. ];
  134. yield [
  135. '<?php if (!\str_starts_with(\strtolower($haystack6), \strtolower($needle)) ) {}',
  136. '<?php if (\stripos($haystack6, $needle) !== 0) {}',
  137. ['modernize_stripos' => true],
  138. ];
  139. yield [
  140. '<?php if ( !\str_starts_with($haystack6, $needle)) {}',
  141. '<?php if (0 !== \strpos($haystack6, $needle)) {}',
  142. ];
  143. yield [
  144. '<?php if ( !\str_starts_with(\strtolower($haystack6), \strtolower($needle))) {}',
  145. '<?php if (0 !== \stripos($haystack6, $needle)) {}',
  146. ['modernize_stripos' => true],
  147. ];
  148. yield 'casing operand' => [
  149. '<?php if (str_contains($haystack7, $needle) ) {}',
  150. '<?php if (strpos($haystack7, $needle) !== FALSE) {}',
  151. ];
  152. yield 'case insensitive casing operand' => [
  153. '<?php if (str_contains(strtolower($haystack7), strtolower($needle)) ) {}',
  154. '<?php if (stripos($haystack7, $needle) !== FALSE) {}',
  155. ['modernize_stripos' => true],
  156. ];
  157. yield [
  158. '<?php if (!str_contains($haystack8, $needle) ) {}',
  159. '<?php if (strpos($haystack8, $needle) === false) {}',
  160. ];
  161. yield [
  162. '<?php if (!str_contains(strtolower($haystack8), strtolower($needle)) ) {}',
  163. '<?php if (stripos($haystack8, $needle) === false) {}',
  164. ['modernize_stripos' => true],
  165. ];
  166. yield [
  167. '<?php if ( !str_starts_with($haystack9, $needle)) {}',
  168. '<?php if (0 !== strpos($haystack9, $needle)) {}',
  169. ];
  170. yield [
  171. '<?php if ( !str_starts_with(strtolower($haystack9), strtolower($needle))) {}',
  172. '<?php if (0 !== stripos($haystack9, $needle)) {}',
  173. ['modernize_stripos' => true],
  174. ];
  175. yield [
  176. '<?php $a = !str_starts_with($haystack9a, $needle) ;',
  177. '<?php $a = strpos($haystack9a, $needle) !== 0;',
  178. ];
  179. yield [
  180. '<?php $a = !str_starts_with(strtolower($haystack9a), strtolower($needle)) ;',
  181. '<?php $a = stripos($haystack9a, $needle) !== 0;',
  182. ['modernize_stripos' => true],
  183. ];
  184. yield 'comments inside, no spacing' => [
  185. '<?php if (/* foo *//* bar */str_contains($haystack10,$a)) {}',
  186. '<?php if (/* foo */false/* bar */!==strpos($haystack10,$a)) {}',
  187. ];
  188. yield 'case insensitive comments inside, no spacing' => [
  189. '<?php if (/* foo *//* bar */str_contains(strtolower($haystack10),strtolower($a))) {}',
  190. '<?php if (/* foo */false/* bar */!==stripos($haystack10,$a)) {}',
  191. ['modernize_stripos' => true],
  192. ];
  193. yield [
  194. '<?php $a = !str_contains($haystack11, $needle)?>',
  195. '<?php $a = false === strpos($haystack11, $needle)?>',
  196. ];
  197. yield [
  198. '<?php $a = !str_contains(strtolower($haystack11), strtolower($needle))?>',
  199. '<?php $a = false === stripos($haystack11, $needle)?>',
  200. ['modernize_stripos' => true],
  201. ];
  202. yield [
  203. '<?php $a = $input && str_contains($input, $method) ? $input : null;',
  204. '<?php $a = $input && strpos($input, $method) !== FALSE ? $input : null;',
  205. ];
  206. yield [
  207. '<?php $a = $input && str_contains(strtolower($input), strtolower($method)) ? $input : null;',
  208. '<?php $a = $input && stripos($input, $method) !== FALSE ? $input : null;',
  209. ['modernize_stripos' => true],
  210. ];
  211. yield [
  212. '<?php !str_starts_with(strtolower($file), strtolower($needle.\DIRECTORY_SEPARATOR));',
  213. '<?php 0 !== stripos($file, $needle.\DIRECTORY_SEPARATOR);',
  214. ['modernize_stripos' => true],
  215. ];
  216. yield [
  217. '<?php !str_starts_with(strtolower($file.\DIRECTORY_SEPARATOR), strtolower($needle.\DIRECTORY_SEPARATOR));',
  218. '<?php 0 !== stripos($file.\DIRECTORY_SEPARATOR, $needle.\DIRECTORY_SEPARATOR);',
  219. ['modernize_stripos' => true],
  220. ];
  221. yield [
  222. '<?php !str_starts_with(strtolower($file.\DIRECTORY_SEPARATOR), strtolower($needle));',
  223. '<?php 0 !== stripos($file.\DIRECTORY_SEPARATOR, $needle);',
  224. ['modernize_stripos' => true],
  225. ];
  226. yield [
  227. '<?php str_starts_with(strtolower(/** comm */ $file.\DIRECTORY_SEPARATOR /* comm2 */), /* comm3 */ strtolower($needle // comm4
  228. ));',
  229. '<?php 0 === stripos(/** comm */ $file.\DIRECTORY_SEPARATOR /* comm2 */, /* comm3 */ $needle // comm4
  230. );',
  231. ['modernize_stripos' => true],
  232. ];
  233. // do not fix
  234. yield [
  235. '<?php
  236. $x = 1;
  237. $x = "strpos";
  238. // if (false === strpos($haystack12, $needle)) {}
  239. /** if (false === strpos($haystack13, $needle)) {} */
  240. ',
  241. ];
  242. yield [
  243. '<?php
  244. $x = 1;
  245. $x = "stripos";
  246. // if (false === strpos($haystack12, $needle)) {}
  247. /** if (false === strpos($haystack13, $needle)) {} */
  248. ',
  249. ];
  250. yield 'disabled stripos (default)' => [
  251. '<?php if (stripos($haystack3, $needle) === 0) {}',
  252. ];
  253. yield 'disabled stripos' => [
  254. '<?php if (stripos($haystack3, $needle) === 0) {}',
  255. null,
  256. ['modernize_stripos' => false],
  257. ];
  258. yield 'different namespace' => [
  259. '<?php if (a\strpos($haystack14, $needle) === 0) {}',
  260. ];
  261. yield 'case insensitive different namespace' => [
  262. '<?php if (a\stripos($haystack14, $needle) === 0) {}',
  263. ];
  264. yield 'different namespace with yoda' => [
  265. '<?php if (0 === a\strpos($haystack14, $needle)) {}',
  266. ];
  267. yield 'case insensitive different namespace with yoda' => [
  268. '<?php if (0 === a\stripos($haystack14, $needle)) {}',
  269. ];
  270. yield 'non condition (hardcoded)' => [
  271. '<?php $x = strpos(\'foo\', \'f\');',
  272. ];
  273. yield 'case insensitive non condition (hardcoded)' => [
  274. '<?php $x = stripos(\'foo\', \'f\');',
  275. ];
  276. yield 'non condition' => [
  277. '<?php $x = strpos($haystack15, $needle) ?>',
  278. ];
  279. yield 'case insensitive non condition' => [
  280. '<?php $x = stripos($haystack15, $needle) ?>',
  281. ];
  282. yield 'none zero int' => [
  283. '<?php if (1 !== strpos($haystack16, $needle)) {}',
  284. ];
  285. yield 'case insensitive none zero int' => [
  286. '<?php if (1 !== stripos($haystack16, $needle)) {}',
  287. ];
  288. yield 'greater condition' => [
  289. '<?php if (strpos($haystack17, $needle) > 0) {}',
  290. ];
  291. yield 'case insensitive greater condition' => [
  292. '<?php if (stripos($haystack17, $needle) > 0) {}',
  293. ];
  294. yield 'lesser condition' => [
  295. '<?php if (0 < strpos($haystack18, $needle)) {}',
  296. ];
  297. yield 'case insensitive lesser condition' => [
  298. '<?php if (0 < stripos($haystack18, $needle)) {}',
  299. ];
  300. yield 'no argument' => [
  301. '<?php $z = strpos();',
  302. ];
  303. yield 'case insensitive no argument' => [
  304. '<?php $z = stripos();',
  305. ];
  306. yield 'one argument' => [
  307. '<?php if (0 === strpos($haystack1)) {}',
  308. ];
  309. yield 'case insensitive one argument' => [
  310. '<?php if (0 === stripos($haystack1)) {}',
  311. ];
  312. yield '3 arguments' => [
  313. '<?php if (0 === strpos($haystack1, $a, $b)) {}',
  314. ];
  315. yield 'case insensitive 3 arguments' => [
  316. '<?php if (0 === stripos($haystack1, $a, $b)) {}',
  317. ];
  318. yield 'higher precedence 1' => [
  319. '<?php if (4 + 0 !== strpos($haystack9, $needle)) {}',
  320. ];
  321. yield 'case insensitive higher precedence 1' => [
  322. '<?php if (4 + 0 !== stripos($haystack9, $needle)) {}',
  323. ];
  324. yield 'higher precedence 2' => [
  325. '<?php if (!false === strpos($haystack, $needle)) {}',
  326. ];
  327. yield 'case insensitive higher precedence 2' => [
  328. '<?php if (!false === stripos($haystack, $needle)) {}',
  329. ];
  330. yield 'higher precedence 3' => [
  331. '<?php $a = strpos($haystack, $needle) === 0 + 1;',
  332. ];
  333. yield 'case insensitive higher precedence 3' => [
  334. '<?php $a = stripos($haystack, $needle) === 0 + 1;',
  335. ];
  336. yield 'higher precedence 4' => [
  337. '<?php $a = strpos($haystack, $needle) === 0 > $b;',
  338. ];
  339. yield 'case insensitive higher precedence 4' => [
  340. '<?php $a = stripos($haystack, $needle) === 0 > $b;',
  341. ];
  342. }
  343. }