ClassAttributesSeparationFixerTest.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  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\ClassAttributesSeparationFixer;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. use PhpCsFixer\WhitespacesFixerConfig;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer
  26. */
  27. final class ClassAttributesSeparationFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @param _AutogeneratedInputConfiguration $configuration
  31. *
  32. * @dataProvider provideFixCases
  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. '<?php
  43. class Sample
  44. {
  45. private $a; // foo
  46. /** second in a hour */
  47. private $b;
  48. }
  49. ',
  50. '<?php
  51. class Sample
  52. {private $a; // foo
  53. /** second in a hour */
  54. private $b;
  55. }
  56. ',
  57. ];
  58. yield 'empty class' => [
  59. '<?php class Foo {}',
  60. ];
  61. yield 'simple top class' => [
  62. '<?php class A {
  63. public function Foo(){}
  64. }',
  65. '<?php class A {public function Foo(){}}',
  66. ];
  67. yield 'comment' => [
  68. '<?php class A {
  69. /* function comment */
  70. public function Bar(){}
  71. }',
  72. '<?php class A {/* function comment */public function Bar(){}
  73. }',
  74. ];
  75. yield 'comment, multiple lines' => [
  76. '<?php class A {
  77. /* some comment */
  78. public function Bar(){}
  79. }',
  80. '<?php class A {
  81. /* some comment */
  82. public function Bar(){}
  83. }',
  84. ];
  85. yield 'simple PHPDoc case' => [
  86. '<?php class Foo
  87. {
  88. /** Doc 1 */
  89. public function A(){}
  90. /** Doc 2 */
  91. public function B(){}
  92. }',
  93. '<?php class Foo
  94. {/** Doc 1 */public function A(){}
  95. /** Doc 2 */
  96. public function B(){}
  97. }',
  98. ];
  99. yield 'add a newline at the end of a class with trait group' => [
  100. '<?php class A
  101. {
  102. use Bar {
  103. __construct as barConstruct;
  104. baz as barBaz;
  105. }
  106. }',
  107. '<?php class A
  108. {
  109. use Bar {
  110. __construct as barConstruct;
  111. baz as barBaz;
  112. }}',
  113. ];
  114. yield 'add a newline at the end of a class with trait' => [
  115. '<?php class A
  116. {
  117. use A\B\C;
  118. }',
  119. '<?php class A
  120. {
  121. use A\B\C;}',
  122. ];
  123. yield 'removes extra lines at the end of an interface' => [
  124. '<?php interface F
  125. {
  126. public function A();
  127. }',
  128. '<?php interface F
  129. {
  130. public function A();
  131. }',
  132. ];
  133. yield 'removes extra lines at the end of an abstract class' => [
  134. '<?php abstract class F
  135. {
  136. public abstract function A();
  137. }',
  138. '<?php abstract class F
  139. {
  140. public abstract function A();
  141. }',
  142. ];
  143. yield 'add a newline at the end of a class' => [
  144. '<?php class A
  145. {
  146. public function A(){}
  147. }',
  148. '<?php class A
  149. {
  150. public function A(){}}',
  151. ];
  152. yield 'add a newline at the end of a class: with comments' => [
  153. '<?php class A
  154. {
  155. public const A = 1; /* foo */ /* bar */
  156. }',
  157. '<?php class A
  158. {
  159. public const A = 1; /* foo */ /* bar */}',
  160. ];
  161. yield 'add a newline at the end of a class: with comments with trailing space' => [
  162. '<?php class A
  163. {
  164. public const A = 1; /* foo */ /* bar */
  165. }',
  166. '<?php class A
  167. {
  168. public const A = 1; /* foo */ /* bar */ }',
  169. ];
  170. $to = $from = '<?php ';
  171. for ($i = 0; $i < 15; ++$i) {
  172. $from .= \sprintf('class A%d{public function GA%d(){return new class {public function B6B%d(){}};}public function otherFunction%d(){}}', $i, $i, $i, $i);
  173. $to .= \sprintf("class A%d{\npublic function GA%d(){return new class {\npublic function B6B%d(){}\n};}\n\npublic function otherFunction%d(){}\n}", $i, $i, $i, $i);
  174. }
  175. yield [$to, $from];
  176. yield [
  177. '<?php $a = new class {
  178. public function H(){}
  179. public function B7(){}
  180. private function C(){}
  181. };',
  182. '<?php $a = new class {
  183. public function H(){}
  184. public function B7(){}
  185. private function C(){}
  186. };',
  187. ];
  188. yield [
  189. '<?php
  190. class A
  191. {
  192. public function getFilter()
  193. {
  194. return new class () implements FilterInterface {
  195. private $d = 123;
  196. public function pass($a, $b) {
  197. echo $a;
  198. }
  199. public $e = 5;
  200. };}
  201. }
  202. ',
  203. '<?php
  204. class A
  205. {public function getFilter()
  206. {
  207. return new class () implements FilterInterface {private $d = 123;
  208. public function pass($a, $b) {
  209. echo $a;
  210. }
  211. public $e = 5;};}
  212. }
  213. ',
  214. ];
  215. yield ['<?php
  216. class SomeClass1
  217. {
  218. // This comment
  219. // is multiline.
  220. public function echoA()
  221. {
  222. echo "a";
  223. }
  224. }
  225. '];
  226. yield [
  227. '<?php
  228. class SomeClass2
  229. {
  230. // This comment
  231. /* is multiline. */
  232. public function echoA()
  233. {
  234. echo "a";
  235. }
  236. }
  237. ',
  238. '<?php
  239. class SomeClass2
  240. {
  241. // This comment
  242. /* is multiline. */public function echoA()
  243. {
  244. echo "a";
  245. }
  246. }
  247. ',
  248. ];
  249. yield [
  250. '<?php
  251. class SomeClass3
  252. {
  253. // This comment
  254. // is multiline.
  255. public function echoA()
  256. {
  257. echo "a";
  258. }
  259. }
  260. ', ];
  261. yield [
  262. '<?php
  263. class SomeClass1
  264. {
  265. private $a; //
  266. public function methodA()
  267. {
  268. }
  269. private $b;
  270. //
  271. public function methodB()
  272. {
  273. }
  274. // C
  275. public function methodC()
  276. {
  277. }
  278. // D
  279. public function methodD()
  280. {
  281. }
  282. /* E */
  283. public function methodE()
  284. {
  285. }
  286. /* F */
  287. public function methodF()
  288. {
  289. }
  290. }
  291. ',
  292. '<?php
  293. class SomeClass1
  294. {
  295. private $a; //
  296. public function methodA()
  297. {
  298. }
  299. private $b;
  300. //
  301. public function methodB()
  302. {
  303. }
  304. // C
  305. public function methodC()
  306. {
  307. }
  308. // D
  309. public function methodD()
  310. {
  311. }
  312. /* E */
  313. public function methodE()
  314. {
  315. }
  316. /* F */
  317. public function methodF()
  318. {
  319. }
  320. }
  321. ', ];
  322. yield ['<?php
  323. class SomeClass
  324. {
  325. // comment
  326. public function echoA()
  327. {
  328. echo "a";
  329. }
  330. }
  331. '];
  332. yield ['<?php
  333. class SomeClass
  334. {
  335. // This comment
  336. // is multiline.
  337. public function echoA()
  338. {
  339. echo "a";
  340. }
  341. }
  342. '];
  343. yield [
  344. '<?php
  345. class SomeClass
  346. {
  347. // comment
  348. public function echoA()
  349. {
  350. echo "a";
  351. }
  352. }
  353. ',
  354. '<?php
  355. class SomeClass
  356. {
  357. // comment
  358. public function echoA()
  359. {
  360. echo "a";
  361. }
  362. }
  363. ',
  364. ];
  365. yield [
  366. '<?php
  367. class SomeClass
  368. {
  369. /* comment */
  370. public function echoB()
  371. {
  372. echo "a";
  373. }
  374. }
  375. ',
  376. '<?php
  377. class SomeClass
  378. {
  379. /* comment */public function echoB()
  380. {
  381. echo "a";
  382. }
  383. }
  384. ',
  385. ];
  386. yield [
  387. '<?php
  388. class SomeClass
  389. {
  390. /* comment */
  391. public function echoC()
  392. {
  393. echo "a";
  394. }
  395. }
  396. ',
  397. '<?php
  398. class SomeClass
  399. {
  400. /* comment */ public function echoC()
  401. {
  402. echo "a";
  403. }
  404. }
  405. ',
  406. ];
  407. yield [
  408. '<?php
  409. abstract class MethodTest2
  410. {
  411. public function method045()
  412. {
  413. $files = null;
  414. if (!empty($files)) {
  415. $this->filter(
  416. function (\SplFileInfo $file) use ($files) {
  417. return !in_array($file->getRelativePathname(), $files, true);
  418. }
  419. );
  420. }
  421. }
  422. private $a;
  423. public static function method145()
  424. {
  425. }
  426. abstract protected function method245();
  427. // comment
  428. final private function method345()
  429. {
  430. }
  431. }
  432. function some1(){ echo 1;}
  433. function some2(){ echo 2;}',
  434. '<?php
  435. abstract class MethodTest2
  436. {
  437. public function method045()
  438. {
  439. $files = null;
  440. if (!empty($files)) {
  441. $this->filter(
  442. function (\SplFileInfo $file) use ($files) {
  443. return !in_array($file->getRelativePathname(), $files, true);
  444. }
  445. );
  446. }
  447. }
  448. private $a;
  449. public static function method145()
  450. {
  451. }
  452. abstract protected function method245();
  453. // comment
  454. final private function method345()
  455. {
  456. }
  457. }
  458. function some1(){ echo 1;}
  459. function some2(){ echo 2;}',
  460. ];
  461. yield [
  462. '<?php
  463. /*
  464. * This file is part of the PHP CS utility.
  465. *
  466. * (c) Fabien Potencier <fabien@symfony.com>
  467. *
  468. * This source file is subject to the MIT license that is bundled
  469. * with this source code in the file LICENSE.
  470. */
  471. namespace PhpCsFixer\Linter;
  472. /**
  473. * Dummy linter. No linting is performed. No error is raised.
  474. *
  475. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  476. *
  477. * @internal
  478. */
  479. final class NullLinter implements LinterInterface
  480. {
  481. /**
  482. * {@inheritdoc}
  483. */
  484. public function lintFile($path)
  485. {
  486. unset($path);
  487. }
  488. /**
  489. * {@inheritdoc}
  490. */
  491. public function lintSource($source)
  492. {
  493. unset($source);
  494. }
  495. }
  496. ',
  497. ];
  498. // do not touch anonymous functions (since PHP doesn't allow
  499. // for class attributes being functions :(, we only have to test
  500. // those used within methods)
  501. yield [
  502. '<?php
  503. class MethodTestAnonymous
  504. {
  505. public function method444a()
  506. {
  507. $text = "hello";
  508. $example = function ($arg) use ($message) {
  509. var_dump($arg . " " . $message);
  510. };
  511. $example($text);
  512. $example = function($arg) use ($message) {
  513. var_dump($arg . " " . $message);
  514. };
  515. $example = function /*test*/ ($arg) use ($message) {
  516. var_dump($arg . " " . $message);
  517. };
  518. }
  519. }',
  520. ];
  521. yield [
  522. '<?php
  523. class MethodTest1
  524. {
  525. private $c; //
  526. public function method444a()
  527. {
  528. }
  529. /**
  530. *
  531. */
  532. public function method444b()
  533. {
  534. }
  535. //
  536. public function method444c()
  537. {
  538. }
  539. private $a;
  540. public function method444d()
  541. {
  542. }
  543. private $b;
  544. //
  545. public function method444e()
  546. {
  547. }
  548. public function method444f()
  549. {
  550. }
  551. private $d; //
  552. public function method444f1()
  553. {
  554. }
  555. /**/
  556. public function method444g()
  557. {
  558. }
  559. }',
  560. '<?php
  561. class MethodTest1
  562. {
  563. private $c; //
  564. public function method444a()
  565. {
  566. }
  567. /**
  568. *
  569. */
  570. public function method444b()
  571. {
  572. }
  573. //
  574. public function method444c()
  575. {
  576. }
  577. private $a;
  578. public function method444d()
  579. {
  580. }
  581. private $b;
  582. //
  583. public function method444e()
  584. {
  585. }
  586. public function method444f()
  587. {
  588. }
  589. private $d; //
  590. public function method444f1()
  591. {
  592. }
  593. /**/
  594. public function method444g()
  595. {
  596. }
  597. }',
  598. ];
  599. // spaces between methods
  600. yield [
  601. '<?php
  602. abstract class MethodTest3
  603. {
  604. public function method021()
  605. {
  606. }
  607. public static function method121()
  608. {
  609. }
  610. abstract protected function method221(); '.'
  611. final private function method321a()
  612. {
  613. }
  614. }',
  615. '<?php
  616. abstract class MethodTest3
  617. {
  618. public function method021()
  619. {
  620. }
  621. public static function method121()
  622. {
  623. }
  624. abstract protected function method221();
  625. '.'
  626. final private function method321a()
  627. {
  628. }
  629. }', ];
  630. // don't change correct code
  631. yield [
  632. '<?php
  633. class SmallHelperException extends \Exception
  634. {
  635. public function getId111()
  636. {
  637. return 1;
  638. }
  639. public function getMessage111()
  640. {
  641. return \'message\';
  642. }
  643. }
  644. class MethodTest123124124
  645. {
  646. public function method111a(){}
  647. public function method211a(){}
  648. }',
  649. ];
  650. // do not touch function out of class scope
  651. yield [
  652. '<?php
  653. function some0() {
  654. }
  655. class MethodTest4
  656. {
  657. public function method122b()
  658. {
  659. }
  660. public function method222b()
  661. {
  662. }
  663. }
  664. function some() {
  665. }
  666. function some2() {
  667. }
  668. ',
  669. ];
  670. yield [
  671. '<?php interface A {
  672. public function B1(); // allowed comment
  673. public function C(); // allowed comment
  674. }',
  675. '<?php interface A {public function B1(); // allowed comment
  676. public function C(); // allowed comment
  677. }',
  678. ];
  679. yield [
  680. '<?php class Foo {
  681. var $a;
  682. var $b;
  683. }',
  684. '<?php class Foo {
  685. var $a;
  686. var $b;
  687. }',
  688. ];
  689. yield [
  690. '<?php
  691. class A
  692. {
  693. /** 1 */
  694. function A2() {}
  695. /** 2 */
  696. function B2() {}
  697. }
  698. ',
  699. '<?php
  700. class A
  701. {
  702. /** 1 */
  703. function A2() {}
  704. /** 2 */
  705. function B2() {}
  706. }
  707. ',
  708. ];
  709. // do not touch well formatted traits
  710. yield [
  711. '<?php
  712. trait OkTrait
  713. {
  714. function getReturnTypeOk()
  715. {
  716. }
  717. /**
  718. *
  719. */
  720. function getReturnDescriptionOk()
  721. {
  722. }
  723. }',
  724. ];
  725. yield [
  726. '<?php
  727. trait ezcReflectionReturnInfo {
  728. public $x = 1;
  729. protected function getA(){echo 1;}
  730. function getB(){echo 2;}
  731. protected function getC(){echo 3;}
  732. /** Description */
  733. function getD(){echo 4;}
  734. protected function getE(){echo 3;}
  735. private $a;
  736. function getF(){echo 4;}
  737. }',
  738. '<?php
  739. trait ezcReflectionReturnInfo {
  740. public $x = 1;
  741. protected function getA(){echo 1;}function getB(){echo 2;}
  742. protected function getC(){echo 3;}/** Description */function getD(){echo 4;}
  743. protected function getE(){echo 3;}private $a;function getF(){echo 4;}
  744. }',
  745. ];
  746. yield [
  747. '<?php
  748. trait SomeReturnInfo {
  749. function getReturnType()
  750. {
  751. }
  752. function getReturnDescription()
  753. {
  754. }
  755. function getReturnDescription2()
  756. {
  757. }
  758. abstract public function getWorld();
  759. }',
  760. '<?php
  761. trait SomeReturnInfo {
  762. function getReturnType()
  763. {
  764. }
  765. function getReturnDescription()
  766. {
  767. } function getReturnDescription2()
  768. {
  769. }
  770. abstract public function getWorld();
  771. }',
  772. ];
  773. yield [
  774. '<?php
  775. interface TestInterface
  776. {
  777. public function someInterfaceMethod4();
  778. public function someInterfaceMethod5();
  779. /**
  780. * {@link}
  781. */ '.'
  782. public function someInterfaceMethod6();
  783. public function someInterfaceMethod7();
  784. public function someInterfaceMethod8();
  785. }',
  786. '<?php
  787. interface TestInterface
  788. { public function someInterfaceMethod4();
  789. public function someInterfaceMethod5();
  790. /**
  791. * {@link}
  792. */ '.'
  793. public function someInterfaceMethod6();
  794. public function someInterfaceMethod7(); public function someInterfaceMethod8();
  795. }',
  796. ];
  797. // do not touch well formatted interfaces
  798. yield [
  799. '<?php
  800. interface TestInterfaceOK
  801. {
  802. public function someMethod1();
  803. public function someMethod2();
  804. }',
  805. ];
  806. // method after trait use
  807. yield [
  808. '<?php
  809. trait ezcReflectionReturnInfo {
  810. function getReturnDescription() {}
  811. }
  812. class ezcReflectionMethod extends ReflectionMethod {
  813. use ezcReflectionReturnInfo;
  814. function afterUseTrait(){}
  815. function afterUseTrait2(){}
  816. }',
  817. '<?php
  818. trait ezcReflectionReturnInfo {
  819. function getReturnDescription() {}
  820. }
  821. class ezcReflectionMethod extends ReflectionMethod {
  822. use ezcReflectionReturnInfo;function afterUseTrait(){}function afterUseTrait2(){}
  823. }',
  824. ];
  825. yield 'multi line property' => [
  826. '<?php class Foo
  827. {
  828. private $prop = [
  829. 1 => true,
  830. 2 => false,
  831. ];
  832. // comment2
  833. private $bar = 1;
  834. }',
  835. '<?php class Foo
  836. {
  837. private $prop = [
  838. 1 => true,
  839. 2 => false,
  840. ]; // comment2
  841. private $bar = 1;
  842. }',
  843. ['elements' => ['property' => 'one']],
  844. ];
  845. yield 'trait group import none' => [
  846. '<?php class Foo
  847. {
  848. use Ao;
  849. use B0 { X0 as Y0;} // test
  850. use A;
  851. use B { X as Y;} // test
  852. use Char;
  853. use Bar {
  854. __construct as barConstruct;
  855. baz as barBaz;
  856. }
  857. use Dua;
  858. }',
  859. '<?php class Foo
  860. {
  861. use Ao;
  862. use B0 { X0 as Y0;} // test
  863. use A;
  864. use B { X as Y;} // test
  865. use Char;
  866. use Bar {
  867. __construct as barConstruct;
  868. baz as barBaz;
  869. }
  870. use Dua;
  871. }',
  872. ['elements' => ['trait_import' => 'none']],
  873. ];
  874. yield [
  875. '<?php
  876. class Foo
  877. {
  878. /** A */
  879. private $email;
  880. private $foo0; #0 /* test */
  881. private $foo1; #1
  882. private $foo2; /* @2 */
  883. }',
  884. '<?php
  885. class Foo
  886. {
  887. /** A */
  888. private $email;
  889. private $foo0; #0 /* test */
  890. private $foo1; #1
  891. private $foo2; /* @2 */
  892. }',
  893. ['elements' => ['property' => 'none']],
  894. ];
  895. yield [
  896. '<?php
  897. class Sample
  898. {
  899. /** @var int */
  900. const FOO = 1;
  901. /** @var int */
  902. const BAR = 2;
  903. const BAZ = 3;
  904. const OTHER = 4;
  905. const OTHER2 = 5;
  906. }',
  907. '<?php
  908. class Sample
  909. {
  910. /** @var int */
  911. const FOO = 1;
  912. /** @var int */
  913. const BAR = 2;
  914. const BAZ = 3;
  915. const OTHER = 4;
  916. const OTHER2 = 5;
  917. }',
  918. ['elements' => ['const' => 'none']],
  919. ];
  920. yield 'multiple trait import 5954' => [
  921. '<?php
  922. class Foo
  923. {
  924. use Bar, Baz;
  925. }',
  926. null,
  927. ['elements' => ['method' => 'one']],
  928. ];
  929. yield 'multiple trait import with method 5954' => [
  930. '<?php
  931. class Foo
  932. {
  933. use Bar, Baz;
  934. public function f() {}
  935. }',
  936. '<?php
  937. class Foo
  938. {
  939. use Bar, Baz;
  940. public function f() {}
  941. }',
  942. ['elements' => ['method' => 'one']],
  943. ];
  944. yield 'trait group import 5843' => [
  945. '<?php
  946. class Foo
  947. {
  948. use Ao;
  949. use B0 { X0 as Y0;} // test
  950. use A;
  951. use B { X as Y;} // test
  952. use Char;
  953. use Bar {
  954. __construct as barConstruct;
  955. baz as barBaz;
  956. }
  957. use Dua;
  958. public function aaa()
  959. {
  960. }
  961. }',
  962. '<?php
  963. class Foo
  964. {
  965. use Ao;
  966. use B0 { X0 as Y0;} // test
  967. use A;
  968. use B { X as Y;} // test
  969. use Char;
  970. use Bar {
  971. __construct as barConstruct;
  972. baz as barBaz;
  973. }
  974. use Dua;
  975. public function aaa()
  976. {
  977. }
  978. }',
  979. ['elements' => ['method' => 'one', 'trait_import' => 'one']],
  980. ];
  981. yield [
  982. '<?php
  983. class Foo
  984. {
  985. use SomeTrait1;
  986. use SomeTrait2;
  987. public function Bar(){}
  988. }
  989. ',
  990. '<?php
  991. class Foo
  992. {
  993. use SomeTrait1;
  994. use SomeTrait2;
  995. public function Bar(){}
  996. }
  997. ',
  998. ['elements' => ['method' => 'one', 'trait_import' => 'one']],
  999. ];
  1000. yield 'trait group import 5852' => [
  1001. '<?php
  1002. class Foo
  1003. {
  1004. use A;
  1005. use B;
  1006. /**
  1007. *
  1008. */
  1009. public function A(){}
  1010. }',
  1011. '<?php
  1012. class Foo
  1013. {
  1014. use A;
  1015. use B;
  1016. /**
  1017. *
  1018. */
  1019. public function A(){}
  1020. }',
  1021. ['elements' => ['const' => 'one', 'method' => 'one', 'property' => 'one', 'trait_import' => 'none']],
  1022. ];
  1023. yield [
  1024. '<?php
  1025. abstract class Example
  1026. {
  1027. use SomeTrait;
  1028. use AnotherTrait;
  1029. public $property;
  1030. abstract public function method(): void;
  1031. }',
  1032. '<?php
  1033. abstract class Example
  1034. {
  1035. use SomeTrait;
  1036. use AnotherTrait;
  1037. public $property;
  1038. abstract public function method(): void;
  1039. }',
  1040. ['elements' => ['const' => 'one', 'method' => 'one', 'property' => 'one']],
  1041. ];
  1042. yield [
  1043. '<?php
  1044. class A
  1045. {
  1046. private $a = null;
  1047. public $b = 1;
  1048. function A() {}
  1049. }
  1050. ',
  1051. '<?php
  1052. class A
  1053. {
  1054. private $a = null;
  1055. public $b = 1;
  1056. function A() {}
  1057. }
  1058. ',
  1059. ['elements' => ['property' => 'one']],
  1060. ];
  1061. yield [
  1062. '<?php
  1063. class A
  1064. {
  1065. private $a = null;
  1066. public $b = 1;
  1067. function A() {}
  1068. }
  1069. ',
  1070. '<?php
  1071. class A
  1072. {
  1073. private $a = null;
  1074. public $b = 1;
  1075. function A() {}
  1076. }
  1077. ',
  1078. ['elements' => ['property' => 'none']],
  1079. ];
  1080. yield [
  1081. '<?php
  1082. class A
  1083. {
  1084. const A = 1;
  1085. const THREE = ONE + self::TWO; /* test */ # test
  1086. const B = 2;
  1087. }
  1088. ',
  1089. '<?php
  1090. class A
  1091. {
  1092. const A = 1;
  1093. const THREE = ONE + self::TWO; /* test */ # test
  1094. const B = 2;
  1095. }
  1096. ',
  1097. ['elements' => ['const' => 'one']],
  1098. ];
  1099. yield [
  1100. '<?php
  1101. class A
  1102. {
  1103. const A = 1;
  1104. const THREE = ONE + self::TWO;
  1105. const B = 2;
  1106. }
  1107. ',
  1108. '<?php
  1109. class A
  1110. {
  1111. const A = 1;
  1112. const THREE = ONE + self::TWO;
  1113. const B = 2;
  1114. }
  1115. ',
  1116. ['elements' => ['const' => 'none']],
  1117. ];
  1118. yield [
  1119. '<?php
  1120. class A
  1121. {
  1122. function D() {}
  1123. function B4() {}
  1124. }
  1125. ',
  1126. '<?php
  1127. class A
  1128. {
  1129. function D() {}
  1130. function B4() {}
  1131. }
  1132. ',
  1133. ['elements' => ['method' => 'one']],
  1134. ];
  1135. yield [
  1136. '<?php
  1137. class A
  1138. {
  1139. function A() {}
  1140. function B() {}
  1141. }
  1142. ',
  1143. '<?php
  1144. class A
  1145. {
  1146. function A() {}
  1147. function B() {}
  1148. }
  1149. ',
  1150. ['elements' => ['method' => 'none']],
  1151. ];
  1152. yield [
  1153. '<?php
  1154. class A
  1155. {
  1156. private $x;
  1157. private $y;
  1158. final function f1() {}
  1159. final function f2() {}
  1160. }
  1161. ',
  1162. '<?php
  1163. class A
  1164. {
  1165. private $x;
  1166. private $y;
  1167. final function f1() {}
  1168. final function f2() {}
  1169. }
  1170. ',
  1171. ['elements' => ['property' => 'none', 'method' => 'one']],
  1172. ];
  1173. yield [
  1174. '<?php
  1175. class A
  1176. {
  1177. const FOO = 1;
  1178. const BAR = 2;
  1179. function f1() {}
  1180. function f2() {}
  1181. }
  1182. ',
  1183. '<?php
  1184. class A
  1185. {
  1186. const FOO = 1;
  1187. const BAR = 2;
  1188. function f1() {}
  1189. function f2() {}
  1190. }
  1191. ',
  1192. ['elements' => ['const' => 'none', 'method' => 'one']],
  1193. ];
  1194. yield [
  1195. '<?php
  1196. class A
  1197. {
  1198. const FOO = 1;
  1199. const BAR = 2;
  1200. public function f1() {}
  1201. public function f2() {}
  1202. }
  1203. ',
  1204. '<?php
  1205. class A
  1206. {
  1207. const FOO = 1;
  1208. const BAR = 2;
  1209. public function f1() {}
  1210. public function f2() {}
  1211. }
  1212. ',
  1213. ['elements' => ['const' => 'none', 'method' => 'one']],
  1214. ];
  1215. yield [
  1216. '<?php
  1217. class A
  1218. {
  1219. const B = 2;
  1220. const FOO = 1;
  1221. const BAR = 2;
  1222. /** @var int */
  1223. const BAZ = 3;
  1224. /** @var int */
  1225. const NEW = 4;
  1226. /** @var int */
  1227. const A = 5;
  1228. }
  1229. ',
  1230. '<?php
  1231. class A
  1232. {
  1233. const B = 2;
  1234. const FOO = 1;
  1235. const BAR = 2;
  1236. /** @var int */
  1237. const BAZ = 3;
  1238. /** @var int */
  1239. const NEW = 4;
  1240. /** @var int */
  1241. const A = 5;
  1242. }
  1243. ',
  1244. ['elements' => ['const' => 'only_if_meta']],
  1245. ];
  1246. yield [
  1247. '<?php
  1248. class B
  1249. {
  1250. public $foo;
  1251. /** @var string */
  1252. public $bar;
  1253. public $baz;
  1254. }
  1255. ',
  1256. '<?php
  1257. class B
  1258. {
  1259. public $foo;
  1260. /** @var string */
  1261. public $bar;
  1262. public $baz;
  1263. }
  1264. ',
  1265. ['elements' => ['property' => 'only_if_meta']],
  1266. ];
  1267. yield [
  1268. '<?php
  1269. class C
  1270. {
  1271. public function f1() {}
  1272. public function f2() {}
  1273. public function f3() {}
  1274. /** @return string */
  1275. public function f4() {}
  1276. }
  1277. ',
  1278. '<?php
  1279. class C
  1280. {
  1281. public function f1() {}
  1282. public function f2() {}
  1283. public function f3() {}
  1284. /** @return string */
  1285. public function f4() {}
  1286. }
  1287. ',
  1288. ['elements' => ['method' => 'only_if_meta']],
  1289. ];
  1290. yield [
  1291. '<?php
  1292. class Sample
  1293. {
  1294. /** @var int */
  1295. const ART = 1;
  1296. const SCIENCE = 2;
  1297. /** @var string */
  1298. public $a;
  1299. /** @var int */
  1300. public $b;
  1301. public $c;
  1302. /**
  1303. * @param string $a
  1304. * @param int $b
  1305. * @param int $c
  1306. */
  1307. public function __construct($a, $b, $c) {}
  1308. public function __destruct() {}
  1309. }
  1310. ',
  1311. '<?php
  1312. class Sample
  1313. {
  1314. /** @var int */
  1315. const ART = 1;
  1316. const SCIENCE = 2;
  1317. /** @var string */
  1318. public $a;
  1319. /** @var int */
  1320. public $b;
  1321. public $c;
  1322. /**
  1323. * @param string $a
  1324. * @param int $b
  1325. * @param int $c
  1326. */
  1327. public function __construct($a, $b, $c) {}
  1328. public function __destruct() {}
  1329. }
  1330. ',
  1331. ['elements' => ['const' => 'only_if_meta', 'property' => 'only_if_meta', 'method' => 'only_if_meta']],
  1332. ];
  1333. yield [
  1334. '<?php
  1335. class A
  1336. {
  1337. use A;
  1338. use B;
  1339. private $a = null;
  1340. public $b = 1;
  1341. }
  1342. ',
  1343. '<?php
  1344. class A
  1345. {
  1346. use A;
  1347. use B;
  1348. private $a = null;
  1349. public $b = 1;
  1350. }
  1351. ',
  1352. ['elements' => ['property' => 'none', 'trait_import' => 'none']],
  1353. ];
  1354. yield [
  1355. '<?php
  1356. class Foo {
  1357. public function H1(){}
  1358. /** */
  1359. public const BAR = 123;
  1360. /** */
  1361. private const BAZ = "a";
  1362. }',
  1363. '<?php
  1364. class Foo {
  1365. public function H1(){}
  1366. /** */
  1367. public const BAR = 123;
  1368. /** */
  1369. private const BAZ = "a";
  1370. }',
  1371. ];
  1372. yield [
  1373. '<?php
  1374. class Foo {
  1375. private ?int $foo;
  1376. protected string $bar;
  1377. public iterable $baz;
  1378. var ? Foo\Bar $qux;
  1379. }',
  1380. '<?php
  1381. class Foo {
  1382. private ?int $foo;
  1383. protected string $bar;
  1384. public iterable $baz;
  1385. var ? Foo\Bar $qux;
  1386. }',
  1387. ];
  1388. yield [
  1389. '<?php
  1390. class Foo {
  1391. private array $foo;
  1392. private array $bar;
  1393. }',
  1394. '<?php
  1395. class Foo {
  1396. private array $foo;
  1397. private array $bar;
  1398. }',
  1399. ];
  1400. yield [
  1401. '<?php
  1402. class Entity
  1403. {
  1404. /**
  1405. * @ORM\Column(name="one", type="text")
  1406. */
  1407. private string $one;
  1408. /**
  1409. * @ORM\Column(name="two", type="text")
  1410. */
  1411. private string $two;
  1412. private string $three;
  1413. private string $four;
  1414. private string $five;
  1415. }',
  1416. '<?php
  1417. class Entity
  1418. {
  1419. /**
  1420. * @ORM\Column(name="one", type="text")
  1421. */
  1422. private string $one;
  1423. /**
  1424. * @ORM\Column(name="two", type="text")
  1425. */
  1426. private string $two;
  1427. private string $three;
  1428. private string $four;
  1429. private string $five;
  1430. }',
  1431. ['elements' => ['property' => 'only_if_meta']],
  1432. ];
  1433. yield [
  1434. '<?php
  1435. class Foo
  1436. {
  1437. use SomeTrait1;
  1438. use SomeTrait2;
  1439. public function Bar(){}
  1440. }
  1441. ',
  1442. '<?php
  1443. class Foo
  1444. {
  1445. use SomeTrait1;
  1446. use SomeTrait2;
  1447. public function Bar(){}
  1448. }
  1449. ',
  1450. ];
  1451. }
  1452. /**
  1453. * @param _AutogeneratedInputConfiguration $config
  1454. *
  1455. * @dataProvider provideFix80Cases
  1456. *
  1457. * @requires PHP 8.0
  1458. */
  1459. public function testFix80(string $expected, ?string $input, array $config = []): void
  1460. {
  1461. $this->fixer->configure($config);
  1462. $this->doTest($expected, $input);
  1463. }
  1464. public static function provideFix80Cases(): iterable
  1465. {
  1466. yield 'attributes' => [
  1467. '<?php
  1468. class User1
  1469. {
  1470. #[ORM\Id, ORM\Column("integer"), ORM\GeneratedValue]
  1471. private $id;
  1472. #[ORM\Column("string", ORM\Column::UNIQUE)]
  1473. #[Assert\String()]
  1474. #[Assert\Email(["message" => "The email {{ value }} is not a valid email."])]
  1475. private $email;
  1476. #[Assert\String()]
  1477. private $name;
  1478. }',
  1479. '<?php
  1480. class User1
  1481. {
  1482. #[ORM\Id, ORM\Column("integer"), ORM\GeneratedValue]
  1483. private $id;
  1484. #[ORM\Column("string", ORM\Column::UNIQUE)]
  1485. #[Assert\String()]
  1486. #[Assert\Email(["message" => "The email {{ value }} is not a valid email."])]
  1487. private $email;
  1488. #[Assert\String()]
  1489. private $name;
  1490. }',
  1491. ];
  1492. yield 'attributes minimal' => [
  1493. '<?php
  1494. class User2{
  1495. #[ORM\Id, ORM\Column("integer"), ORM\GeneratedValue]
  1496. private $id;
  1497. }',
  1498. '<?php
  1499. class User2{#[ORM\Id, ORM\Column("integer"), ORM\GeneratedValue] private $id;}',
  1500. ];
  1501. yield 'attribute block' => [
  1502. '<?php
  1503. class User3
  1504. {
  1505. private $id;
  1506. #[ORM\Column("string")]
  1507. #[Assert\Email(["message" => "Foo"])]
  1508. private $email;
  1509. }',
  1510. '<?php
  1511. class User3
  1512. {
  1513. private $id;
  1514. #[ORM\Column("string")]
  1515. #[Assert\Email(["message" => "Foo"])] private $email;
  1516. }',
  1517. ];
  1518. yield 'constructor property promotion' => [
  1519. '<?php
  1520. class Foo {
  1521. private array $foo;
  1522. private array $bar;
  1523. public function __construct(
  1524. public float $x = 0.0,
  1525. protected float $y = 0.0,
  1526. private float $z = 0.0,
  1527. ) {}
  1528. }',
  1529. '<?php
  1530. class Foo {
  1531. private array $foo;
  1532. private array $bar;
  1533. public function __construct(
  1534. public float $x = 0.0,
  1535. protected float $y = 0.0,
  1536. private float $z = 0.0,
  1537. ) {}
  1538. }',
  1539. ];
  1540. yield 'typed properties' => [
  1541. '<?php
  1542. class Foo {
  1543. private static int | float | null $a;
  1544. private static int | float | null $b;
  1545. private int | float | null $c;
  1546. private int | float | null $d;
  1547. }',
  1548. '<?php
  1549. class Foo {
  1550. private static int | float | null $a;
  1551. private static int | float | null $b;
  1552. private int | float | null $c;
  1553. private int | float | null $d;
  1554. }',
  1555. ];
  1556. yield 'attributes with conditional spacing' => [
  1557. '<?php
  1558. class User
  1559. {
  1560. private $id;
  1561. #[Assert\String()]
  1562. private $name;
  1563. private $email;
  1564. }
  1565. ',
  1566. '<?php
  1567. class User
  1568. {
  1569. private $id;
  1570. #[Assert\String()]
  1571. private $name;
  1572. private $email;
  1573. }
  1574. ',
  1575. ['elements' => ['property' => 'only_if_meta']],
  1576. ];
  1577. yield 'mixed attributes and phpdoc with conditional spacing' => [
  1578. '<?php
  1579. class User
  1580. {
  1581. private $id;
  1582. /** @var string */
  1583. #[Assert\Email(["message" => "Foo"])]
  1584. private $email;
  1585. #[Assert\String()]
  1586. #[ORM\Column()]
  1587. private $place;
  1588. #[ORM\Column()]
  1589. /** @var string */
  1590. private $hash;
  1591. /** @var string **/
  1592. #[ORM\Column()]
  1593. /** @internal */
  1594. private $updatedAt;
  1595. }
  1596. ',
  1597. '<?php
  1598. class User
  1599. {
  1600. private $id;
  1601. /** @var string */
  1602. #[Assert\Email(["message" => "Foo"])]
  1603. private $email;
  1604. #[Assert\String()]
  1605. #[ORM\Column()]
  1606. private $place;
  1607. #[ORM\Column()]
  1608. /** @var string */
  1609. private $hash;
  1610. /** @var string **/
  1611. #[ORM\Column()]
  1612. /** @internal */
  1613. private $updatedAt;
  1614. }
  1615. ',
  1616. ['elements' => ['property' => 'only_if_meta']],
  1617. ];
  1618. yield [
  1619. '<?php
  1620. class Foo
  1621. {
  1622. #[Assert\Email(["message" => "Foo"])]
  1623. private $email;
  1624. private $foo1; #1
  1625. private $foo2; /* @2 */
  1626. }',
  1627. '<?php
  1628. class Foo
  1629. {
  1630. #[Assert\Email(["message" => "Foo"])]
  1631. private $email;
  1632. private $foo1; #1
  1633. private $foo2; /* @2 */
  1634. }',
  1635. ['elements' => ['property' => 'none']],
  1636. ];
  1637. }
  1638. /**
  1639. * @param _AutogeneratedInputConfiguration $config
  1640. *
  1641. * @dataProvider provideFix81Cases
  1642. *
  1643. * @requires PHP 8.1
  1644. */
  1645. public function testFix81(string $expected, ?string $input, array $config = []): void
  1646. {
  1647. $this->fixer->configure($config);
  1648. $this->doTest($expected, $input);
  1649. }
  1650. public static function provideFix81Cases(): iterable
  1651. {
  1652. yield [
  1653. '<?php class A {
  1654. public int $a0;
  1655. public readonly int $a1;
  1656. readonly public int $a2;
  1657. readonly int $a3;
  1658. public int $a4;
  1659. }',
  1660. '<?php class A {
  1661. public int $a0;
  1662. public readonly int $a1;
  1663. readonly public int $a2;
  1664. readonly int $a3;
  1665. public int $a4;
  1666. }',
  1667. ];
  1668. yield [
  1669. '<?php
  1670. class Foo
  1671. {
  1672. final public const B1 = "1";
  1673. public final const B2 = "2";
  1674. final const B3 = "3";
  1675. }
  1676. ',
  1677. '<?php
  1678. class Foo
  1679. {
  1680. final public const B1 = "1";
  1681. public final const B2 = "2";
  1682. final const B3 = "3";
  1683. }
  1684. ',
  1685. ];
  1686. yield 'intersection properties' => [
  1687. '<?php
  1688. class Foo {
  1689. private static Bar & Something & Baz $a;
  1690. private static Bar & Something & Baz $b;
  1691. private Bar & Something & Baz $c;
  1692. private Bar & Something & Baz $d;
  1693. }',
  1694. '<?php
  1695. class Foo {
  1696. private static Bar & Something & Baz $a;
  1697. private static Bar & Something & Baz $b;
  1698. private Bar & Something & Baz $c;
  1699. private Bar & Something & Baz $d;
  1700. }',
  1701. ];
  1702. $input = '<?php
  1703. enum Cards: string
  1704. {
  1705. protected const Deck = "d.d";
  1706. protected const Pack = "p.p";
  1707. case Hearts = "H";
  1708. case Spades = "S";
  1709. case Diamonds = "D";
  1710. case Clubs = "C";
  1711. protected function test() {
  1712. echo 1;
  1713. }
  1714. protected function test2() {
  1715. echo 2;
  1716. }
  1717. }
  1718. ';
  1719. yield [
  1720. '<?php
  1721. enum Cards: string
  1722. {
  1723. protected const Deck = "d.d";
  1724. protected const Pack = "p.p";
  1725. case Hearts = "H";
  1726. case Spades = "S";
  1727. case Diamonds = "D";
  1728. case Clubs = "C";
  1729. protected function test() {
  1730. echo 1;
  1731. }
  1732. protected function test2() {
  1733. echo 2;
  1734. }
  1735. }
  1736. ',
  1737. $input,
  1738. ['elements' => [
  1739. 'const' => 'one',
  1740. 'method' => 'one',
  1741. 'case' => 'one',
  1742. ]],
  1743. ];
  1744. yield [
  1745. '<?php
  1746. enum Cards: string
  1747. {
  1748. protected const Deck = "d.d";
  1749. protected const Pack = "p.p";
  1750. case Hearts = "H";
  1751. case Spades = "S";
  1752. case Diamonds = "D";
  1753. case Clubs = "C";
  1754. protected function test() {
  1755. echo 1;
  1756. }
  1757. protected function test2() {
  1758. echo 2;
  1759. }
  1760. }
  1761. ',
  1762. $input,
  1763. ['elements' => [
  1764. 'const' => 'none',
  1765. 'method' => 'one',
  1766. 'case' => 'none',
  1767. ]],
  1768. ];
  1769. }
  1770. /**
  1771. * @dataProvider provideFix82Cases
  1772. *
  1773. * @requires PHP 8.2
  1774. */
  1775. public function testFix82(string $expected, ?string $input = null): void
  1776. {
  1777. $this->doTest($expected, $input);
  1778. }
  1779. /**
  1780. * @return iterable<array{0: string, 1?: string}>
  1781. */
  1782. public static function provideFix82Cases(): iterable
  1783. {
  1784. yield [
  1785. '<?php
  1786. trait Foo {
  1787. const Bar = 1;
  1788. const Baz = 2;
  1789. }',
  1790. '<?php
  1791. trait Foo {
  1792. const Bar = 1;
  1793. const Baz = 2;
  1794. }',
  1795. ];
  1796. yield [
  1797. '<?php
  1798. class X
  1799. {
  1800. private A|(B&C) $propertyName;
  1801. }',
  1802. ];
  1803. }
  1804. /**
  1805. * @dataProvider provideWithWhitespacesConfigCases
  1806. */
  1807. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  1808. {
  1809. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  1810. $this->doTest($expected, $input);
  1811. }
  1812. /**
  1813. * @return iterable<array{string, string}>
  1814. */
  1815. public static function provideWithWhitespacesConfigCases(): iterable
  1816. {
  1817. yield [
  1818. "<?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",
  1819. "<?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",
  1820. ];
  1821. yield [
  1822. "<?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",
  1823. "<?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",
  1824. ];
  1825. }
  1826. /**
  1827. * @param array<array-key, mixed> $elements
  1828. *
  1829. * @dataProvider provideInvalidConfigurationCases
  1830. */
  1831. public function testInvalidConfiguration(array $elements): void
  1832. {
  1833. $this->expectException(InvalidFixerConfigurationException::class);
  1834. $this->fixer->configure(['elements' => $elements]);
  1835. }
  1836. public static function provideInvalidConfigurationCases(): iterable
  1837. {
  1838. yield 'numeric keys' => [['method', 'property']];
  1839. yield 'wrong key name' => [['methods' => 'one']];
  1840. yield 'wrong key value' => [['method' => 'two']];
  1841. }
  1842. /**
  1843. * @dataProvider provideCommentBlockStartDetectionCases
  1844. */
  1845. public function testCommentBlockStartDetection(int $expected, string $code, int $index): void
  1846. {
  1847. Tokens::clearCache();
  1848. $tokens = Tokens::fromCode($code);
  1849. $result = \Closure::bind(static fn (ClassAttributesSeparationFixer $fixer): int => $fixer->findCommentBlockStart($tokens, $index, 0), null, ClassAttributesSeparationFixer::class)($this->fixer);
  1850. self::assertSame(
  1851. $expected,
  1852. $result,
  1853. \sprintf('Expected index %d (%s) got index %d (%s).', $expected, $tokens[$expected]->toJson(), $result, $tokens[$result]->toJson())
  1854. );
  1855. }
  1856. /**
  1857. * @return iterable<array{int, string, int}>
  1858. */
  1859. public static function provideCommentBlockStartDetectionCases(): iterable
  1860. {
  1861. yield [
  1862. 4,
  1863. '<?php
  1864. //ui
  1865. //j1
  1866. //k2
  1867. ',
  1868. 6,
  1869. ];
  1870. yield [
  1871. 4,
  1872. '<?php
  1873. //ui
  1874. //j1
  1875. //k2
  1876. ',
  1877. 5,
  1878. ];
  1879. yield [
  1880. 4,
  1881. '<?php
  1882. /**/
  1883. //j1
  1884. //k2
  1885. ',
  1886. 6,
  1887. ];
  1888. yield [
  1889. 4,
  1890. '<?php
  1891. $a;//j
  1892. //k
  1893. ',
  1894. 6,
  1895. ];
  1896. yield [
  1897. 2,
  1898. '<?php
  1899. //a
  1900. ',
  1901. 2,
  1902. ];
  1903. yield [
  1904. 2,
  1905. '<?php
  1906. //b
  1907. //c
  1908. ',
  1909. 2,
  1910. ];
  1911. yield [
  1912. 2,
  1913. '<?php
  1914. //d
  1915. //e
  1916. ',
  1917. 4,
  1918. ];
  1919. yield [
  1920. 2,
  1921. '<?php
  1922. /**/
  1923. //f
  1924. //g
  1925. //h
  1926. ',
  1927. 8,
  1928. ];
  1929. }
  1930. }