TernaryOperatorSpacesFixerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Operator\TernaryOperatorSpacesFixer
  20. */
  21. final class TernaryOperatorSpacesFixerTest 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 static function provideFixCases(): iterable
  31. {
  32. yield 'handle goto labels 1' => [
  33. '<?php
  34. beginning:
  35. echo $guard ? 1 : 2;',
  36. '<?php
  37. beginning:
  38. echo $guard?1:2;',
  39. ];
  40. yield 'handle goto labels 2' => [
  41. '<?php
  42. function A(){}
  43. beginning:
  44. echo $guard ? 1 : 2;',
  45. '<?php
  46. function A(){}
  47. beginning:
  48. echo $guard?1:2;',
  49. ];
  50. yield 'handle goto labels 3' => [
  51. '<?php
  52. ;
  53. beginning:
  54. echo $guard ? 1 : 2;',
  55. '<?php
  56. ;
  57. beginning:
  58. echo $guard?1:2;',
  59. ];
  60. yield 'handle goto labels 4' => [
  61. '<?php
  62. {
  63. beginning:
  64. echo $guard ? 1 : 2;}',
  65. '<?php
  66. {
  67. beginning:
  68. echo $guard?1:2;}',
  69. ];
  70. yield [
  71. '<?php $a = $a ? 1 : 0;',
  72. '<?php $a = $a ? 1 : 0;',
  73. ];
  74. yield [
  75. '<?php $a = $a ?
  76. #
  77. : $b;',
  78. ];
  79. yield [
  80. '<?php $a = $a#
  81. ? '.'
  82. #
  83. 1 : 0;',
  84. ];
  85. yield [
  86. '<?php $val = (1===1) ? true : false;',
  87. '<?php $val = (1===1)?true:false;',
  88. ];
  89. yield [
  90. '<?php $val = 1===1 ? true : false;',
  91. '<?php $val = 1===1?true:false;',
  92. ];
  93. yield [
  94. '<?php
  95. $a = $b ? 2 : ($bc ? 2 : 3);
  96. $a = $bc ? 2 : 3;',
  97. '<?php
  98. $a = $b ? 2 : ($bc?2:3);
  99. $a = $bc?2:3;',
  100. ];
  101. yield [
  102. '<?php $config = $config ?: new Config();',
  103. '<?php $config = $config ? : new Config();',
  104. ];
  105. yield [
  106. '<?php
  107. $a = $b ? (
  108. $c + 1
  109. ) : (
  110. $d + 1
  111. );',
  112. ];
  113. yield [
  114. '<?php
  115. $a = $b
  116. ? $c
  117. : $d;',
  118. '<?php
  119. $a = $b
  120. ?$c
  121. :$d;',
  122. ];
  123. yield [
  124. '<?php
  125. $a = $b //
  126. ? $c /**/
  127. : $d;',
  128. '<?php
  129. $a = $b //
  130. ?$c /**/
  131. :$d;',
  132. ];
  133. yield [
  134. '<?php
  135. $a = ($b
  136. ? $c
  137. : ($d
  138. ? $e
  139. : $f
  140. )
  141. );',
  142. ];
  143. yield [
  144. '<?php
  145. $a = ($b
  146. ? ($c1 ? $c2 : ($c3a ?: $c3b))
  147. : ($d1 ? $d2 : $d3)
  148. );',
  149. '<?php
  150. $a = ($b
  151. ? ($c1?$c2:($c3a? :$c3b))
  152. : ($d1?$d2:$d3)
  153. );',
  154. ];
  155. yield [
  156. '<?php
  157. $foo = $isBar ? 1 : 2;
  158. switch ($foo) {
  159. case 1: return 3;
  160. case 2: return 4;
  161. }
  162. ',
  163. '<?php
  164. $foo = $isBar? 1 : 2;
  165. switch ($foo) {
  166. case 1: return 3;
  167. case 2: return 4;
  168. }
  169. ',
  170. ];
  171. yield [
  172. '<?php
  173. return $isBar ? array_sum(array_map(function ($x) { switch ($x) { case 1: return $y ? 2 : 3; case 4: return 5; } }, [1, 2, 3])) : 128;
  174. ',
  175. '<?php
  176. return $isBar?array_sum(array_map(function ($x) { switch ($x) { case 1: return $y? 2 : 3; case 4: return 5; } }, [1, 2, 3])):128;
  177. ',
  178. ];
  179. yield [
  180. '<?php
  181. declare(ticks=1):enddeclare;
  182. for ($i = 0; $i < 100; $i++): echo "."; endfor;
  183. foreach ($foo as $bar): $i++; endforeach;
  184. if ($x === 1): echo "One"; elseif ($x === 2): echo "Two"; else: echo "Three"; endif;
  185. switch (true): default: return 0; endswitch;
  186. while ($i > 10): $i--; endwhile;
  187. /* ternary operator to make the file a candidate for fixing */ true ? 1 : 0;
  188. ',
  189. ];
  190. }
  191. /**
  192. * @dataProvider provideFix80Cases
  193. *
  194. * @requires PHP 8.0
  195. */
  196. public function testFix80(string $expected, ?string $input = null): void
  197. {
  198. $this->doTest($expected, $input);
  199. }
  200. public static function provideFix80Cases(): iterable
  201. {
  202. yield 'nullable types in constructor property promotion' => [
  203. '<?php
  204. class Foo
  205. {
  206. public function __construct(
  207. private ?string $foo = null,
  208. protected ?string $bar = null,
  209. public ?string $xyz = null,
  210. ) {
  211. /* ternary operator to make the file a candidate for fixing */ true ? 1 : 0;
  212. }
  213. }',
  214. ];
  215. }
  216. /**
  217. * @dataProvider provideFix81Cases
  218. *
  219. * @requires PHP 8.1
  220. */
  221. public function testFix81(string $expected, ?string $input = null): void
  222. {
  223. $this->doTest($expected, $input);
  224. }
  225. public static function provideFix81Cases(): iterable
  226. {
  227. yield [
  228. <<<'PHP'
  229. <?php
  230. enum TaskType: int
  231. {
  232. public function foo(bool $value): string
  233. {
  234. return $value ? 'foo' : 'bar';
  235. }
  236. }
  237. PHP,
  238. ];
  239. }
  240. }