NoUselessSprintfFixerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\FunctionNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer
  18. */
  19. final class NoUselessSprintfFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @dataProvider provideFixCases
  23. */
  24. public function testFix(string $expected, ?string $input = null): void
  25. {
  26. $this->doTest($expected, $input);
  27. }
  28. public function provideFixCases()
  29. {
  30. yield 'simple' => [
  31. '<?php echo "bar";',
  32. '<?php echo sprintf("bar");',
  33. ];
  34. yield 'simple II' => [
  35. "<?php echo 'bar' ?>",
  36. "<?php echo sprintf('bar') ?>",
  37. ];
  38. yield 'simple III' => [
  39. '<?php echo $bar;',
  40. '<?php echo sprintf($bar);',
  41. ];
  42. yield 'simple IV' => [
  43. '<?php echo 1;',
  44. '<?php echo sprintf(1);',
  45. ];
  46. yield 'minimal' => [
  47. '<?php $foo;',
  48. '<?php sprintf($foo);',
  49. ];
  50. yield 'case insensitive' => [
  51. '<?php echo "bar";',
  52. '<?php echo SPRINTF("bar");',
  53. ];
  54. yield 'nested' => [
  55. '<?php echo /* test */"bar";',
  56. '<?php echo sprintf(sprintf(sprintf(/* test */sprintf(sprintf(sprintf("bar"))))));',
  57. ];
  58. yield [
  59. '<?php namespace Foo {
  60. function sprintf($foo) {
  61. echo $foo;
  62. }
  63. }',
  64. ];
  65. yield [
  66. '<?php namespace Foo;
  67. function sprintf($foo) {
  68. echo $foo;
  69. }
  70. ',
  71. ];
  72. yield [
  73. '<?php
  74. echo \Foo\sprintf("abc");
  75. echo $bar->sprintf($foo);
  76. echo Bar::sprintf($foo);
  77. echo sprint(...$foo);
  78. echo sprint("%d", 1);
  79. echo sprint("%d%d%d", 1, 2, 3);
  80. echo sprint();
  81. ',
  82. ];
  83. if (\PHP_VERSION_ID >= 70000) {
  84. yield [
  85. '<?php echo sprint[2]("foo");
  86. ',
  87. ];
  88. }
  89. if (\PHP_VERSION_ID > 70300) {
  90. yield 'trailing comma' => [
  91. '<?php echo "bar";',
  92. '<?php echo sprintf("bar",);',
  93. ];
  94. }
  95. if (\PHP_VERSION_ID < 80000) {
  96. yield [
  97. '<?php echo "bar";',
  98. '<?php echo \ sprintf("bar");',
  99. ];
  100. yield [
  101. '<?php echo A /* 1 */ \ B \ sprintf("bar");',
  102. ];
  103. }
  104. }
  105. }