OrderedClassElementsFixerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. /**
  14. * @author Gregor Harlan <gharlan@web.de>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer
  19. */
  20. final class OrderedClassElementsFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. return [
  35. [
  36. <<<'EOT'
  37. <?php
  38. class Foo {}
  39. class Bar
  40. {
  41. }
  42. EOT
  43. ],
  44. [
  45. <<<'EOT'
  46. <?php
  47. class Foo { const C1 = 1; protected $abc = 'abc'; public function baz($y, $z) {} private function bar1($x) { return 1; } }
  48. EOT
  49. , <<<'EOT'
  50. <?php
  51. class Foo { private function bar1($x) { return 1; } protected $abc = 'abc'; const C1 = 1; public function baz($y, $z) {} }
  52. EOT
  53. ],
  54. [
  55. <<<'EOT'
  56. <?php
  57. interface FooInterface
  58. {
  59. const CONST1 = 'const1';
  60. const CONST2 = 'const2';
  61. public function xyz($x, $y, $z); // comment
  62. /**
  63. * @param array $a
  64. *
  65. * @return string
  66. */
  67. function abc(array &$a = null);
  68. public function def();
  69. }
  70. EOT
  71. , <<<'EOT'
  72. <?php
  73. interface FooInterface
  74. {
  75. public function xyz($x, $y, $z); // comment
  76. const CONST1 = 'const1';
  77. /**
  78. * @param array $a
  79. *
  80. * @return string
  81. */
  82. function abc(array &$a = null);
  83. const CONST2 = 'const2';
  84. public function def();
  85. }
  86. EOT
  87. ],
  88. [
  89. <<<'EOT'
  90. <?php
  91. abstract class Foo extends FooParent implements FooInterface1, FooInterface2
  92. {
  93. use Bar;
  94. use Baz {
  95. abc as private;
  96. }
  97. const C1 = 1;
  98. /* comment for C2 */
  99. const C2 = 2;
  100. public $fooPublic;
  101. // comment 3
  102. protected $fooProtected = array(1, 2);
  103. // comment 1
  104. private $fooPrivate;
  105. protected function __construct()
  106. {
  107. }
  108. public function __destruct() {}
  109. public function __clone() {}
  110. public static function setUpBeforeClass() {}
  111. public static function teardownafterclass() {
  112. } /* multiline
  113. comment */
  114. protected function setUp() {}
  115. protected function tearDown() {}
  116. abstract public function foo1($a, $b = 1);
  117. // foo2
  118. function foo2()
  119. {
  120. return $this->foo1(1);
  121. }
  122. public static function foo3(self $foo)
  123. {
  124. return $foo->foo2();
  125. } /* comment 1 */ /* comment 2 */
  126. // comment
  127. /**
  128. * Docblock
  129. */
  130. protected function foo4(\ArrayObject $object, array $array, $a = null)
  131. {
  132. bar();
  133. if (!$a) {
  134. $a = function ($x) {
  135. var_dump($x);
  136. };
  137. }
  138. }
  139. private function foo5()
  140. {
  141. } // end foo5
  142. }
  143. EOT
  144. , <<<'EOT'
  145. <?php
  146. abstract class Foo extends FooParent implements FooInterface1, FooInterface2
  147. {
  148. // comment 1
  149. private $fooPrivate;
  150. abstract public function foo1($a, $b = 1);
  151. protected function tearDown() {}
  152. public function __clone() {}
  153. const C1 = 1;
  154. // foo2
  155. function foo2()
  156. {
  157. return $this->foo1(1);
  158. }
  159. public static function setUpBeforeClass() {}
  160. public function __destruct() {}
  161. use Bar;
  162. public static function foo3(self $foo)
  163. {
  164. return $foo->foo2();
  165. } /* comment 1 */ /* comment 2 */
  166. // comment 3
  167. protected $fooProtected = array(1, 2);
  168. public $fooPublic;
  169. /* comment for C2 */
  170. const C2 = 2;
  171. public static function teardownafterclass() {
  172. } /* multiline
  173. comment */
  174. use Baz {
  175. abc as private;
  176. }
  177. private function foo5()
  178. {
  179. } // end foo5
  180. protected function setUp() {}
  181. protected function __construct()
  182. {
  183. }
  184. // comment
  185. /**
  186. * Docblock
  187. */
  188. protected function foo4(\ArrayObject $object, array $array, $a = null)
  189. {
  190. bar();
  191. if (!$a) {
  192. $a = function ($x) {
  193. var_dump($x);
  194. };
  195. }
  196. }
  197. }
  198. EOT
  199. ],
  200. [
  201. <<<'EOT'
  202. <?php
  203. class Foo
  204. {
  205. const C = 'C';
  206. public function abc() {}
  207. protected function xyz() {}
  208. }
  209. class Bar
  210. {
  211. const C = 1;
  212. public function foo($a) { return 1; }
  213. public function baz() {}
  214. }
  215. EOT
  216. , <<<'EOT'
  217. <?php
  218. class Foo
  219. {
  220. protected function xyz() {}
  221. const C = 'C';
  222. public function abc() {}
  223. }
  224. class Bar
  225. {
  226. public function foo($a) { return 1; }
  227. const C = 1;
  228. public function baz() {}
  229. }
  230. EOT
  231. ],
  232. [
  233. <<<'EOT'
  234. <?php
  235. trait FooTrait
  236. {
  237. use BarTrait;
  238. use BazTrait;
  239. protected function abc() {
  240. }
  241. }
  242. EOT
  243. , <<<'EOT'
  244. <?php
  245. trait FooTrait
  246. {
  247. protected function abc() {
  248. }
  249. use BarTrait;
  250. use BazTrait;
  251. }
  252. EOT
  253. ],
  254. ];
  255. }
  256. /**
  257. * @param string $expected
  258. * @param null|string $input
  259. * @param array $configuration
  260. *
  261. * @dataProvider provideFix71Cases
  262. * @requires PHP 7.1
  263. */
  264. public function testFix71(array $configuration, $expected, $input = null)
  265. {
  266. $this->fixer->configure($configuration);
  267. $this->doTest($expected, $input);
  268. }
  269. public function provideFix71Cases()
  270. {
  271. return [
  272. [
  273. [],
  274. <<<'EOT'
  275. <?php
  276. class Foo
  277. {
  278. const C2 = 2;
  279. public const C1 = 1;
  280. public const C3 = 3;
  281. protected const C4 = 4;
  282. private const C5 = 5;
  283. }
  284. EOT
  285. , <<<'EOT'
  286. <?php
  287. class Foo
  288. {
  289. private const C5 = 5;
  290. const C2 = 2;
  291. public const C1 = 1;
  292. protected const C4 = 4;
  293. public const C3 = 3;
  294. }
  295. EOT
  296. ],
  297. [
  298. ['sortAlgorithm' => 'alpha'],
  299. <<<'EOT'
  300. <?php
  301. class Foo
  302. {
  303. public const C1 = 1;
  304. const C2 = 2;
  305. public const C3 = 3;
  306. protected const C4 = 4;
  307. private const C5 = 5;
  308. }
  309. EOT
  310. , <<<'EOT'
  311. <?php
  312. class Foo
  313. {
  314. private const C5 = 5;
  315. const C2 = 2;
  316. public const C1 = 1;
  317. protected const C4 = 4;
  318. public const C3 = 3;
  319. }
  320. EOT
  321. ],
  322. ];
  323. }
  324. /**
  325. * @param string $expected
  326. *
  327. * @group legacy
  328. * @dataProvider provideConfigurationCases
  329. * @expectedDeprecation Passing "order" at the root of the configuration is deprecated and will not be supported in 3.0, use "order" => array(...) option instead.
  330. */
  331. public function testLegacyFixWithConfiguration(array $configuration, $expected)
  332. {
  333. static $input = <<<'EOT'
  334. <?php
  335. class Foo
  336. {
  337. private static function privStatFunc() {}
  338. protected static $protStatProp;
  339. public static $pubStatProp1;
  340. public function pubFunc1() {}
  341. use BarTrait;
  342. public $pubProp1;
  343. public function __toString() {}
  344. protected function protFunc() {}
  345. protected $protProp;
  346. function pubFunc2() {}
  347. public function __destruct() {}
  348. var $pubProp2;
  349. private static $privStatProp;
  350. use BazTrait;
  351. public static function pubStatFunc1() {}
  352. public function pubFunc3() {}
  353. private $privProp;
  354. const C1 = 1;
  355. static function pubStatFunc2() {}
  356. private function privFunc() {}
  357. public static $pubStatProp2;
  358. protected function __construct() {}
  359. const C2 = 2;
  360. public static function pubStatFunc3() {}
  361. public $pubProp3;
  362. protected static function protStatFunc() {}
  363. }
  364. EOT;
  365. $this->fixer->configure($configuration);
  366. $this->doTest($expected, $input);
  367. }
  368. /**
  369. * @param string $expected
  370. *
  371. * @dataProvider provideConfigurationCases
  372. */
  373. public function testFixWithConfiguration(array $configuration, $expected)
  374. {
  375. static $input = <<<'EOT'
  376. <?php
  377. class Foo
  378. {
  379. private static function privStatFunc() {}
  380. protected static $protStatProp;
  381. public static $pubStatProp1;
  382. public function pubFunc1() {}
  383. use BarTrait;
  384. public $pubProp1;
  385. public function __toString() {}
  386. protected function protFunc() {}
  387. protected $protProp;
  388. function pubFunc2() {}
  389. public function __destruct() {}
  390. var $pubProp2;
  391. private static $privStatProp;
  392. use BazTrait;
  393. public static function pubStatFunc1() {}
  394. public function pubFunc3() {}
  395. private $privProp;
  396. const C1 = 1;
  397. static function pubStatFunc2() {}
  398. private function privFunc() {}
  399. public static $pubStatProp2;
  400. protected function __construct() {}
  401. const C2 = 2;
  402. public static function pubStatFunc3() {}
  403. public $pubProp3;
  404. protected static function protStatFunc() {}
  405. }
  406. EOT;
  407. $this->fixer->configure(['order' => $configuration]);
  408. $this->doTest($expected, $input);
  409. }
  410. public function provideConfigurationCases()
  411. {
  412. return [
  413. [
  414. ['use_trait', 'constant', 'property', 'construct', 'method', 'destruct'],
  415. <<<'EOT'
  416. <?php
  417. class Foo
  418. {
  419. use BarTrait;
  420. use BazTrait;
  421. const C1 = 1;
  422. const C2 = 2;
  423. protected static $protStatProp;
  424. public static $pubStatProp1;
  425. public $pubProp1;
  426. protected $protProp;
  427. var $pubProp2;
  428. private static $privStatProp;
  429. private $privProp;
  430. public static $pubStatProp2;
  431. public $pubProp3;
  432. protected function __construct() {}
  433. private static function privStatFunc() {}
  434. public function pubFunc1() {}
  435. public function __toString() {}
  436. protected function protFunc() {}
  437. function pubFunc2() {}
  438. public static function pubStatFunc1() {}
  439. public function pubFunc3() {}
  440. static function pubStatFunc2() {}
  441. private function privFunc() {}
  442. public static function pubStatFunc3() {}
  443. protected static function protStatFunc() {}
  444. public function __destruct() {}
  445. }
  446. EOT
  447. ],
  448. [
  449. ['public', 'protected', 'private'],
  450. <<<'EOT'
  451. <?php
  452. class Foo
  453. {
  454. public static $pubStatProp1;
  455. public function pubFunc1() {}
  456. public $pubProp1;
  457. public function __toString() {}
  458. function pubFunc2() {}
  459. public function __destruct() {}
  460. var $pubProp2;
  461. public static function pubStatFunc1() {}
  462. public function pubFunc3() {}
  463. const C1 = 1;
  464. static function pubStatFunc2() {}
  465. public static $pubStatProp2;
  466. const C2 = 2;
  467. public static function pubStatFunc3() {}
  468. public $pubProp3;
  469. protected static $protStatProp;
  470. protected function protFunc() {}
  471. protected $protProp;
  472. protected function __construct() {}
  473. protected static function protStatFunc() {}
  474. private static function privStatFunc() {}
  475. private static $privStatProp;
  476. private $privProp;
  477. private function privFunc() {}
  478. use BarTrait;
  479. use BazTrait;
  480. }
  481. EOT
  482. ],
  483. [
  484. [
  485. 'use_trait',
  486. 'constant',
  487. 'property_public_static',
  488. 'property_protected_static',
  489. 'property_private_static',
  490. 'property_public',
  491. 'property_protected',
  492. 'property_private',
  493. 'construct',
  494. 'destruct',
  495. 'magic',
  496. 'method_public_static',
  497. 'method_protected_static',
  498. 'method_private_static',
  499. 'method_public',
  500. 'method_protected',
  501. 'method_private',
  502. ],
  503. <<<'EOT'
  504. <?php
  505. class Foo
  506. {
  507. use BarTrait;
  508. use BazTrait;
  509. const C1 = 1;
  510. const C2 = 2;
  511. public static $pubStatProp1;
  512. public static $pubStatProp2;
  513. protected static $protStatProp;
  514. private static $privStatProp;
  515. public $pubProp1;
  516. var $pubProp2;
  517. public $pubProp3;
  518. protected $protProp;
  519. private $privProp;
  520. protected function __construct() {}
  521. public function __destruct() {}
  522. public function __toString() {}
  523. public static function pubStatFunc1() {}
  524. static function pubStatFunc2() {}
  525. public static function pubStatFunc3() {}
  526. protected static function protStatFunc() {}
  527. private static function privStatFunc() {}
  528. public function pubFunc1() {}
  529. function pubFunc2() {}
  530. public function pubFunc3() {}
  531. protected function protFunc() {}
  532. private function privFunc() {}
  533. }
  534. EOT
  535. ],
  536. ];
  537. }
  538. /**
  539. * @param array $configuration
  540. * @param string $input
  541. * @param string $expected
  542. *
  543. * @dataProvider provideSortingConfigurationCases
  544. */
  545. public function testFixWithSortingAlhorithm(array $configuration, $input, $expected)
  546. {
  547. $this->fixer->configure($configuration);
  548. $this->doTest($expected, $input);
  549. }
  550. public function provideSortingConfigurationCases()
  551. {
  552. return [
  553. [
  554. [
  555. 'order' => [
  556. 'property_public_static',
  557. 'method_public',
  558. 'method_private',
  559. ],
  560. 'sortAlgorithm' => 'alpha',
  561. ],
  562. <<<'EOT'
  563. <?php
  564. class Example
  565. {
  566. public function D(){}
  567. public static $pubStatProp2;
  568. public function B1(){}
  569. public function B2(){}
  570. private function E(){}
  571. public static $pubStatProp1;
  572. public function A(){}
  573. public function C(){}
  574. public function C1(){}
  575. }
  576. EOT
  577. ,
  578. <<<'EOT'
  579. <?php
  580. class Example
  581. {
  582. public static $pubStatProp1;
  583. public static $pubStatProp2;
  584. public function A(){}
  585. public function B1(){}
  586. public function B2(){}
  587. public function C(){}
  588. public function C1(){}
  589. public function D(){}
  590. private function E(){}
  591. }
  592. EOT
  593. ],
  594. [
  595. [
  596. 'order' => [
  597. 'use_trait',
  598. 'constant',
  599. 'property_public_static',
  600. 'property_protected_static',
  601. 'property_private_static',
  602. 'property_public',
  603. 'property_protected',
  604. 'property_private',
  605. 'construct',
  606. 'destruct',
  607. 'magic',
  608. 'method_public_static',
  609. 'method_protected_static',
  610. 'method_private_static',
  611. 'method_public',
  612. 'method_protected',
  613. 'method_private',
  614. ],
  615. 'sortAlgorithm' => 'alpha',
  616. ],
  617. <<<'EOT'
  618. <?php
  619. class Foo
  620. {
  621. private static function privStatFunc() {}
  622. protected static $protStatProp;
  623. use BazTrait;
  624. public static $pubStatProp2;
  625. public $pubProp3;
  626. use BarTrait;
  627. public function __toString() {}
  628. protected function protFunc() {}
  629. protected $protProp;
  630. function pubFunc2() {}
  631. public $pubProp1;
  632. public function __destruct() {}
  633. var $pubProp2;
  634. public function __magicB() {}
  635. const C2 = 2;
  636. public static $pubStatProp1;
  637. public function __magicA() {}
  638. private static $privStatProp;
  639. static function pubStatFunc2() {}
  640. public function pubFunc3(int $b, int $c) {
  641. $a = $b*$c;
  642. return $a % 4;
  643. }
  644. private $privProp;
  645. const C1 = 1;
  646. public static function pubStatFunc3() {
  647. return $this->privFunc();
  648. }
  649. public function pubFunc1() {}
  650. public static function pubStatFunc1() {}
  651. private function privFunc() {}
  652. protected function __construct() {}
  653. protected static function protStatFunc() {}
  654. }
  655. EOT
  656. ,
  657. <<<'EOT'
  658. <?php
  659. class Foo
  660. {
  661. use BarTrait;
  662. use BazTrait;
  663. const C1 = 1;
  664. const C2 = 2;
  665. public static $pubStatProp1;
  666. public static $pubStatProp2;
  667. protected static $protStatProp;
  668. private static $privStatProp;
  669. public $pubProp1;
  670. var $pubProp2;
  671. public $pubProp3;
  672. protected $protProp;
  673. private $privProp;
  674. protected function __construct() {}
  675. public function __destruct() {}
  676. public function __magicA() {}
  677. public function __magicB() {}
  678. public function __toString() {}
  679. public static function pubStatFunc1() {}
  680. static function pubStatFunc2() {}
  681. public static function pubStatFunc3() {
  682. return $this->privFunc();
  683. }
  684. protected static function protStatFunc() {}
  685. private static function privStatFunc() {}
  686. public function pubFunc1() {}
  687. function pubFunc2() {}
  688. public function pubFunc3(int $b, int $c) {
  689. $a = $b*$c;
  690. return $a % 4;
  691. }
  692. protected function protFunc() {}
  693. private function privFunc() {}
  694. }
  695. EOT
  696. ],
  697. ];
  698. }
  699. public function testWrongConfig()
  700. {
  701. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  702. $this->expectExceptionMessageRegExp('/^\[ordered_class_elements\] Invalid configuration: The option "order" .*\.$/');
  703. $this->fixer->configure(['order' => ['foo']]);
  704. }
  705. }