NoSuperfluousElseifFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\ControlStructure;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\AbstractNoUselessElseFixer
  18. * @covers \PhpCsFixer\Fixer\ControlStructure\NoSuperfluousElseifFixer
  19. */
  20. final class NoSuperfluousElseifFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @dataProvider provideFixCases
  24. */
  25. public function testFix(string $expected, ?string $input = null): void
  26. {
  27. $this->doTest($expected, $input);
  28. }
  29. public function provideFixCases(): array
  30. {
  31. return [
  32. [
  33. '<?php
  34. if ($some) { return 1; } if ($a == 6){ $test = false; } //',
  35. '<?php
  36. if ($some) { return 1; } elseif ($a == 6){ $test = false; } //',
  37. ],
  38. [
  39. '<?php
  40. if ($foo) {
  41. return 1;
  42. }
  43. if ($bar) {
  44. return 2;
  45. }
  46. if ($baz) {
  47. return 3;
  48. } else {
  49. return 4;
  50. }',
  51. '<?php
  52. if ($foo) {
  53. return 1;
  54. } elseif ($bar) {
  55. return 2;
  56. } else if ($baz) {
  57. return 3;
  58. } else {
  59. return 4;
  60. }',
  61. ],
  62. [
  63. '<?php
  64. if ($foo)
  65. return 1;
  66. if ($bar)
  67. echo \'bar\';
  68. else {
  69. return 3;
  70. }',
  71. '<?php
  72. if ($foo)
  73. return 1;
  74. elseif ($bar)
  75. echo \'bar\';
  76. else {
  77. return 3;
  78. }',
  79. ],
  80. [
  81. '<?php
  82. if ($foo)
  83. ?><?php
  84. elseif ($bar)
  85. ?><?php
  86. else {
  87. ?><?php
  88. }',
  89. ],
  90. [
  91. '<?php
  92. if ($foo) {
  93. ?><?php
  94. return;
  95. }
  96. if ($bar)
  97. ?><?php
  98. else {
  99. ?><?php
  100. }',
  101. '<?php
  102. if ($foo) {
  103. ?><?php
  104. return;
  105. } elseif ($bar)
  106. ?><?php
  107. else {
  108. ?><?php
  109. }',
  110. ],
  111. [
  112. '<?php
  113. while (1) {
  114. if (2) {
  115. if (3) {
  116. if (4) {
  117. die;
  118. }
  119. if (5) {
  120. exit;
  121. } else {#foo
  122. throw new \Exception();
  123. }
  124. '.'
  125. continue;
  126. }
  127. if (6) {
  128. return null;
  129. } else {
  130. return 1;
  131. }
  132. '.'
  133. break;
  134. }
  135. /* bar */if (7)
  136. return 2 + 3;
  137. else {# baz
  138. die(\'foo\');
  139. }
  140. }',
  141. '<?php
  142. while (1) {
  143. if (2) {
  144. if (3) {
  145. if (4) {
  146. die;
  147. } elseif (5) {
  148. exit;
  149. } else {#foo
  150. throw new \Exception();
  151. }
  152. '.'
  153. continue;
  154. } else if (6) {
  155. return null;
  156. } else {
  157. return 1;
  158. }
  159. '.'
  160. break;
  161. } else/* bar */if (7)
  162. return 2 + 3;
  163. else {# baz
  164. die(\'foo\');
  165. }
  166. }',
  167. ],
  168. [
  169. '<?php
  170. if ($a === false)
  171. {
  172. if ($v) { $ret = "foo"; }
  173. elseif($a)
  174. die;
  175. }
  176. elseif($a)
  177. $ret .= $value;
  178. return $ret;',
  179. ],
  180. [
  181. '<?php
  182. if ($a)
  183. echo 1;
  184. else if ($b)
  185. die;
  186. else {
  187. echo 2;
  188. }',
  189. ],
  190. [
  191. '<?php
  192. if ($a) {
  193. echo 1;
  194. } else if ($b)
  195. die;
  196. else {
  197. echo 2;
  198. }',
  199. ],
  200. [
  201. '<?php
  202. if ($a) {
  203. echo 1;
  204. } else if ($b) {
  205. die;
  206. } else {
  207. echo 2;
  208. }',
  209. ],
  210. [
  211. '<?php
  212. if ($foo) {
  213. return 1;
  214. }
  215. if ($bar) {
  216. return 2;
  217. }
  218. if ($baz) {
  219. throw new class extends Exception{};
  220. } else {
  221. return 4;
  222. }',
  223. '<?php
  224. if ($foo) {
  225. return 1;
  226. } elseif ($bar) {
  227. return 2;
  228. } else if ($baz) {
  229. throw new class extends Exception{};
  230. } else {
  231. return 4;
  232. }',
  233. ],
  234. ];
  235. }
  236. /**
  237. * @dataProvider provideFix80Cases
  238. * @requires PHP 8.0
  239. */
  240. public function testFix80(string $expected): void
  241. {
  242. $this->doTest($expected);
  243. }
  244. public function provideFix80Cases(): \Generator
  245. {
  246. yield [
  247. '<?php
  248. if ($foo) {
  249. $a = $bar ?? throw new \Exception();
  250. } elseif ($bar) {
  251. echo 1;
  252. }
  253. ',
  254. ];
  255. }
  256. }