NoUselessNullsafeOperatorFixerTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Operator;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @requires PHP 8.0
  18. *
  19. * @covers \PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer>
  22. */
  23. final class NoUselessNullsafeOperatorFixerTest 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<string, array{string, string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'simple case + comment' => [
  38. '<?php $a = new class extends foo {
  39. public function bar() {
  40. $this->g();
  41. // $this?->g();
  42. }
  43. };',
  44. '<?php $a = new class extends foo {
  45. public function bar() {
  46. $this?->g();
  47. // $this?->g();
  48. }
  49. };',
  50. ];
  51. yield 'multiple casing cases + comment + no candidate' => [
  52. '<?php $a = new class extends foo {
  53. public function bar() {
  54. return $THIS /*1*/ -> g().$THis->g().$this->do()?->o();
  55. }
  56. };',
  57. '<?php $a = new class extends foo {
  58. public function bar() {
  59. return $THIS /*1*/ ?-> g().$THis?->g().$this->do()?->o();
  60. }
  61. };',
  62. ];
  63. }
  64. }