MbStrFunctionsFixerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer>
  22. */
  23. final class MbStrFunctionsFixerTest 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 ['<?php $x = "strlen";'];
  38. yield ['<?php $x = Foo::strlen("bar");'];
  39. yield ['<?php $x = new strlen("bar");'];
  40. yield ['<?php $x = new \strlen("bar");'];
  41. yield ['<?php $x = new Foo\strlen("bar");'];
  42. yield ['<?php $x = Foo\strlen("bar");'];
  43. yield ['<?php $x = strlen::call("bar");'];
  44. yield ['<?php $x = $foo->strlen("bar");'];
  45. yield ['<?php $x = strlen();']; // number of arguments mismatch
  46. yield ['<?php $x = strlen($a, $b);']; // number of arguments mismatch
  47. yield ['<?php $x = mb_strlen("bar");', '<?php $x = strlen("bar");'];
  48. yield ['<?php $x = \mb_strlen("bar");', '<?php $x = \strlen("bar");'];
  49. yield ['<?php $x = mb_strtolower(mb_strstr("bar", "a"));', '<?php $x = strtolower(strstr("bar", "a"));'];
  50. yield ['<?php $x = mb_strtolower( \mb_strstr ("bar", "a"));', '<?php $x = strtolower( \strstr ("bar", "a"));'];
  51. yield ['<?php $x = mb_substr("bar", 2, 1);', '<?php $x = substr("bar", 2, 1);'];
  52. yield [
  53. '<?php
  54. interface Test
  55. {
  56. public function &strlen($a);
  57. public function strtolower($a);
  58. }',
  59. ];
  60. yield [
  61. '<?php $a = mb_str_split($a);',
  62. '<?php $a = str_split($a);',
  63. ];
  64. yield [
  65. <<<'PHP'
  66. <?php
  67. namespace Foo;
  68. use function Bar\strlen;
  69. use function mb_strtolower;
  70. use function mb_strtoupper;
  71. return strlen($x) > 10 ? mb_strtolower($x) : mb_strtoupper($x);
  72. PHP,
  73. <<<'PHP'
  74. <?php
  75. namespace Foo;
  76. use function Bar\strlen;
  77. use function strtolower;
  78. use function strtoupper;
  79. return strlen($x) > 10 ? strtolower($x) : strtoupper($x);
  80. PHP,
  81. ];
  82. }
  83. /**
  84. * @requires PHP 8.3
  85. *
  86. * @dataProvider provideFix83Cases
  87. */
  88. public function testFix83(string $expected, ?string $input = null): void
  89. {
  90. $this->doTest($expected, $input);
  91. }
  92. /**
  93. * @return iterable<string, array{string, null|string}>
  94. */
  95. public static function provideFix83Cases(): iterable
  96. {
  97. yield 'mb_str_pad()' => [
  98. '<?php $x = mb_str_pad("bar", 2, "0", STR_PAD_LEFT);',
  99. '<?php $x = str_pad("bar", 2, "0", STR_PAD_LEFT);',
  100. ];
  101. }
  102. /**
  103. * @requires PHP 8.4
  104. *
  105. * @dataProvider provideFix84Cases
  106. */
  107. public function testFix84(string $expected, ?string $input = null): void
  108. {
  109. $this->doTest($expected, $input);
  110. }
  111. /**
  112. * @return iterable<string, array{string, null|string}>
  113. */
  114. public static function provideFix84Cases(): iterable
  115. {
  116. yield 'mb_trim 1 argument' => [
  117. '<?php $x = mb_trim(" foo ");',
  118. '<?php $x = trim(" foo ");',
  119. ];
  120. yield 'mb_trim 2 arguments' => [
  121. '<?php $x = mb_trim("____foo__", "_");',
  122. '<?php $x = trim("____foo__", "_");',
  123. ];
  124. yield 'ltrim' => [
  125. '<?php $x = mb_ltrim(" foo ");',
  126. '<?php $x = ltrim(" foo ");',
  127. ];
  128. yield 'rtrim' => [
  129. '<?php $x = mb_rtrim(" foo ");',
  130. '<?php $x = rtrim(" foo ");',
  131. ];
  132. }
  133. }