NoUselessReturnFixerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\ReturnNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\ReturnNotation\NoUselessReturnFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ReturnNotation\NoUselessReturnFixer>
  20. */
  21. final class NoUselessReturnFixerTest 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. /**
  31. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php
  37. function bar($baz)
  38. {
  39. if ($baz)
  40. return $this->baz();
  41. else
  42. return;
  43. }',
  44. ];
  45. yield [
  46. '<?php
  47. function bar($baz)
  48. {
  49. if ($baz)
  50. return $this->baz();
  51. elseif($a)
  52. return;
  53. }',
  54. ];
  55. yield [
  56. '<?php
  57. function bar($baz)
  58. {
  59. if ($baz)
  60. return $this->baz();
  61. else if($a)
  62. return;
  63. }',
  64. ];
  65. yield [
  66. '<?php
  67. function bar($baz)
  68. {
  69. if ($baz)
  70. return;
  71. }',
  72. ];
  73. yield [
  74. '<?php
  75. function b($b) {
  76. if ($b) {
  77. return;
  78. }
  79. /**/
  80. }',
  81. '<?php
  82. function b($b) {
  83. if ($b) {
  84. return;
  85. }
  86. return /**/;
  87. }',
  88. ];
  89. yield [
  90. '<?php
  91. class Test2
  92. {
  93. private static function a($a)
  94. {
  95. if ($a) {
  96. return;
  97. }
  98. $c1 = function() use ($a) {
  99. if ($a)
  100. return;
  101. if ($a > 1) return;
  102. echo $a;
  103. '.'
  104. };
  105. $c1();
  106. '.'
  107. '.'
  108. }
  109. private function test()
  110. {
  111. $d = function(){
  112. echo 123;
  113. '.'
  114. };
  115. $d();
  116. }
  117. }',
  118. '<?php
  119. class Test2
  120. {
  121. private static function a($a)
  122. {
  123. if ($a) {
  124. return;
  125. }
  126. $c1 = function() use ($a) {
  127. if ($a)
  128. return;
  129. if ($a > 1) return;
  130. echo $a;
  131. return;
  132. };
  133. $c1();
  134. return
  135. ;
  136. }
  137. private function test()
  138. {
  139. $d = function(){
  140. echo 123;
  141. return;
  142. };
  143. $d();
  144. }
  145. }',
  146. ];
  147. yield [
  148. '<?php
  149. function aT($a) {
  150. if ($a) {
  151. return;
  152. }
  153. '.'
  154. }',
  155. '<?php
  156. function aT($a) {
  157. if ($a) {
  158. return;
  159. }
  160. return ;
  161. }',
  162. ];
  163. yield [
  164. '<?php return;',
  165. ];
  166. yield [
  167. '<?php
  168. function c($c) {
  169. if ($c) {
  170. return;
  171. }
  172. //'.'
  173. }',
  174. '<?php
  175. function c($c) {
  176. if ($c) {
  177. return;
  178. }
  179. return;//
  180. }',
  181. ];
  182. yield [
  183. '<?php
  184. class Test {
  185. private static function d($d) {
  186. if ($d) {
  187. return;
  188. }
  189. }
  190. }',
  191. '<?php
  192. class Test {
  193. private static function d($d) {
  194. if ($d) {
  195. return;
  196. }
  197. return;}
  198. }',
  199. ];
  200. yield [
  201. '<?php
  202. interface FooInterface
  203. {
  204. public function fnc();
  205. }',
  206. ];
  207. yield [
  208. '<?php
  209. abstract class AbstractFoo
  210. {
  211. abstract public function fnc();
  212. abstract public function fnc1();
  213. static private function fn2(){}
  214. public function fnc3() {
  215. echo 1 . self::fn2();//{}
  216. }
  217. }',
  218. ];
  219. yield [
  220. '<?php
  221. function foo () { }',
  222. ];
  223. yield [
  224. '<?php
  225. $a = function() {
  226. /**/
  227. '.'
  228. /* a */ //
  229. '.'
  230. };
  231. ',
  232. '<?php
  233. $a = function() {
  234. return ; /**/
  235. return ;
  236. /* a */ return; //
  237. return;
  238. };
  239. ',
  240. ];
  241. }
  242. }