MbStrFunctionsFixerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\AbstractFunctionReferenceFixer
  20. * @covers \PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer>
  23. */
  24. final class MbStrFunctionsFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. yield ['<?php $x = "strlen";'];
  39. yield ['<?php $x = Foo::strlen("bar");'];
  40. yield ['<?php $x = new strlen("bar");'];
  41. yield ['<?php $x = new \strlen("bar");'];
  42. yield ['<?php $x = new Foo\strlen("bar");'];
  43. yield ['<?php $x = Foo\strlen("bar");'];
  44. yield ['<?php $x = strlen::call("bar");'];
  45. yield ['<?php $x = $foo->strlen("bar");'];
  46. yield ['<?php $x = strlen();']; // number of arguments mismatch
  47. yield ['<?php $x = strlen($a, $b);']; // number of arguments mismatch
  48. yield ['<?php $x = mb_strlen("bar");', '<?php $x = strlen("bar");'];
  49. yield ['<?php $x = \mb_strlen("bar");', '<?php $x = \strlen("bar");'];
  50. yield ['<?php $x = mb_strtolower(mb_strstr("bar", "a"));', '<?php $x = strtolower(strstr("bar", "a"));'];
  51. yield ['<?php $x = mb_strtolower( \mb_strstr ("bar", "a"));', '<?php $x = strtolower( \strstr ("bar", "a"));'];
  52. yield ['<?php $x = mb_substr("bar", 2, 1);', '<?php $x = substr("bar", 2, 1);'];
  53. yield [
  54. '<?php
  55. interface Test
  56. {
  57. public function &strlen($a);
  58. public function strtolower($a);
  59. }',
  60. ];
  61. yield [
  62. '<?php $a = mb_str_split($a);',
  63. '<?php $a = str_split($a);',
  64. ];
  65. }
  66. /**
  67. * @requires PHP 8.3
  68. *
  69. * @dataProvider provideFix83Cases
  70. */
  71. public function testFix83(string $expected, ?string $input = null): void
  72. {
  73. $this->doTest($expected, $input);
  74. }
  75. /**
  76. * @return iterable<string, array{string, null|string}>
  77. */
  78. public static function provideFix83Cases(): iterable
  79. {
  80. yield 'mb_str_pad()' => [
  81. '<?php $x = mb_str_pad("bar", 2, "0", STR_PAD_LEFT);',
  82. '<?php $x = str_pad("bar", 2, "0", STR_PAD_LEFT);',
  83. ];
  84. }
  85. /**
  86. * @requires PHP 8.4
  87. *
  88. * @dataProvider provideFix84Cases
  89. */
  90. public function testFix84(string $expected, ?string $input = null): void
  91. {
  92. $this->doTest($expected, $input);
  93. }
  94. /**
  95. * @return iterable<string, array{string, null|string}>
  96. */
  97. public static function provideFix84Cases(): iterable
  98. {
  99. yield 'mb_trim 1 argument' => [
  100. '<?php $x = mb_trim(" foo ");',
  101. '<?php $x = trim(" foo ");',
  102. ];
  103. yield 'mb_trim 2 arguments' => [
  104. '<?php $x = mb_trim("____foo__", "_");',
  105. '<?php $x = trim("____foo__", "_");',
  106. ];
  107. yield 'ltrim' => [
  108. '<?php $x = mb_ltrim(" foo ");',
  109. '<?php $x = ltrim(" foo ");',
  110. ];
  111. yield 'rtrim' => [
  112. '<?php $x = mb_rtrim(" foo ");',
  113. '<?php $x = rtrim(" foo ");',
  114. ];
  115. }
  116. }