NoUselessReturnFixerTest.php 5.0 KB

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