NoSpacesInsideParenthesisFixerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. return [
  54. [
  55. '<?php foo();',
  56. '<?php foo( );',
  57. ],
  58. [
  59. '<?php
  60. if (true) {
  61. // if body
  62. }',
  63. '<?php
  64. if ( true ) {
  65. // if body
  66. }',
  67. ],
  68. [
  69. '<?php
  70. if (true) {
  71. // if body
  72. }',
  73. '<?php
  74. if ( true ) {
  75. // if body
  76. }',
  77. ],
  78. [
  79. '<?php
  80. function foo($bar, $baz)
  81. {
  82. // function body
  83. }',
  84. '<?php
  85. function foo( $bar, $baz )
  86. {
  87. // function body
  88. }',
  89. ],
  90. [
  91. '<?php
  92. $foo->bar($arg1, $arg2);',
  93. '<?php
  94. $foo->bar( $arg1, $arg2 );',
  95. ],
  96. [
  97. '<?php
  98. $var = array( 1, 2, 3 );
  99. ',
  100. ],
  101. [
  102. '<?php
  103. $var = [ 1, 2, 3 ];
  104. ',
  105. ],
  106. // list call with trailing comma - need to leave alone
  107. [
  108. '<?php list($path, $mode, ) = foo();',
  109. ],
  110. [
  111. '<?php list($path, $mode,) = foo();',
  112. ],
  113. [
  114. '<?php
  115. $a = $b->test( // do not remove space
  116. $e // between `(` and `)`
  117. // and this comment
  118. );',
  119. ],
  120. ];
  121. }
  122. /**
  123. * @dataProvider provideFix80Cases
  124. *
  125. * @requires PHP 8.0
  126. */
  127. public function testFix80(string $expected, string $input): void
  128. {
  129. $this->doTest($expected, $input);
  130. }
  131. public static function provideFix80Cases(): iterable
  132. {
  133. yield [
  134. '<?php function foo(mixed $a){}',
  135. '<?php function foo( mixed $a ){}',
  136. ];
  137. }
  138. /**
  139. * @dataProvider provideFix81Cases
  140. *
  141. * @requires PHP 8.1
  142. */
  143. public function testFix81(string $expected, string $input): void
  144. {
  145. $this->doTest($expected, $input);
  146. }
  147. public static function provideFix81Cases(): iterable
  148. {
  149. yield 'first callable class' => [
  150. '<?php $a = strlen(...);',
  151. '<?php $a = strlen( ... );',
  152. ];
  153. }
  154. }