OrderedClassElementsFixerTest.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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\ClassNotation;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. /**
  17. * @author Gregor Harlan <gharlan@web.de>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer
  26. */
  27. final class OrderedClassElementsFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @dataProvider provideFixCases
  31. *
  32. * @param _AutogeneratedInputConfiguration $configuration
  33. */
  34. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  35. {
  36. $this->fixer->configure($configuration);
  37. $this->doTest($expected, $input);
  38. }
  39. public static function provideFixCases(): iterable
  40. {
  41. yield [
  42. <<<'EOT'
  43. <?php
  44. class Foo {}
  45. class Bar
  46. {
  47. }
  48. EOT,
  49. ];
  50. yield [
  51. <<<'EOT'
  52. <?php
  53. class Foo { const C1 = 1; protected $abc = 'abc'; public function baz($y, $z) {} private function bar1($x) { return 1; } }
  54. EOT,
  55. <<<'EOT'
  56. <?php
  57. class Foo { private function bar1($x) { return 1; } protected $abc = 'abc'; const C1 = 1; public function baz($y, $z) {} }
  58. EOT,
  59. ];
  60. yield [
  61. <<<'EOT'
  62. <?php
  63. interface FooInterface
  64. {
  65. /** const 1 */
  66. const CONST1 = 'const1';
  67. // foo
  68. // const 2
  69. const CONST2 = 'const2';
  70. public function xyz($x, $y, $z); // comment
  71. /**
  72. * @param array $a
  73. *
  74. * @return string
  75. */
  76. function abc(array &$a = null);
  77. public function def();
  78. }
  79. EOT,
  80. <<<'EOT'
  81. <?php
  82. interface FooInterface
  83. {
  84. public function xyz($x, $y, $z); // comment
  85. /** const 1 */
  86. const CONST1 = 'const1';
  87. /**
  88. * @param array $a
  89. *
  90. * @return string
  91. */
  92. function abc(array &$a = null);
  93. // foo
  94. // const 2
  95. const CONST2 = 'const2';
  96. public function def();
  97. }
  98. EOT,
  99. ];
  100. yield [
  101. <<<'EOT'
  102. <?php
  103. abstract class Foo extends FooParent implements FooInterface1, FooInterface2
  104. {
  105. use Bar;
  106. use Baz {
  107. abc as private;
  108. }
  109. const C1 = 1;
  110. /* comment for C2 */
  111. const C2 = 2;
  112. public $fooPublic;
  113. // comment 3
  114. protected $fooProtected = array(1, 2);
  115. // comment 1
  116. private $fooPrivate;
  117. protected function __construct()
  118. {
  119. }
  120. public function __destruct() {}
  121. public function __clone() {}
  122. public static function setUpBeforeClass() {}
  123. public static function tearDownAfterclass() {
  124. } /* multiline
  125. comment */
  126. protected function setUp() {}
  127. protected function assertPreConditions() {}
  128. protected function assertPostConditions() {}
  129. protected function tearDown() {}
  130. abstract public function foo1($a, $b = 1);
  131. // foo2
  132. function foo2()
  133. {
  134. return $this->foo1(1);
  135. }
  136. public static function foo3(self $foo)
  137. {
  138. return $foo->foo2();
  139. } /* comment 1 */ /* comment 2 */
  140. // comment
  141. /**
  142. * Docblock
  143. */
  144. protected function foo4(\ArrayObject $object, array $array, $a = null)
  145. {
  146. bar();
  147. if (!$a) {
  148. $a = function ($x) {
  149. var_dump($x);
  150. };
  151. }
  152. }
  153. private function foo5()
  154. {
  155. } // end foo5
  156. }
  157. EOT,
  158. <<<'EOT'
  159. <?php
  160. abstract class Foo extends FooParent implements FooInterface1, FooInterface2
  161. {
  162. // comment 1
  163. private $fooPrivate;
  164. abstract public function foo1($a, $b = 1);
  165. protected function setUp() {}
  166. protected function tearDown() {}
  167. public function __clone() {}
  168. const C1 = 1;
  169. // foo2
  170. function foo2()
  171. {
  172. return $this->foo1(1);
  173. }
  174. public static function setUpBeforeClass() {}
  175. public function __destruct() {}
  176. use Bar;
  177. public static function foo3(self $foo)
  178. {
  179. return $foo->foo2();
  180. } /* comment 1 */ /* comment 2 */
  181. // comment 3
  182. protected $fooProtected = array(1, 2);
  183. public $fooPublic;
  184. /* comment for C2 */
  185. const C2 = 2;
  186. public static function tearDownAfterclass() {
  187. } /* multiline
  188. comment */
  189. protected function assertPostConditions() {}
  190. use Baz {
  191. abc as private;
  192. }
  193. protected function assertPreConditions() {}
  194. private function foo5()
  195. {
  196. } // end foo5
  197. protected function __construct()
  198. {
  199. }
  200. // comment
  201. /**
  202. * Docblock
  203. */
  204. protected function foo4(\ArrayObject $object, array $array, $a = null)
  205. {
  206. bar();
  207. if (!$a) {
  208. $a = function ($x) {
  209. var_dump($x);
  210. };
  211. }
  212. }
  213. }
  214. EOT,
  215. ];
  216. yield [
  217. <<<'EOT'
  218. <?php
  219. class Foo
  220. {
  221. const C = 'C';
  222. public function abc() {}
  223. protected function xyz() {}
  224. }
  225. class Bar
  226. {
  227. const C = 1;
  228. public function foo($a) { return 1; }
  229. public function baz() {}
  230. }
  231. EOT,
  232. <<<'EOT'
  233. <?php
  234. class Foo
  235. {
  236. protected function xyz() {}
  237. const C = 'C';
  238. public function abc() {}
  239. }
  240. class Bar
  241. {
  242. public function foo($a) { return 1; }
  243. const C = 1;
  244. public function baz() {}
  245. }
  246. EOT,
  247. ];
  248. yield [
  249. <<<'EOT'
  250. <?php
  251. trait FooTrait
  252. {
  253. use BarTrait;
  254. use BazTrait;
  255. protected function abc() {
  256. }
  257. }
  258. EOT,
  259. <<<'EOT'
  260. <?php
  261. trait FooTrait
  262. {
  263. protected function abc() {
  264. }
  265. use BarTrait;
  266. use BazTrait;
  267. }
  268. EOT,
  269. ];
  270. yield [
  271. <<<'EOT'
  272. <?php
  273. /**
  274. * @see #4086
  275. */
  276. class ComplexStringVariableAndUseTrait
  277. {
  278. use FooTrait;
  279. public function sayHello($name)
  280. {
  281. echo "Hello, {$name}!";
  282. }
  283. }
  284. EOT,
  285. <<<'EOT'
  286. <?php
  287. /**
  288. * @see #4086
  289. */
  290. class ComplexStringVariableAndUseTrait
  291. {
  292. public function sayHello($name)
  293. {
  294. echo "Hello, {$name}!";
  295. }
  296. use FooTrait;
  297. }
  298. EOT,
  299. ];
  300. yield [
  301. <<<'EOT'
  302. <?php
  303. class Foo
  304. {
  305. use BarTrait;
  306. use BazTrait;
  307. const C1 = 1;
  308. const C2 = 2;
  309. protected static $protStatProp;
  310. public static $pubStatProp1;
  311. public $pubProp1;
  312. protected $protProp;
  313. var $pubProp2;
  314. private static $privStatProp;
  315. private $privProp;
  316. public static $pubStatProp2;
  317. public $pubProp3;
  318. protected function __construct() {}
  319. private static function privStatFunc() {}
  320. public function pubFunc1() {}
  321. public function __toString() {}
  322. protected function protFunc() {}
  323. function pubFunc2() {}
  324. public static function pubStatFunc1() {}
  325. public function pubFunc3() {}
  326. static function pubStatFunc2() {}
  327. private function privFunc() {}
  328. public static function pubStatFunc3() {}
  329. protected static function protStatFunc() {}
  330. public function __destruct() {}
  331. }
  332. EOT,
  333. <<<'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. ['order' => ['use_trait', 'constant', 'property', 'construct', 'method', 'destruct']],
  366. ];
  367. yield [
  368. <<<'EOT'
  369. <?php
  370. class Foo
  371. {
  372. public static $pubStatProp1;
  373. public function pubFunc1() {}
  374. public $pubProp1;
  375. public function __toString() {}
  376. function pubFunc2() {}
  377. public function __destruct() {}
  378. var $pubProp2;
  379. public static function pubStatFunc1() {}
  380. public function pubFunc3() {}
  381. const C1 = 1;
  382. static function pubStatFunc2() {}
  383. public static $pubStatProp2;
  384. const C2 = 2;
  385. public static function pubStatFunc3() {}
  386. public $pubProp3;
  387. protected static $protStatProp;
  388. protected function protFunc() {}
  389. protected $protProp;
  390. protected function __construct() {}
  391. protected static function protStatFunc() {}
  392. private static function privStatFunc() {}
  393. private static $privStatProp;
  394. private $privProp;
  395. private function privFunc() {}
  396. use BarTrait;
  397. use BazTrait;
  398. }
  399. EOT,
  400. <<<'EOT'
  401. <?php
  402. class Foo
  403. {
  404. private static function privStatFunc() {}
  405. protected static $protStatProp;
  406. public static $pubStatProp1;
  407. public function pubFunc1() {}
  408. use BarTrait;
  409. public $pubProp1;
  410. public function __toString() {}
  411. protected function protFunc() {}
  412. protected $protProp;
  413. function pubFunc2() {}
  414. public function __destruct() {}
  415. var $pubProp2;
  416. private static $privStatProp;
  417. use BazTrait;
  418. public static function pubStatFunc1() {}
  419. public function pubFunc3() {}
  420. private $privProp;
  421. const C1 = 1;
  422. static function pubStatFunc2() {}
  423. private function privFunc() {}
  424. public static $pubStatProp2;
  425. protected function __construct() {}
  426. const C2 = 2;
  427. public static function pubStatFunc3() {}
  428. public $pubProp3;
  429. protected static function protStatFunc() {}
  430. }
  431. EOT,
  432. ['order' => ['public', 'protected', 'private']],
  433. ];
  434. yield [
  435. <<<'EOT'
  436. <?php
  437. class Foo
  438. {
  439. use BarTrait;
  440. use BazTrait;
  441. const C1 = 1;
  442. const C2 = 2;
  443. public static $pubStatProp1;
  444. public static $pubStatProp2;
  445. protected static $protStatProp;
  446. private static $privStatProp;
  447. public $pubProp1;
  448. var $pubProp2;
  449. public $pubProp3;
  450. protected $protProp;
  451. private $privProp;
  452. protected function __construct() {}
  453. public function __destruct() {}
  454. public function __toString() {}
  455. public static function pubStatFunc1() {}
  456. static function pubStatFunc2() {}
  457. public static function pubStatFunc3() {}
  458. protected static function protStatFunc() {}
  459. private static function privStatFunc() {}
  460. public function pubFunc1() {}
  461. function pubFunc2() {}
  462. public function pubFunc3() {}
  463. protected function protFunc() {}
  464. private function privFunc() {}
  465. }
  466. EOT,
  467. <<<'EOT'
  468. <?php
  469. class Foo
  470. {
  471. private static function privStatFunc() {}
  472. protected static $protStatProp;
  473. public static $pubStatProp1;
  474. public function pubFunc1() {}
  475. use BarTrait;
  476. public $pubProp1;
  477. public function __toString() {}
  478. protected function protFunc() {}
  479. protected $protProp;
  480. function pubFunc2() {}
  481. public function __destruct() {}
  482. var $pubProp2;
  483. private static $privStatProp;
  484. use BazTrait;
  485. public static function pubStatFunc1() {}
  486. public function pubFunc3() {}
  487. private $privProp;
  488. const C1 = 1;
  489. static function pubStatFunc2() {}
  490. private function privFunc() {}
  491. public static $pubStatProp2;
  492. protected function __construct() {}
  493. const C2 = 2;
  494. public static function pubStatFunc3() {}
  495. public $pubProp3;
  496. protected static function protStatFunc() {}
  497. }
  498. EOT,
  499. [
  500. 'order' => [
  501. 'use_trait',
  502. 'constant',
  503. 'property_public_static',
  504. 'property_protected_static',
  505. 'property_private_static',
  506. 'property_public',
  507. 'property_protected',
  508. 'property_private',
  509. 'construct',
  510. 'destruct',
  511. 'magic',
  512. 'method_public_static',
  513. 'method_protected_static',
  514. 'method_private_static',
  515. 'method_public',
  516. 'method_protected',
  517. 'method_private',
  518. ],
  519. ],
  520. ];
  521. yield [
  522. <<<'EOT'
  523. <?php
  524. abstract class Foo
  525. {
  526. use BarTrait;
  527. use BazTrait;
  528. const C1 = 1;
  529. const C2 = 2;
  530. protected static $protStatProp;
  531. public static $pubStatProp1;
  532. public $pubProp1;
  533. protected $protProp;
  534. var $pubProp2;
  535. private static $privStatProp;
  536. private $privProp;
  537. public static $pubStatProp2;
  538. public $pubProp3;
  539. protected function __construct() {}
  540. abstract public function absPubFunc();
  541. private static function privStatFunc() {}
  542. public function pubFunc1() {}
  543. abstract protected function absProtFunc();
  544. public function __toString() {}
  545. protected function protFunc() {}
  546. function pubFunc2() {}
  547. abstract protected static function absProtStatFunc();
  548. public static function pubStatFunc1() {}
  549. public function pubFunc3() {}
  550. static function pubStatFunc2() {}
  551. private function privFunc() {}
  552. abstract public static function absPubStatFunc();
  553. public static function pubStatFunc3() {}
  554. protected static function protStatFunc() {}
  555. public function __destruct() {}
  556. }
  557. EOT,
  558. <<<'EOT'
  559. <?php
  560. abstract class Foo
  561. {
  562. abstract public function absPubFunc();
  563. private static function privStatFunc() {}
  564. protected static $protStatProp;
  565. public static $pubStatProp1;
  566. public function pubFunc1() {}
  567. abstract protected function absProtFunc();
  568. use BarTrait;
  569. public $pubProp1;
  570. public function __toString() {}
  571. protected function protFunc() {}
  572. protected $protProp;
  573. function pubFunc2() {}
  574. abstract protected static function absProtStatFunc();
  575. public function __destruct() {}
  576. var $pubProp2;
  577. private static $privStatProp;
  578. use BazTrait;
  579. public static function pubStatFunc1() {}
  580. public function pubFunc3() {}
  581. private $privProp;
  582. const C1 = 1;
  583. static function pubStatFunc2() {}
  584. private function privFunc() {}
  585. public static $pubStatProp2;
  586. abstract public static function absPubStatFunc();
  587. protected function __construct() {}
  588. const C2 = 2;
  589. public static function pubStatFunc3() {}
  590. public $pubProp3;
  591. protected static function protStatFunc() {}
  592. }
  593. EOT,
  594. ['order' => ['use_trait', 'constant', 'property', 'construct', 'method', 'destruct']],
  595. ];
  596. yield [
  597. <<<'EOT'
  598. <?php
  599. abstract class Foo
  600. {
  601. abstract public function absPubFunc();
  602. public static $pubStatProp1;
  603. public function pubFunc1() {}
  604. public $pubProp1;
  605. public function __toString() {}
  606. function pubFunc2() {}
  607. public function __destruct() {}
  608. var $pubProp2;
  609. public static function pubStatFunc1() {}
  610. public function pubFunc3() {}
  611. const C1 = 1;
  612. static function pubStatFunc2() {}
  613. public static $pubStatProp2;
  614. abstract public static function absPubStatFunc();
  615. const C2 = 2;
  616. public static function pubStatFunc3() {}
  617. public $pubProp3;
  618. protected static $protStatProp;
  619. abstract protected function absProtFunc();
  620. protected function protFunc() {}
  621. protected $protProp;
  622. abstract protected static function absProtStatFunc();
  623. protected function __construct() {}
  624. protected static function protStatFunc() {}
  625. private static function privStatFunc() {}
  626. private static $privStatProp;
  627. private $privProp;
  628. private function privFunc() {}
  629. use BarTrait;
  630. use BazTrait;
  631. }
  632. EOT,
  633. <<<'EOT'
  634. <?php
  635. abstract class Foo
  636. {
  637. abstract public function absPubFunc();
  638. private static function privStatFunc() {}
  639. protected static $protStatProp;
  640. public static $pubStatProp1;
  641. public function pubFunc1() {}
  642. abstract protected function absProtFunc();
  643. use BarTrait;
  644. public $pubProp1;
  645. public function __toString() {}
  646. protected function protFunc() {}
  647. protected $protProp;
  648. function pubFunc2() {}
  649. abstract protected static function absProtStatFunc();
  650. public function __destruct() {}
  651. var $pubProp2;
  652. private static $privStatProp;
  653. use BazTrait;
  654. public static function pubStatFunc1() {}
  655. public function pubFunc3() {}
  656. private $privProp;
  657. const C1 = 1;
  658. static function pubStatFunc2() {}
  659. private function privFunc() {}
  660. public static $pubStatProp2;
  661. abstract public static function absPubStatFunc();
  662. protected function __construct() {}
  663. const C2 = 2;
  664. public static function pubStatFunc3() {}
  665. public $pubProp3;
  666. protected static function protStatFunc() {}
  667. }
  668. EOT,
  669. ['order' => ['public', 'protected', 'private']],
  670. ];
  671. yield [
  672. <<<'EOT'
  673. <?php
  674. abstract class Foo
  675. {
  676. use BarTrait;
  677. use BazTrait;
  678. const C1 = 1;
  679. const C2 = 2;
  680. public static $pubStatProp1;
  681. public static $pubStatProp2;
  682. protected static $protStatProp;
  683. private static $privStatProp;
  684. public $pubProp1;
  685. var $pubProp2;
  686. public $pubProp3;
  687. protected $protProp;
  688. private $privProp;
  689. protected function __construct() {}
  690. public function __destruct() {}
  691. public function __toString() {}
  692. public static function pubStatFunc1() {}
  693. static function pubStatFunc2() {}
  694. public static function pubStatFunc3() {}
  695. abstract public static function absPubStatFunc();
  696. protected static function protStatFunc() {}
  697. abstract protected static function absProtStatFunc();
  698. private static function privStatFunc() {}
  699. public function pubFunc1() {}
  700. function pubFunc2() {}
  701. public function pubFunc3() {}
  702. abstract public function absPubFunc();
  703. protected function protFunc() {}
  704. abstract protected function absProtFunc();
  705. private function privFunc() {}
  706. }
  707. EOT,
  708. <<<'EOT'
  709. <?php
  710. abstract class Foo
  711. {
  712. abstract public function absPubFunc();
  713. private static function privStatFunc() {}
  714. protected static $protStatProp;
  715. public static $pubStatProp1;
  716. public function pubFunc1() {}
  717. abstract protected function absProtFunc();
  718. use BarTrait;
  719. public $pubProp1;
  720. public function __toString() {}
  721. protected function protFunc() {}
  722. protected $protProp;
  723. function pubFunc2() {}
  724. abstract protected static function absProtStatFunc();
  725. public function __destruct() {}
  726. var $pubProp2;
  727. private static $privStatProp;
  728. use BazTrait;
  729. public static function pubStatFunc1() {}
  730. public function pubFunc3() {}
  731. private $privProp;
  732. const C1 = 1;
  733. static function pubStatFunc2() {}
  734. private function privFunc() {}
  735. public static $pubStatProp2;
  736. abstract public static function absPubStatFunc();
  737. protected function __construct() {}
  738. const C2 = 2;
  739. public static function pubStatFunc3() {}
  740. public $pubProp3;
  741. protected static function protStatFunc() {}
  742. }
  743. EOT,
  744. [
  745. 'order' => [
  746. 'use_trait',
  747. 'constant',
  748. 'property_public_static',
  749. 'property_protected_static',
  750. 'property_private_static',
  751. 'property_public',
  752. 'property_protected',
  753. 'property_private',
  754. 'construct',
  755. 'destruct',
  756. 'magic',
  757. 'method_public_static',
  758. 'method_public_abstract_static',
  759. 'method_protected_static',
  760. 'method_protected_abstract_static',
  761. 'method_private_static',
  762. 'method_public',
  763. 'method_public_abstract',
  764. 'method_protected',
  765. 'method_protected_abstract',
  766. 'method_private',
  767. ],
  768. ],
  769. ];
  770. yield [
  771. <<<'EOT'
  772. <?php
  773. abstract class Foo
  774. {
  775. public function test2(){}
  776. public abstract function test1();
  777. }
  778. EOT,
  779. <<<'EOT'
  780. <?php
  781. abstract class Foo
  782. {
  783. public abstract function test1();
  784. public function test2(){}
  785. }
  786. EOT,
  787. [
  788. 'order' => [
  789. 'method_public',
  790. 'method_abstract',
  791. ],
  792. ],
  793. ];
  794. yield [
  795. <<<'EOT'
  796. <?php
  797. abstract class Foo
  798. {
  799. protected function __construct() {}
  800. public function __invoke() {}
  801. public function __destruct() {}
  802. public static function pubStatFunc1() {}
  803. static function pubStatFunc2() {}
  804. public static function pubStatFunc3() {}
  805. private function custom1() {}
  806. abstract public static function absPubStatFunc();
  807. protected static function protStatFunc() {}
  808. abstract protected static function absProtStatFunc();
  809. private static function privStatFunc() {}
  810. public function pubFunc1() {}
  811. function pubFunc2() {}
  812. public function pubFunc3() {}
  813. abstract public function custom3();
  814. abstract public function absPubFunc();
  815. protected function protFunc() {}
  816. abstract protected function absProtFunc();
  817. private function privFunc() {}
  818. protected static function custom2() {}
  819. public function __get($prop) {}
  820. public function __toString() {}
  821. }
  822. EOT,
  823. <<<'EOT'
  824. <?php
  825. abstract class Foo
  826. {
  827. public function __get($prop) {}
  828. private function custom1() {}
  829. abstract public function absPubFunc();
  830. private static function privStatFunc() {}
  831. public function pubFunc1() {}
  832. abstract protected function absProtFunc();
  833. public function __toString() {}
  834. protected function protFunc() {}
  835. function pubFunc2() {}
  836. abstract protected static function absProtStatFunc();
  837. public function __destruct() {}
  838. public static function pubStatFunc1() {}
  839. public function __invoke() {}
  840. public function pubFunc3() {}
  841. static function pubStatFunc2() {}
  842. private function privFunc() {}
  843. abstract public static function absPubStatFunc();
  844. protected function __construct() {}
  845. public static function pubStatFunc3() {}
  846. protected static function protStatFunc() {}
  847. abstract public function custom3();
  848. protected static function custom2() {}
  849. }
  850. EOT,
  851. [
  852. 'order' => [
  853. 'construct',
  854. 'method:__invoke',
  855. 'destruct',
  856. 'method_public_static',
  857. 'method:custom1',
  858. 'method_public_abstract_static',
  859. 'method_protected_static',
  860. 'method_protected_abstract_static',
  861. 'method_private_static',
  862. 'method_public',
  863. 'method:custom3',
  864. 'method_public_abstract',
  865. 'method_protected',
  866. 'method_protected_abstract',
  867. 'method_private',
  868. 'method:custom2',
  869. 'magic',
  870. ],
  871. ],
  872. ];
  873. yield [
  874. <<<'EOT'
  875. <?php
  876. abstract class Foo
  877. {
  878. public function pub() {}
  879. public function bar() {}
  880. public function __toString() {}
  881. }
  882. EOT,
  883. <<<'EOT'
  884. <?php
  885. abstract class Foo
  886. {
  887. public function __toString() {}
  888. public function pub() {}
  889. public function bar() {}
  890. }
  891. EOT,
  892. [
  893. 'order' => [
  894. 'method:foo',
  895. 'method_public',
  896. 'method:bar',
  897. 'method:baz',
  898. 'magic',
  899. ],
  900. ],
  901. ];
  902. yield [
  903. <<<'EOT'
  904. <?php
  905. class Example
  906. {
  907. public static $pubStatProp1;
  908. public static $pubStatProp2;
  909. public function A(){}
  910. public function B1(){}
  911. public function B2(){}
  912. public function C(){}
  913. public function C1(){}
  914. public function D(){}
  915. private function E(){}
  916. }
  917. EOT,
  918. <<<'EOT'
  919. <?php
  920. class Example
  921. {
  922. public function D(){}
  923. public static $pubStatProp2;
  924. public function B1(){}
  925. public function B2(){}
  926. private function E(){}
  927. public static $pubStatProp1;
  928. public function A(){}
  929. public function C(){}
  930. public function C1(){}
  931. }
  932. EOT,
  933. [
  934. 'order' => [
  935. 'property_public_static',
  936. 'method_public',
  937. 'method_private',
  938. ],
  939. 'sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA,
  940. ],
  941. ];
  942. yield [
  943. <<<'EOT'
  944. <?php
  945. class Foo
  946. {
  947. use BarTrait;
  948. use BazTrait;
  949. const C1 = 1;
  950. const C2 = 2;
  951. public static $pubStatProp1;
  952. public static $pubStatProp2;
  953. protected static $protStatProp;
  954. private static $privStatProp;
  955. public $pubProp1;
  956. var $pubProp2;
  957. public $pubProp3;
  958. protected $protProp;
  959. private $privProp;
  960. protected function __construct() {}
  961. public function __destruct() {}
  962. public function __magicA() {}
  963. public function __magicB() {}
  964. public function __toString() {}
  965. public static function pubStatFunc1() {}
  966. static function pubStatFunc2() {}
  967. public static function pubStatFunc3() {
  968. return $this->privFunc();
  969. }
  970. protected static function protStatFunc() {}
  971. private static function privStatFunc() {}
  972. public function pubFunc1() {}
  973. function pubFunc2() {}
  974. public function pubFunc3(int $b, int $c) {
  975. $a = $b*$c;
  976. return $a % 4;
  977. }
  978. protected function protFunc() {}
  979. private function privFunc() {}
  980. }
  981. EOT,
  982. <<<'EOT'
  983. <?php
  984. class Foo
  985. {
  986. private static function privStatFunc() {}
  987. protected static $protStatProp;
  988. use BazTrait;
  989. public static $pubStatProp2;
  990. public $pubProp3;
  991. use BarTrait;
  992. public function __toString() {}
  993. protected function protFunc() {}
  994. protected $protProp;
  995. function pubFunc2() {}
  996. public $pubProp1;
  997. public function __destruct() {}
  998. var $pubProp2;
  999. public function __magicB() {}
  1000. const C2 = 2;
  1001. public static $pubStatProp1;
  1002. public function __magicA() {}
  1003. private static $privStatProp;
  1004. static function pubStatFunc2() {}
  1005. public function pubFunc3(int $b, int $c) {
  1006. $a = $b*$c;
  1007. return $a % 4;
  1008. }
  1009. private $privProp;
  1010. const C1 = 1;
  1011. public static function pubStatFunc3() {
  1012. return $this->privFunc();
  1013. }
  1014. public function pubFunc1() {}
  1015. public static function pubStatFunc1() {}
  1016. private function privFunc() {}
  1017. protected function __construct() {}
  1018. protected static function protStatFunc() {}
  1019. }
  1020. EOT,
  1021. [
  1022. 'order' => [
  1023. 'use_trait',
  1024. 'constant',
  1025. 'property_public_static',
  1026. 'property_protected_static',
  1027. 'property_private_static',
  1028. 'property_public',
  1029. 'property_protected',
  1030. 'property_private',
  1031. 'construct',
  1032. 'destruct',
  1033. 'magic',
  1034. 'method_public_static',
  1035. 'method_protected_static',
  1036. 'method_private_static',
  1037. 'method_public',
  1038. 'method_protected',
  1039. 'method_private',
  1040. ],
  1041. 'sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA,
  1042. ],
  1043. ];
  1044. yield [
  1045. <<<'EOT'
  1046. <?php
  1047. abstract class Foo
  1048. {
  1049. use BarTrait;
  1050. use BazTrait;
  1051. const C1 = 1;
  1052. const C2 = 2;
  1053. public static $pubStatProp1;
  1054. public static $pubStatProp2;
  1055. protected static $protStatProp;
  1056. private static $privStatProp;
  1057. public $pubProp1;
  1058. var $pubProp2;
  1059. public $pubProp3;
  1060. protected $protProp;
  1061. private $privProp;
  1062. protected function __construct() {}
  1063. public function __destruct() {}
  1064. public function __magicA() {}
  1065. public function __magicB() {}
  1066. public function __toString() {}
  1067. public static function pubStatFunc1() {}
  1068. static function pubStatFunc2() {}
  1069. public static function pubStatFunc3() {
  1070. return $this->privFunc();
  1071. }
  1072. abstract public static function absPubStatFunc1();
  1073. protected static function protStatFunc() {}
  1074. abstract protected static function absProtStatFunc();
  1075. private static function privStatFunc() {}
  1076. public function pubFunc1() {}
  1077. function pubFunc2() {}
  1078. public function pubFunc3(int $b, int $c) {
  1079. $a = $b*$c;
  1080. return $a % 4;
  1081. }
  1082. abstract public function absPubFunc();
  1083. protected function protFunc() {}
  1084. abstract protected function absProtFunc();
  1085. private function privFunc() {}
  1086. }
  1087. EOT,
  1088. <<<'EOT'
  1089. <?php
  1090. abstract class Foo
  1091. {
  1092. private static function privStatFunc() {}
  1093. protected static $protStatProp;
  1094. use BazTrait;
  1095. abstract public function absPubFunc();
  1096. public static $pubStatProp2;
  1097. public $pubProp3;
  1098. use BarTrait;
  1099. public function __toString() {}
  1100. protected function protFunc() {}
  1101. protected $protProp;
  1102. function pubFunc2() {}
  1103. public $pubProp1;
  1104. abstract protected static function absProtStatFunc();
  1105. public function __destruct() {}
  1106. var $pubProp2;
  1107. public function __magicB() {}
  1108. const C2 = 2;
  1109. public static $pubStatProp1;
  1110. public function __magicA() {}
  1111. private static $privStatProp;
  1112. static function pubStatFunc2() {}
  1113. public function pubFunc3(int $b, int $c) {
  1114. $a = $b*$c;
  1115. return $a % 4;
  1116. }
  1117. private $privProp;
  1118. const C1 = 1;
  1119. abstract protected function absProtFunc();
  1120. public static function pubStatFunc3() {
  1121. return $this->privFunc();
  1122. }
  1123. public function pubFunc1() {}
  1124. public static function pubStatFunc1() {}
  1125. private function privFunc() {}
  1126. protected function __construct() {}
  1127. protected static function protStatFunc() {}
  1128. abstract public static function absPubStatFunc1();
  1129. }
  1130. EOT,
  1131. [
  1132. 'order' => [
  1133. 'use_trait',
  1134. 'constant',
  1135. 'property_public_static',
  1136. 'property_protected_static',
  1137. 'property_private_static',
  1138. 'property_public',
  1139. 'property_protected',
  1140. 'property_private',
  1141. 'construct',
  1142. 'destruct',
  1143. 'magic',
  1144. 'method_public_static',
  1145. 'method_public_abstract_static',
  1146. 'method_protected_static',
  1147. 'method_protected_abstract_static',
  1148. 'method_private_static',
  1149. 'method_public',
  1150. 'method_public_abstract',
  1151. 'method_protected',
  1152. 'method_protected_abstract',
  1153. 'method_private',
  1154. ],
  1155. 'sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA,
  1156. ],
  1157. ];
  1158. yield [
  1159. <<<'EOT'
  1160. <?php
  1161. class Foo
  1162. {
  1163. const C2 = 2;
  1164. public const C1 = 1;
  1165. public const C3 = 3;
  1166. protected const C4 = 4;
  1167. private const C5 = 5;
  1168. }
  1169. EOT,
  1170. <<<'EOT'
  1171. <?php
  1172. class Foo
  1173. {
  1174. private const C5 = 5;
  1175. const C2 = 2;
  1176. public const C1 = 1;
  1177. protected const C4 = 4;
  1178. public const C3 = 3;
  1179. }
  1180. EOT,
  1181. ];
  1182. yield [
  1183. <<<'EOT'
  1184. <?php
  1185. class Foo
  1186. {
  1187. public const C1 = 1;
  1188. const C2 = 2;
  1189. public const C3b = 3;
  1190. protected const C4a = 4;
  1191. private const C5 = 5;
  1192. }
  1193. EOT,
  1194. <<<'EOT'
  1195. <?php
  1196. class Foo
  1197. {
  1198. private const C5 = 5;
  1199. const C2 = 2;
  1200. public const C1 = 1;
  1201. protected const C4a = 4;
  1202. public const C3b = 3;
  1203. }
  1204. EOT,
  1205. ['sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA],
  1206. ];
  1207. yield [
  1208. <<<'EOT'
  1209. <?php
  1210. class Foo
  1211. {
  1212. const A_ = 1;
  1213. const AA = 2;
  1214. const Ab = 3;
  1215. }
  1216. EOT,
  1217. <<<'EOT'
  1218. <?php
  1219. class Foo
  1220. {
  1221. const AA = 2;
  1222. const Ab = 3;
  1223. const A_ = 1;
  1224. }
  1225. EOT,
  1226. ['sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA],
  1227. ];
  1228. yield [
  1229. <<<'EOT'
  1230. <?php
  1231. class Foo
  1232. {
  1233. const AA = 2;
  1234. const A_ = 1;
  1235. const Ab = 3;
  1236. }
  1237. EOT,
  1238. <<<'EOT'
  1239. <?php
  1240. class Foo
  1241. {
  1242. const AA = 2;
  1243. const Ab = 3;
  1244. const A_ = 1;
  1245. }
  1246. EOT,
  1247. ['sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA, 'case_sensitive' => true],
  1248. ];
  1249. // test with no candidate
  1250. $template = '<?php
  1251. class TestClass
  1252. {
  1253. public function %s(){}
  1254. public function %s(){}
  1255. }';
  1256. foreach (['__construct', '__destruct', '__sleep', 'abc'] as $methodName) {
  1257. yield [
  1258. \sprintf($template, $methodName, 'z'),
  1259. \sprintf($template, 'z', $methodName),
  1260. ['order' => ['use_trait'], 'sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA],
  1261. ];
  1262. }
  1263. }
  1264. /**
  1265. * @param _AutogeneratedInputConfiguration $configuration
  1266. *
  1267. * @dataProvider provideFixPre80Cases
  1268. */
  1269. public function testFixPre80(string $expected, ?string $input = null, array $configuration = []): void
  1270. {
  1271. $this->fixer->configure($configuration);
  1272. $this->doTest($expected, $input);
  1273. }
  1274. public static function provideFixPre80Cases(): iterable
  1275. {
  1276. yield [
  1277. '<?php
  1278. class Foo {
  1279. public iterable $baz;
  1280. var ? Foo\Bar $qux;
  1281. protected string $bar;
  1282. private ?int $foo;
  1283. }',
  1284. '<?php
  1285. class Foo {
  1286. private ?int $foo;
  1287. protected string $bar;
  1288. public iterable $baz;
  1289. var ? Foo\Bar $qux;
  1290. }',
  1291. ];
  1292. yield [
  1293. '<?php
  1294. class Foo {
  1295. public string $bar;
  1296. public array $baz;
  1297. public ?int $foo;
  1298. public ? Foo\Bar $qux;
  1299. }',
  1300. '<?php
  1301. class Foo {
  1302. public array $baz;
  1303. public ? Foo\Bar $qux;
  1304. public string $bar;
  1305. public ?int $foo;
  1306. }',
  1307. [
  1308. 'sort_algorithm' => OrderedClassElementsFixer::SORT_ALPHA,
  1309. ],
  1310. ];
  1311. }
  1312. public function testInvalidConfiguration(): void
  1313. {
  1314. $this->expectException(InvalidFixerConfigurationException::class);
  1315. $this->expectExceptionMessageMatches('/^\[ordered_class_elements\] Invalid configuration: The option "order" .*\.$/');
  1316. $this->fixer->configure(['order' => ['foo']]);
  1317. }
  1318. /**
  1319. * @dataProvider provideFix80Cases
  1320. *
  1321. * @requires PHP 8.0
  1322. */
  1323. public function testFix80(string $expected, string $input): void
  1324. {
  1325. $this->doTest($expected, $input);
  1326. }
  1327. /**
  1328. * @return iterable<array{string, string}>
  1329. */
  1330. public static function provideFix80Cases(): iterable
  1331. {
  1332. yield [
  1333. '<?php
  1334. trait TestTrait
  1335. {
  1336. abstract static public function abstractStaticPublic();
  1337. abstract private function abstractPrivate();
  1338. abstract static private function abstractStaticPrivate();
  1339. }
  1340. ',
  1341. '<?php
  1342. trait TestTrait
  1343. {
  1344. abstract private function abstractPrivate();
  1345. abstract static private function abstractStaticPrivate();
  1346. abstract static public function abstractStaticPublic();
  1347. }
  1348. ',
  1349. ];
  1350. yield [
  1351. '<?php class Foo
  1352. {
  1353. #[PublicBarAttribute0]
  1354. public int $bar = 1;
  1355. #[PublicAttribute0]
  1356. #[PublicAttribute1]
  1357. #[PublicAttribute2]
  1358. public function fooPublic()
  1359. {
  1360. }
  1361. #[PrivateAttribute0]
  1362. private function fooPrivate()
  1363. {
  1364. }
  1365. }',
  1366. '<?php class Foo
  1367. {
  1368. #[PrivateAttribute0]
  1369. private function fooPrivate()
  1370. {
  1371. }
  1372. #[PublicBarAttribute0]
  1373. public int $bar = 1;
  1374. #[PublicAttribute0]
  1375. #[PublicAttribute1]
  1376. #[PublicAttribute2]
  1377. public function fooPublic()
  1378. {
  1379. }
  1380. }',
  1381. ];
  1382. }
  1383. /**
  1384. * @param _AutogeneratedInputConfiguration $configuration
  1385. *
  1386. * @dataProvider provideFix81Cases
  1387. *
  1388. * @requires PHP 8.1
  1389. */
  1390. public function testFix81(string $expected, ?string $input = null, array $configuration = []): void
  1391. {
  1392. $this->fixer->configure($configuration);
  1393. $this->doTest($expected, $input);
  1394. }
  1395. public static function provideFix81Cases(): iterable
  1396. {
  1397. yield [
  1398. '<?php
  1399. class A
  1400. {
  1401. readonly public string $publicProp0;
  1402. public readonly string $publicProp1;
  1403. public string $pubProp2;
  1404. protected readonly string $protectedProp0;
  1405. readonly protected string $protectedProp1;
  1406. readonly private string $privateProp0;
  1407. private readonly string $privateProp1;
  1408. }
  1409. ',
  1410. '<?php
  1411. class A
  1412. {
  1413. public string $pubProp2;
  1414. readonly public string $publicProp0;
  1415. public readonly string $publicProp1;
  1416. private readonly string $privateProp1;
  1417. readonly private string $privateProp0;
  1418. protected readonly string $protectedProp0;
  1419. readonly protected string $protectedProp1;
  1420. }
  1421. ',
  1422. ['order' => ['property_public_readonly', 'property_public', 'property_protected_readonly', 'property_private_readonly'], 'sort_algorithm' => 'alpha'],
  1423. ];
  1424. yield [
  1425. '<?php
  1426. enum A: int
  1427. {
  1428. case Foo = 1;
  1429. case Bar = 2;
  1430. private const C1 = 1;
  1431. function qux() {
  1432. switch (true) {
  1433. case 1: break;
  1434. }
  1435. }
  1436. }
  1437. ',
  1438. '<?php
  1439. enum A: int
  1440. {
  1441. private const C1 = 1;
  1442. case Foo = 1;
  1443. function qux() {
  1444. switch (true) {
  1445. case 1: break;
  1446. }
  1447. }
  1448. case Bar = 2;
  1449. }
  1450. ',
  1451. ];
  1452. }
  1453. /**
  1454. * @dataProvider provideFix82Cases
  1455. *
  1456. * @requires PHP 8.2
  1457. */
  1458. public function testFix82(string $expected, ?string $input = null): void
  1459. {
  1460. $this->doTest($expected, $input);
  1461. }
  1462. /**
  1463. * @return iterable<array{string, string}>
  1464. */
  1465. public static function provideFix82Cases(): iterable
  1466. {
  1467. yield [
  1468. '<?php trait Foo { const C1 = 1; protected $abc = "abc"; }',
  1469. '<?php trait Foo { protected $abc = "abc"; const C1 = 1; }',
  1470. ];
  1471. }
  1472. }