TernaryOperatorSpacesFixerTest.php 5.7 KB

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