NoSpacesInsideParenthesisFixerTest.php 3.6 KB

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