NoSpacesInsideParenthesisFixerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Marc Aubé
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Whitespace\NoSpacesInsideParenthesisFixer
  20. */
  21. final class NoSpacesInsideParenthesisFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public function testLeaveNewLinesAlone(): void
  31. {
  32. $expected = <<<'EOF'
  33. <?php
  34. class Foo
  35. {
  36. private function bar()
  37. {
  38. if (foo(
  39. 'foo' ,
  40. 'bar' ,
  41. [1, 2, 3],
  42. 'baz' // a comment just to mix things up
  43. )) {
  44. return 1;
  45. };
  46. }
  47. }
  48. EOF;
  49. $this->doTest($expected);
  50. }
  51. public static function provideFixCases(): iterable
  52. {
  53. yield [
  54. '<?php foo();',
  55. '<?php foo( );',
  56. ];
  57. yield [
  58. '<?php
  59. if (true) {
  60. // if body
  61. }',
  62. '<?php
  63. if ( true ) {
  64. // if body
  65. }',
  66. ];
  67. yield [
  68. '<?php
  69. if (true) {
  70. // if body
  71. }',
  72. '<?php
  73. if ( true ) {
  74. // if body
  75. }',
  76. ];
  77. yield [
  78. '<?php
  79. function foo($bar, $baz)
  80. {
  81. // function body
  82. }',
  83. '<?php
  84. function foo( $bar, $baz )
  85. {
  86. // function body
  87. }',
  88. ];
  89. yield [
  90. '<?php
  91. $foo->bar($arg1, $arg2);',
  92. '<?php
  93. $foo->bar( $arg1, $arg2 );',
  94. ];
  95. yield [
  96. '<?php
  97. $var = array( 1, 2, 3 );
  98. ',
  99. ];
  100. yield [
  101. '<?php
  102. $var = [ 1, 2, 3 ];
  103. ',
  104. ];
  105. // list call with trailing comma - need to leave alone
  106. yield [
  107. '<?php list($path, $mode, ) = foo();',
  108. ];
  109. yield [
  110. '<?php list($path, $mode,) = foo();',
  111. ];
  112. yield [
  113. '<?php
  114. $a = $b->test( // do not remove space
  115. $e // between `(` and `)`
  116. // and this comment
  117. );',
  118. ];
  119. }
  120. /**
  121. * @dataProvider provideFix80Cases
  122. *
  123. * @requires PHP 8.0
  124. */
  125. public function testFix80(string $expected, string $input): void
  126. {
  127. $this->doTest($expected, $input);
  128. }
  129. public static function provideFix80Cases(): iterable
  130. {
  131. yield [
  132. '<?php function foo(mixed $a){}',
  133. '<?php function foo( mixed $a ){}',
  134. ];
  135. }
  136. /**
  137. * @dataProvider provideFix81Cases
  138. *
  139. * @requires PHP 8.1
  140. */
  141. public function testFix81(string $expected, string $input): void
  142. {
  143. $this->doTest($expected, $input);
  144. }
  145. public static function provideFix81Cases(): iterable
  146. {
  147. yield 'first callable class' => [
  148. '<?php $a = strlen(...);',
  149. '<?php $a = strlen( ... );',
  150. ];
  151. }
  152. }