TernaryOperatorSpacesFixerTest.php 5.8 KB

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