NoSuperfluousElseifFixerTest.php 7.2 KB

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