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. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php foo();',
  39. '<?php foo( );',
  40. ];
  41. yield [
  42. '<?php
  43. if (true) {
  44. // if body
  45. }',
  46. '<?php
  47. if ( true ) {
  48. // if body
  49. }',
  50. ];
  51. yield [
  52. '<?php
  53. if (true) {
  54. // if body
  55. }',
  56. '<?php
  57. if ( true ) {
  58. // if body
  59. }',
  60. ];
  61. yield [
  62. '<?php
  63. function foo($bar, $baz)
  64. {
  65. // function body
  66. }',
  67. '<?php
  68. function foo( $bar, $baz )
  69. {
  70. // function body
  71. }',
  72. ];
  73. yield [
  74. '<?php
  75. $foo->bar($arg1, $arg2);',
  76. '<?php
  77. $foo->bar( $arg1, $arg2 );',
  78. ];
  79. yield [
  80. '<?php
  81. $var = array( 1, 2, 3 );
  82. ',
  83. ];
  84. yield [
  85. '<?php
  86. $var = [ 1, 2, 3 ];
  87. ',
  88. ];
  89. // list call with trailing comma - need to leave alone
  90. yield [
  91. '<?php list($path, $mode, ) = foo();',
  92. ];
  93. yield [
  94. '<?php list($path, $mode,) = foo();',
  95. ];
  96. yield [
  97. '<?php
  98. $a = $b->test( // do not remove space
  99. $e // between `(` and `)`
  100. // and this comment
  101. );',
  102. ];
  103. }
  104. public function testLeaveNewLinesAlone(): void
  105. {
  106. $expected = <<<'EOF'
  107. <?php
  108. class Foo
  109. {
  110. private function bar()
  111. {
  112. if (foo(
  113. 'foo' ,
  114. 'bar' ,
  115. [1, 2, 3],
  116. 'baz' // a comment just to mix things up
  117. )) {
  118. return 1;
  119. };
  120. }
  121. }
  122. EOF;
  123. $this->doTest($expected);
  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. }