ClassAttributesSeparationFixerTest.php 44 KB

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