ErrorSuppressionFixerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\LanguageConstruct;
  13. use PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Jules Pietri <jules@heahprod.com>
  17. * @author Kuba Werłos <werlos@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer
  22. */
  23. final class ErrorSuppressionFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param array<string, mixed> $config
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $config = []): void
  31. {
  32. $this->fixer->configure($config);
  33. $this->doTest($expected, $input);
  34. }
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php trigger_error("This is not a deprecation warning."); @f(); ?>',
  39. ];
  40. yield [
  41. '<?php trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
  42. ];
  43. yield [
  44. '<?php A\B\trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  45. ];
  46. yield [
  47. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  48. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  49. ];
  50. yield [
  51. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  52. null,
  53. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => false],
  54. ];
  55. yield [
  56. '<?php @\trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  57. '<?php \trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  58. ];
  59. yield [
  60. '<?php echo "test";@trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  61. '<?php echo "test";trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  62. ];
  63. yield [
  64. '<?php //
  65. @Trigger_Error/**/("This is a deprecation warning.", E_USER_DEPRECATED/***/); ?>',
  66. '<?php //
  67. Trigger_Error/**/("This is a deprecation warning.", E_USER_DEPRECATED/***/); ?>',
  68. ];
  69. yield [
  70. '<?php new trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  71. ];
  72. yield [
  73. '<?php new \trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  74. ];
  75. yield [
  76. '<?php $foo->trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  77. ];
  78. yield [
  79. '<?php Foo::trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  80. ];
  81. yield [
  82. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); mkdir("dir"); ?>',
  83. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
  84. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
  85. ];
  86. yield [
  87. '<?php $foo->isBar(); ?>',
  88. '<?php @$foo->isBar(); ?>',
  89. [ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
  90. ];
  91. yield [
  92. '<?php Foo::isBar(); ?>',
  93. '<?php @Foo::isBar(); ?>',
  94. [ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
  95. ];
  96. yield [
  97. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
  98. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
  99. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['mkdir']],
  100. ];
  101. yield [
  102. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); unlink($path); ?>',
  103. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); @unlink($path); ?>',
  104. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['mkdir']],
  105. ];
  106. yield [
  107. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
  108. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
  109. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['trigger_error']],
  110. ];
  111. yield [
  112. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED, );',
  113. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED, );',
  114. ];
  115. }
  116. /**
  117. * @dataProvider provideFixPre80Cases
  118. *
  119. * @requires PHP <8.0
  120. */
  121. public function testFixPre80(string $expected, string $input = null): void
  122. {
  123. $this->doTest($expected, $input);
  124. }
  125. public static function provideFixPre80Cases(): iterable
  126. {
  127. yield [
  128. '<?php \A\B/* */\trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  129. ];
  130. }
  131. /**
  132. * @dataProvider provideFix81Cases
  133. *
  134. * @requires PHP 8.1
  135. */
  136. public function testFix81(string $expected, string $input = null): void
  137. {
  138. $this->doTest($expected, $input);
  139. }
  140. public static function provideFix81Cases(): iterable
  141. {
  142. yield [
  143. '<?php $a = trigger_error(...);',
  144. ];
  145. }
  146. }