MethodSeparationFixerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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\ClassNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\ClassNotation\MethodSeparationFixer
  18. */
  19. final class MethodSeparationFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @param string $expected
  23. * @param null|string $input
  24. *
  25. * @dataProvider provideFixClassesCases
  26. */
  27. public function testFixClasses($expected, $input = null)
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. public function provideFixClassesCases()
  32. {
  33. $cases = [];
  34. $cases[] = ['<?php
  35. class SomeClass1
  36. {
  37. // This comment
  38. // is multiline.
  39. public function echoA()
  40. {
  41. echo "a";
  42. }
  43. }
  44. '];
  45. $cases[] = [
  46. '<?php
  47. class SomeClass2
  48. {
  49. // This comment
  50. /* is multiline. */
  51. public function echoA()
  52. {
  53. echo "a";
  54. }
  55. }
  56. ',
  57. '<?php
  58. class SomeClass2
  59. {
  60. // This comment
  61. /* is multiline. */public function echoA()
  62. {
  63. echo "a";
  64. }
  65. }
  66. ',
  67. ];
  68. $cases[] = [
  69. '<?php
  70. class SomeClass3
  71. {
  72. // This comment
  73. // is multiline.
  74. public function echoA()
  75. {
  76. echo "a";
  77. }
  78. }
  79. ', ];
  80. $cases[] = [
  81. '<?php
  82. class SomeClass1
  83. {
  84. private $a; //
  85. public function methodA()
  86. {
  87. }
  88. private $b;
  89. //
  90. public function methodB()
  91. {
  92. }
  93. // C
  94. public function methodC()
  95. {
  96. }
  97. // D
  98. public function methodD()
  99. {
  100. }
  101. /* E */
  102. public function methodE()
  103. {
  104. }
  105. /* F */
  106. public function methodF()
  107. {
  108. }
  109. }
  110. ',
  111. '<?php
  112. class SomeClass1
  113. {
  114. private $a; //
  115. public function methodA()
  116. {
  117. }
  118. private $b;
  119. //
  120. public function methodB()
  121. {
  122. }
  123. // C
  124. public function methodC()
  125. {
  126. }
  127. // D
  128. public function methodD()
  129. {
  130. }
  131. /* E */
  132. public function methodE()
  133. {
  134. }
  135. /* F */
  136. public function methodF()
  137. {
  138. }
  139. }
  140. ', ];
  141. $cases[] = ['<?php
  142. class SomeClass
  143. {
  144. // comment
  145. public function echoA()
  146. {
  147. echo "a";
  148. }
  149. }
  150. '];
  151. $cases[] = ['<?php
  152. class SomeClass
  153. {
  154. // This comment
  155. // is multiline.
  156. public function echoA()
  157. {
  158. echo "a";
  159. }
  160. }
  161. '];
  162. $cases[] = [
  163. '<?php
  164. class SomeClass
  165. {
  166. // comment
  167. public function echoA()
  168. {
  169. echo "a";
  170. }
  171. }
  172. ',
  173. '<?php
  174. class SomeClass
  175. {
  176. // comment
  177. public function echoA()
  178. {
  179. echo "a";
  180. }
  181. }
  182. ',
  183. ];
  184. $cases[] = [
  185. '<?php
  186. class SomeClass
  187. {
  188. /* comment */
  189. public function echoB()
  190. {
  191. echo "a";
  192. }
  193. }
  194. ',
  195. '<?php
  196. class SomeClass
  197. {
  198. /* comment */public function echoB()
  199. {
  200. echo "a";
  201. }
  202. }
  203. ',
  204. ];
  205. $cases[] = [
  206. '<?php
  207. class SomeClass
  208. {
  209. /* comment */
  210. public function echoC()
  211. {
  212. echo "a";
  213. }
  214. }
  215. ',
  216. '<?php
  217. class SomeClass
  218. {
  219. /* comment */ public function echoC()
  220. {
  221. echo "a";
  222. }
  223. }
  224. ',
  225. ];
  226. $cases[] = [
  227. '<?php
  228. abstract class MethodTest2
  229. {
  230. public function method045()
  231. {
  232. $files = null;
  233. if (!empty($files)) {
  234. $this->filter(
  235. function (\SplFileInfo $file) use ($files) {
  236. return !in_array($file->getRelativePathname(), $files, true);
  237. }
  238. );
  239. }
  240. }
  241. private $a;
  242. public static function method145()
  243. {
  244. }
  245. abstract protected function method245();
  246. // comment
  247. final private function method345()
  248. {
  249. }
  250. }
  251. function test1(){ echo 1;}
  252. function test2(){ echo 2;}',
  253. '<?php
  254. abstract class MethodTest2
  255. {
  256. public function method045()
  257. {
  258. $files = null;
  259. if (!empty($files)) {
  260. $this->filter(
  261. function (\SplFileInfo $file) use ($files) {
  262. return !in_array($file->getRelativePathname(), $files, true);
  263. }
  264. );
  265. }
  266. }
  267. private $a;
  268. public static function method145()
  269. {
  270. }
  271. abstract protected function method245();
  272. // comment
  273. final private function method345()
  274. {
  275. }
  276. }
  277. function test1(){ echo 1;}
  278. function test2(){ echo 2;}',
  279. ];
  280. $cases[] = [
  281. '<?php
  282. /*
  283. * This file is part of the PHP CS utility.
  284. *
  285. * (c) Fabien Potencier <fabien@symfony.com>
  286. *
  287. * This source file is subject to the MIT license that is bundled
  288. * with this source code in the file LICENSE.
  289. */
  290. namespace PhpCsFixer\Linter;
  291. /**
  292. * Dummy linter. No linting is performed. No error is raised.
  293. *
  294. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  295. *
  296. * @internal
  297. */
  298. final class NullLinter implements LinterInterface
  299. {
  300. /**
  301. * {@inheritdoc}
  302. */
  303. public function lintFile($path)
  304. {
  305. unset($path);
  306. }
  307. /**
  308. * {@inheritdoc}
  309. */
  310. public function lintSource($source)
  311. {
  312. unset($source);
  313. }
  314. }
  315. ',
  316. ];
  317. // do not touch anonymous functions (since PHP doesn't allow
  318. // for class attributes being functions :(, we only have to test
  319. // those used within methods)
  320. $cases[] = [
  321. '<?php
  322. class MethodTestAnonymous
  323. {
  324. public function method444a()
  325. {
  326. $text = "hello";
  327. $example = function ($arg) use ($message) {
  328. var_dump($arg . " " . $message);
  329. };
  330. $example($text);
  331. $example = function($arg) use ($message) {
  332. var_dump($arg . " " . $message);
  333. };
  334. $example = function /*test*/ ($arg) use ($message) {
  335. var_dump($arg . " " . $message);
  336. };
  337. }
  338. }',
  339. ];
  340. $cases[] = [
  341. '<?php
  342. class MethodTest1
  343. {
  344. private $c; //
  345. public function method444a()
  346. {
  347. }
  348. /**
  349. *
  350. */
  351. public function method444b()
  352. {
  353. }
  354. //
  355. public function method444c()
  356. {
  357. }
  358. private $a;
  359. public function method444d()
  360. {
  361. }
  362. private $b;
  363. //
  364. public function method444e()
  365. {
  366. }
  367. public function method444f()
  368. {
  369. }
  370. private $d; //
  371. public function method444f1()
  372. {
  373. }
  374. /**/
  375. public function method444g()
  376. {
  377. }
  378. }',
  379. '<?php
  380. class MethodTest1
  381. {
  382. private $c; //
  383. public function method444a()
  384. {
  385. }
  386. /**
  387. *
  388. */
  389. public function method444b()
  390. {
  391. }
  392. //
  393. public function method444c()
  394. {
  395. }
  396. private $a;
  397. public function method444d()
  398. {
  399. }
  400. private $b;
  401. //
  402. public function method444e()
  403. {
  404. }
  405. public function method444f()
  406. {
  407. }
  408. private $d; //
  409. public function method444f1()
  410. {
  411. }
  412. /**/
  413. public function method444g()
  414. {
  415. }
  416. }',
  417. ];
  418. // spaces between methods
  419. $cases[] = [
  420. '<?php
  421. abstract class MethodTest3
  422. {
  423. public function method021()
  424. {
  425. }
  426. public static function method121()
  427. {
  428. }
  429. abstract protected function method221(); '.'
  430. final private function method321a()
  431. {
  432. }
  433. }',
  434. '<?php
  435. abstract class MethodTest3
  436. {
  437. public function method021()
  438. {
  439. }
  440. public static function method121()
  441. {
  442. }
  443. abstract protected function method221();
  444. '.'
  445. final private function method321a()
  446. {
  447. }
  448. }', ];
  449. // don't change correct code
  450. $cases[] = [
  451. '<?php
  452. class SmallHelperException extends \Exception
  453. {
  454. public function getId111()
  455. {
  456. return 1;
  457. }
  458. public function getMessage111()
  459. {
  460. return \'message\';
  461. }
  462. }
  463. class MethodTest123124124
  464. {
  465. public function method111a(){}
  466. public function method211a(){}
  467. }',
  468. ];
  469. // do not touch function out of class scope
  470. $cases[] = [
  471. '<?php
  472. function test0() {
  473. }
  474. class MethodTest4
  475. {
  476. public function method122b()
  477. {
  478. }
  479. public function method222b()
  480. {
  481. }
  482. }
  483. function test() {
  484. }
  485. function test2() {
  486. }
  487. ',
  488. ];
  489. return $cases;
  490. }
  491. /**
  492. * @param string $expected
  493. * @param null|string $input
  494. *
  495. * @dataProvider provideFixTraitsCases
  496. */
  497. public function testFixTraits($expected, $input = null)
  498. {
  499. $this->doTest($expected, $input);
  500. }
  501. public function provideFixTraitsCases()
  502. {
  503. $cases = [];
  504. // do not touch well formatted traits
  505. $cases[] = [
  506. '<?php
  507. trait OkTrait
  508. {
  509. function getReturnTypeOk()
  510. {
  511. }
  512. /**
  513. *
  514. */
  515. function getReturnDescriptionOk()
  516. {
  517. }
  518. }',
  519. ];
  520. $cases[] = [
  521. '<?php
  522. trait ezcReflectionReturnInfo {
  523. public $x = 1;
  524. protected function getA(){echo 1;}
  525. function getB(){echo 2;}
  526. protected function getC(){echo 3;}
  527. /** Description */
  528. function getD(){echo 4;}
  529. protected function getE(){echo 3;}
  530. private $a;
  531. function getF(){echo 4;}
  532. }',
  533. '<?php
  534. trait ezcReflectionReturnInfo {
  535. public $x = 1;
  536. protected function getA(){echo 1;}function getB(){echo 2;}
  537. protected function getC(){echo 3;}/** Description */function getD(){echo 4;}
  538. protected function getE(){echo 3;}private $a;function getF(){echo 4;}
  539. }',
  540. ];
  541. $cases[] = [
  542. '<?php
  543. trait SomeReturnInfo {
  544. function getReturnType()
  545. {
  546. }
  547. function getReturnDescription()
  548. {
  549. }
  550. function getReturnDescription2()
  551. {
  552. }
  553. abstract public function getWorld();
  554. }',
  555. '<?php
  556. trait SomeReturnInfo {
  557. function getReturnType()
  558. {
  559. }
  560. function getReturnDescription()
  561. {
  562. } function getReturnDescription2()
  563. {
  564. }
  565. abstract public function getWorld();
  566. }',
  567. ];
  568. return $cases;
  569. }
  570. /**
  571. * @param string $expected
  572. * @param null|string $input
  573. *
  574. * @dataProvider provideFixInterfaceCases
  575. */
  576. public function testFixInterface($expected, $input = null)
  577. {
  578. $this->doTest($expected, $input);
  579. }
  580. public function provideFixInterfaceCases()
  581. {
  582. $cases = [];
  583. $cases[] = [
  584. '<?php
  585. interface TestInterface
  586. {
  587. public function testInterfaceMethod4();
  588. public function testInterfaceMethod5();
  589. /**
  590. * {@link}
  591. */ '.'
  592. public function testInterfaceMethod6();
  593. public function testInterfaceMethod7();
  594. public function testInterfaceMethod8();
  595. }',
  596. '<?php
  597. interface TestInterface
  598. { public function testInterfaceMethod4();
  599. public function testInterfaceMethod5();
  600. /**
  601. * {@link}
  602. */ '.'
  603. public function testInterfaceMethod6();
  604. public function testInterfaceMethod7(); public function testInterfaceMethod8();
  605. }',
  606. ];
  607. // do not touch well formatted interfaces
  608. $cases[] = [
  609. '<?php
  610. interface TestInterfaceOK
  611. {
  612. public function testMethod1();
  613. public function testMethod2();
  614. }',
  615. ];
  616. // method after trait use
  617. $cases[] = [
  618. '<?php
  619. trait ezcReflectionReturnInfo {
  620. function getReturnDescription() {}
  621. }
  622. class ezcReflectionMethod extends ReflectionMethod {
  623. use ezcReflectionReturnInfo;
  624. function afterUseTrait(){}
  625. function afterUseTrait2(){}
  626. }',
  627. '<?php
  628. trait ezcReflectionReturnInfo {
  629. function getReturnDescription() {}
  630. }
  631. class ezcReflectionMethod extends ReflectionMethod {
  632. use ezcReflectionReturnInfo;function afterUseTrait(){}function afterUseTrait2(){}
  633. }',
  634. ];
  635. return $cases;
  636. }
  637. /**
  638. * @param string $expected
  639. * @param null|string $input
  640. *
  641. * @dataProvider provideMessyWhitespacesCases
  642. */
  643. public function testMessyWhitespaces($expected, $input = null)
  644. {
  645. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  646. $this->doTest($expected, $input);
  647. }
  648. public function provideMessyWhitespacesCases()
  649. {
  650. return [
  651. [
  652. "<?php\r\nclass SomeClass\r\n{\r\n // comment\n\n public function echoA()\r\n {\r\n echo 'a';\r\n }\r\n}\r\n",
  653. "<?php\r\nclass SomeClass\r\n{\r\n // comment\n\n\n public function echoA()\r\n {\r\n echo 'a';\r\n }\r\n}\r\n",
  654. ],
  655. [
  656. "<?php\r\nclass SomeClass\r\n{\r\n // comment\r\n\r\n public function echoA()\r\n {\r\n echo 'a';\r\n }\r\n}\r\n",
  657. "<?php\r\nclass SomeClass\r\n{\r\n // comment\r\n\r\n\r\n public function echoA()\r\n {\r\n echo 'a';\r\n }\r\n}\r\n",
  658. ],
  659. ];
  660. }
  661. }