ReturnAssignmentFixerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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\ReturnNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author SpacePossum
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ReturnNotation\ReturnAssignmentFixer
  19. */
  20. final class ReturnAssignmentFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @dataProvider provideFixNestedFunctionsCases
  24. *
  25. * @param string $expected
  26. * @param string $input
  27. */
  28. public function testFixNestedFunctions($expected, $input)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixNestedFunctionsCases()
  33. {
  34. return [
  35. [
  36. '<?php
  37. function A($a0,$a1,$a2,$d)
  38. {
  39. if ($a0) {
  40. return 1;
  41. // fix me
  42. }
  43. if ($a1) {
  44. return 2;
  45. // fix me
  46. }
  47. $nested0 = function() {
  48. global $a;
  49. ++$a;
  50. $d = 2;
  51. $nested1 = function () use ($d) {
  52. if ($d) {
  53. return 3;
  54. // fix me
  55. }
  56. $nested2 = function (&$d) {
  57. if ($d) {
  58. $f = 1;
  59. return $f; // fix me not
  60. }
  61. $d = function () {
  62. return 4;
  63. // fix me
  64. };
  65. if ($d+1) {
  66. $f = 1;
  67. return $f; // fix me not
  68. }
  69. };
  70. return $nested2();
  71. };
  72. return $a; // fix me not
  73. };
  74. if ($a2) {
  75. return 5;
  76. // fix me
  77. }
  78. }
  79. function B($b0, $b1, $b2)
  80. {
  81. if ($b0) {
  82. return 10;
  83. // fix me
  84. }
  85. if ($b1) {
  86. return 20;
  87. // fix me
  88. }
  89. if ($b2) {
  90. return 30;
  91. // fix me
  92. }
  93. }
  94. ',
  95. '<?php
  96. function A($a0,$a1,$a2,$d)
  97. {
  98. if ($a0) {
  99. $b = 1;
  100. return $b; // fix me
  101. }
  102. if ($a1) {
  103. $c = 2;
  104. return $c; // fix me
  105. }
  106. $nested0 = function() {
  107. global $a;
  108. ++$a;
  109. $d = 2;
  110. $nested1 = function () use ($d) {
  111. if ($d) {
  112. $f = 3;
  113. return $f; // fix me
  114. }
  115. $nested2 = function (&$d) {
  116. if ($d) {
  117. $f = 1;
  118. return $f; // fix me not
  119. }
  120. $d = function () {
  121. $a = 4;
  122. return $a; // fix me
  123. };
  124. if ($d+1) {
  125. $f = 1;
  126. return $f; // fix me not
  127. }
  128. };
  129. return $nested2();
  130. };
  131. return $a; // fix me not
  132. };
  133. if ($a2) {
  134. $d = 5;
  135. return $d; // fix me
  136. }
  137. }
  138. function B($b0, $b1, $b2)
  139. {
  140. if ($b0) {
  141. $b = 10;
  142. return $b; // fix me
  143. }
  144. if ($b1) {
  145. $c = 20;
  146. return $c; // fix me
  147. }
  148. if ($b2) {
  149. $d = 30;
  150. return $d; // fix me
  151. }
  152. }
  153. ',
  154. ],
  155. ];
  156. }
  157. /**
  158. * @dataProvider provideFixCases
  159. *
  160. * @param string $expected
  161. * @param string $input
  162. */
  163. public function testFix($expected, $input)
  164. {
  165. $this->doTest($expected, $input);
  166. }
  167. public function provideFixCases()
  168. {
  169. return [
  170. [
  171. '<?php
  172. function A()
  173. {
  174. return 15;
  175. }
  176. ',
  177. '<?php
  178. function A()
  179. {
  180. $a = 15;
  181. return $a;
  182. }
  183. ',
  184. ],
  185. [
  186. '<?php
  187. function A()
  188. {
  189. /*0*/return /*1*//*2*/15;/*3*//*4*/ /*5*/ /*6*//*7*//*8*/
  190. }
  191. ',
  192. '<?php
  193. function A()
  194. {
  195. /*0*/$a/*1*/=/*2*/15;/*3*//*4*/ /*5*/ return/*6*/$a/*7*/;/*8*/
  196. }
  197. ',
  198. ],
  199. 'comments with leading space' => [
  200. '<?php
  201. function A()
  202. { #1
  203. #2
  204. return #3
  205. #4
  206. #5
  207. #6
  208. 15 #7
  209. ; #8
  210. #9
  211. #10
  212. #11
  213. #12
  214. #13
  215. #14
  216. #15
  217. }
  218. ',
  219. '<?php
  220. function A()
  221. { #1
  222. #2
  223. $a #3
  224. #4
  225. = #5
  226. #6
  227. 15 #7
  228. ; #8
  229. #9
  230. return #10
  231. #11
  232. $a #12
  233. #13
  234. ; #14
  235. #15
  236. }
  237. ',
  238. ],
  239. [
  240. '<?php
  241. abstract class B
  242. {
  243. abstract protected function Z();public function A()
  244. {
  245. return 16;
  246. }
  247. }
  248. ',
  249. '<?php
  250. abstract class B
  251. {
  252. abstract protected function Z();public function A()
  253. {
  254. $a = 16; return $a;
  255. }
  256. }
  257. ',
  258. ],
  259. [
  260. '<?php
  261. function b() {
  262. if ($c) {
  263. return 0;
  264. }
  265. return testFunction(654+1);
  266. }
  267. ',
  268. '<?php
  269. function b() {
  270. if ($c) {
  271. $b = 0;
  272. return $b;
  273. }
  274. $a = testFunction(654+1);
  275. return $a;
  276. }
  277. ',
  278. ],
  279. 'minimal notation' => [
  280. '<?php $e=function(){return 1;};$f=function(){return 1;};$g=function(){return 1;};',
  281. '<?php $e=function(){$a=1;return$a;};$f=function(){$a=1;return$a;};$g=function(){$a=1;return$a;};',
  282. ],
  283. [
  284. '<?php
  285. function A()
  286. {#1
  287. #2 '.'
  288. return #3
  289. #4
  290. #5
  291. #6
  292. 15#7
  293. ;#8
  294. #9
  295. #10
  296. #11
  297. #12
  298. #13
  299. #14
  300. #15
  301. }
  302. ',
  303. '<?php
  304. function A()
  305. {#1
  306. #2 '.'
  307. $a#3
  308. #4
  309. =#5
  310. #6
  311. 15#7
  312. ;#8
  313. #9
  314. return#10
  315. #11
  316. $a#12
  317. #13
  318. ;#14
  319. #15
  320. }
  321. ',
  322. ],
  323. [
  324. '<?php
  325. function A($b)
  326. {
  327. // Comment
  328. return a("2", 4, $b);
  329. }
  330. ',
  331. '<?php
  332. function A($b)
  333. {
  334. // Comment
  335. $value = a("2", 4, $b);
  336. return $value;
  337. }
  338. ',
  339. ],
  340. [
  341. '<?php function a($b,$c) {if($c>1){echo 1;} return (1 + 2 + $b); }',
  342. '<?php function a($b,$c) {if($c>1){echo 1;} $a= (1 + 2 + $b);return $a; }',
  343. ],
  344. [
  345. '<?php function a($b,$c) {return (3 * 4 + $b); }',
  346. '<?php function a($b,$c) {$zz= (3 * 4 + $b);return $zz; }',
  347. ],
  348. [
  349. '<?php
  350. function a() {
  351. return 4563;
  352. ?> <?php
  353. }
  354. ',
  355. '<?php
  356. function a() {
  357. $a = 4563;
  358. return $a ?> <?php
  359. }
  360. ',
  361. ],
  362. [
  363. '<?php
  364. function a()
  365. {
  366. return $c + 1; /*
  367. var names are case insensitive */ }
  368. ',
  369. '<?php
  370. function a()
  371. {
  372. $A = $c + 1; /*
  373. var names are case insensitive */ return $a ;}
  374. ',
  375. ],
  376. ];
  377. }
  378. /**
  379. * @dataProvider provideDoNotFixCases
  380. *
  381. * @param string $expected
  382. */
  383. public function testDoNotFix($expected)
  384. {
  385. $this->doTest($expected);
  386. }
  387. public function provideDoNotFixCases()
  388. {
  389. return [
  390. 'static' => [
  391. '<?php
  392. function a() {
  393. static $a;
  394. $a = time();
  395. return $a;
  396. }
  397. ',
  398. ],
  399. 'global' => [
  400. '<?php
  401. function a() {
  402. global $a;
  403. $a = time();
  404. return $a;
  405. }
  406. ',
  407. ],
  408. 'passed by reference' => [
  409. '<?php
  410. function foo(&$var)
  411. {
  412. $var = 1;
  413. return $var;
  414. }
  415. ',
  416. ],
  417. 'not in function scope' => [
  418. '<?php
  419. $a = 1; // var might be global here
  420. return $a;
  421. ',
  422. ],
  423. [
  424. '<?php
  425. function a()
  426. {
  427. $a = 1;
  428. ?>
  429. <?php
  430. ;
  431. return $a;
  432. }
  433. ',
  434. ],
  435. [
  436. '<?php
  437. function a()
  438. {
  439. $a = 1 ?><?php return $a;
  440. }',
  441. ],
  442. [
  443. '<?php
  444. function a()
  445. {
  446. $a = 1
  447. ?>
  448. <?php
  449. return $a;
  450. }
  451. ',
  452. ],
  453. [
  454. '<?php
  455. $zz = 1 ?><?php
  456. function a($zz)
  457. {
  458. ;
  459. return $zz;
  460. }
  461. ',
  462. ],
  463. 'return complex statement' => [
  464. '<?php
  465. function a($c)
  466. {
  467. $a = 1;
  468. return $a + $c;
  469. }
  470. ',
  471. ],
  472. 'array assign' => [
  473. '<?php
  474. function a($c)
  475. {
  476. $_SERVER["abc"] = 3;
  477. return $_SERVER;
  478. }
  479. ',
  480. ],
  481. 'if assign' => [
  482. '<?php
  483. function foo ($bar)
  484. {
  485. $a = 123;
  486. if ($bar)
  487. $a = 12345;
  488. return $a;
  489. }
  490. ',
  491. ],
  492. 'else assign' => [
  493. '<?php
  494. function foo ($bar)
  495. {
  496. $a = 123;
  497. if ($bar)
  498. ;
  499. else
  500. $a = 12345;
  501. return $a;
  502. }
  503. ',
  504. ],
  505. 'elseif assign' => [
  506. '<?php
  507. function foo ($bar)
  508. {
  509. $a = 123;
  510. if ($bar)
  511. ;
  512. elseif($b)
  513. $a = 12345;
  514. return $a;
  515. }
  516. ',
  517. ],
  518. 'echo $a = N / comment $a = N;' => [
  519. '<?php
  520. function a($c)
  521. {
  522. $a = 1;
  523. echo $a."=1";
  524. return $a;
  525. }
  526. function b($c)
  527. {
  528. $a = 1;
  529. echo $a."=1;";
  530. return $a;
  531. }
  532. function c($c)
  533. {
  534. $a = 1;
  535. echo $a;
  536. // $a =1;
  537. return $a;
  538. }
  539. ',
  540. ],
  541. 'if ($a = N)' => [
  542. '<?php
  543. function a($c)
  544. {
  545. if ($a = 1)
  546. return $a;
  547. }
  548. ',
  549. ],
  550. 'changed after declaration' => [
  551. '<?php
  552. function a($c)
  553. {
  554. $a = 1;
  555. $a += 1;
  556. return $a;
  557. }
  558. function b($c)
  559. {
  560. $a = 1;
  561. $a -= 1;
  562. return $a;
  563. }
  564. ',
  565. ],
  566. 'complex statement' => [
  567. '<?php
  568. function a($c)
  569. {
  570. $d = $c && $a = 1;
  571. return $a;
  572. }
  573. ',
  574. ],
  575. 'PHP close tag within function' => [
  576. '<?php
  577. function a($zz)
  578. {
  579. $zz = 1 ?><?php
  580. ;
  581. return $zz;
  582. }
  583. ',
  584. ],
  585. 'import global using "require"' => [
  586. '<?php
  587. function a()
  588. {
  589. require __DIR__."/test3.php";
  590. $b = 1;
  591. return $b;
  592. }
  593. ',
  594. ],
  595. 'import global using "require_once"' => [
  596. '<?php
  597. function a()
  598. {
  599. require_once __DIR__."/test3.php";
  600. $b = 1;
  601. return $b;
  602. }
  603. ',
  604. ],
  605. 'import global using "include"' => [
  606. '<?php
  607. function a()
  608. {
  609. include __DIR__."/test3.php";
  610. $b = 1;
  611. return $b;
  612. }
  613. ',
  614. ],
  615. 'import global using "include_once"' => [
  616. '<?php
  617. function a()
  618. {
  619. include_once __DIR__."/test3.php";
  620. $b = 1;
  621. return $b;
  622. }
  623. ',
  624. ],
  625. 'eval' => [
  626. '<?php
  627. $b = function ($z) {
  628. $c = eval($z);
  629. return $c;
  630. };
  631. $c = function ($x) {
  632. $x = eval($x);
  633. $x = 1;
  634. return $x;
  635. };
  636. ',
  637. ],
  638. '${X}' => [
  639. '<?php
  640. function A($g)
  641. {
  642. $h = ${$g};
  643. return $h;
  644. }
  645. ',
  646. ],
  647. '$$' => [
  648. '<?php
  649. function B($c)
  650. {
  651. $b = $$c;
  652. return $b;
  653. }
  654. ',
  655. ],
  656. [
  657. '<?php
  658. class XYZ
  659. {
  660. public function test1()
  661. {
  662. $GLOBALS = 2;
  663. return $GLOBALS;
  664. }
  665. public function test2()
  666. {
  667. $_server = 2;
  668. return $_server;
  669. }
  670. public function __destruct()
  671. {
  672. $GLOBALS[\'a\'] = 2;
  673. return $GLOBALS[\'a\']; // destruct cannot return but still lints
  674. }
  675. };
  676. $a = new XYZ();
  677. $a = 1;
  678. var_dump($a); // $a = 2 here _╯°□°╯︵┻━┻
  679. ',
  680. ],
  681. ];
  682. }
  683. }