SingleClassElementPerStatementFixerTest.php 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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\WhitespacesFixerConfig;
  16. /**
  17. * @author Javier Spagnoletti <phansys@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer
  26. */
  27. final class SingleClassElementPerStatementFixerTest 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 Foo
  44. {
  45. private static $bar1 = array(1,2,3);
  46. private static $bar2 = [1,2,3];
  47. private static $baz1 = array(array(1,2), array(3, 4));
  48. private static $baz2 = array(1,2,3);
  49. private static $aaa1 = 1;
  50. private static $aaa2 = array(2, 2);
  51. private static $aaa3 = 3;
  52. }',
  53. '<?php
  54. class Foo
  55. {
  56. private static $bar1 = array(1,2,3), $bar2 = [1,2,3];
  57. private static $baz1 = array(array(1,2), array(3, 4)), $baz2 = array(1,2,3);
  58. private static $aaa1 = 1, $aaa2 = array(2, 2), $aaa3 = 3;
  59. }',
  60. ];
  61. yield [
  62. '<?php
  63. class Foo
  64. {
  65. const A = 1;
  66. const B = 2;
  67. }
  68. echo Foo::A, Foo::B;
  69. ',
  70. '<?php
  71. class Foo
  72. {
  73. const A = 1, B = 2;
  74. }
  75. echo Foo::A, Foo::B;
  76. ',
  77. ];
  78. yield [
  79. <<<'EOT'
  80. <?php
  81. class Foo { protected static $foo = 1; protected static $bar; protected static $baz=2 ; }
  82. EOT,
  83. <<<'EOT'
  84. <?php
  85. class Foo { protected static $foo = 1,$bar,$baz=2 ; }
  86. EOT,
  87. ];
  88. yield [
  89. <<<'EOT'
  90. <?php
  91. class Foo {}
  92. class Bar
  93. {
  94. }
  95. EOT,
  96. ];
  97. yield [
  98. <<<'EOT'
  99. <?php
  100. class Foo { protected static $foo = 1; protected static $bar; protected static $baz=2 ; }
  101. EOT,
  102. <<<'EOT'
  103. <?php
  104. class Foo { protected static $foo = 1, $bar, $baz=2 ; }
  105. EOT,
  106. ];
  107. yield [
  108. <<<'EOT'
  109. <?php
  110. class Foo { const ONE = 1; const TWO = 2; protected static $foo = 1; protected static $bar; protected static $baz=2 ; const THREE = 3; }
  111. EOT,
  112. <<<'EOT'
  113. <?php
  114. class Foo { const ONE = 1, TWO = 2; protected static $foo = 1, $bar, $baz=2 ; const THREE = 3; }
  115. EOT,
  116. ];
  117. yield [
  118. <<<'EOT'
  119. <?php
  120. class Foo {
  121. protected static $foo = 1;
  122. protected static $bar;
  123. protected static $baz=2;
  124. }
  125. EOT,
  126. <<<'EOT'
  127. <?php
  128. class Foo {
  129. protected static $foo = 1,
  130. $bar,
  131. $baz=2;
  132. }
  133. EOT,
  134. ];
  135. yield [
  136. <<<'EOT'
  137. <?php
  138. class Foo {
  139. /**
  140. * Some great docblock
  141. */
  142. protected static $foo = 1;
  143. protected static $bar;
  144. protected static $baz=2;
  145. }
  146. EOT,
  147. <<<'EOT'
  148. <?php
  149. class Foo {
  150. /**
  151. * Some great docblock
  152. */
  153. protected static $foo = 1,
  154. $bar,
  155. $baz=2;
  156. }
  157. EOT,
  158. ];
  159. yield [
  160. <<<'EOT'
  161. <?php
  162. class Foo {
  163. /**
  164. * @int
  165. */
  166. protected static $foo = 1;
  167. protected static $bar;
  168. protected static $baz=2;
  169. // this is an inline comment, not a docblock
  170. private $var = false;
  171. }
  172. EOT,
  173. <<<'EOT'
  174. <?php
  175. class Foo {
  176. /**
  177. * @int
  178. */
  179. protected static $foo = 1,
  180. $bar,
  181. $baz=2;
  182. // this is an inline comment, not a docblock
  183. private $var = false;
  184. }
  185. EOT,
  186. ];
  187. yield [
  188. <<<'EOT'
  189. <?php
  190. class Foo {
  191. /**
  192. * @int
  193. */
  194. protected static $foo = 1;
  195. protected static $bar;
  196. protected static $baz=2;
  197. function doSomething()
  198. {
  199. }
  200. }
  201. EOT,
  202. <<<'EOT'
  203. <?php
  204. class Foo {
  205. /**
  206. * @int
  207. */
  208. protected static $foo = 1,
  209. $bar,
  210. $baz=2;
  211. function doSomething()
  212. {
  213. }
  214. }
  215. EOT,
  216. ];
  217. yield 'line_breaks_1' => [
  218. <<<'EOT'
  219. <?php
  220. class Foo
  221. {
  222. public $bar = null;
  223. public $initialized = false;
  224. public $configured = false;
  225. public $called = false;
  226. public $arguments = array();
  227. public $baz = null;
  228. public $drop = false;
  229. function doSomething()
  230. {
  231. }
  232. }
  233. EOT,
  234. <<<'EOT'
  235. <?php
  236. class Foo
  237. {
  238. public $bar = null, $initialized = false, $configured = false, $called = false, $arguments = array();
  239. public $baz = null, $drop = false;
  240. function doSomething()
  241. {
  242. }
  243. }
  244. EOT,
  245. ];
  246. yield 'line_breaks_2' => [
  247. <<<'EOT'
  248. <?php
  249. class Foo
  250. {
  251. const TWO = '2';
  252. public $bar = null;
  253. public $initialized = false;
  254. function doSomething()
  255. {
  256. }
  257. }
  258. EOT,
  259. <<<'EOT'
  260. <?php
  261. class Foo
  262. {
  263. const TWO = '2';
  264. public $bar = null, $initialized = false;
  265. function doSomething()
  266. {
  267. }
  268. }
  269. EOT,
  270. ];
  271. yield 'line_breaks_3' => [
  272. <<<'EOT'
  273. <?php
  274. class Foo
  275. {
  276. const TWO = '2';
  277. public $bar = null;
  278. public $initialized = false;
  279. function doSomething()
  280. {
  281. }
  282. }
  283. EOT,
  284. <<<'EOT'
  285. <?php
  286. class Foo
  287. {
  288. const TWO = '2';
  289. public $bar = null, $initialized = false;
  290. function doSomething()
  291. {
  292. }
  293. }
  294. EOT,
  295. ];
  296. yield 'line_breaks_4' => [
  297. <<<'EOT'
  298. <?php
  299. class Foo
  300. {
  301. public $one = 1;
  302. public $bar = null;
  303. public $initialized = false;
  304. public $configured = false;
  305. public $called = false;
  306. public $arguments = array();
  307. function doSomething()
  308. {
  309. }
  310. }
  311. EOT,
  312. <<<'EOT'
  313. <?php
  314. class Foo
  315. {
  316. public $one = 1;
  317. public $bar = null, $initialized = false, $configured = false, $called = false, $arguments = array();
  318. function doSomething()
  319. {
  320. }
  321. }
  322. EOT,
  323. ];
  324. yield 'line_breaks_5' => [
  325. <<<'EOT'
  326. <?php
  327. class Foo
  328. {
  329. public $one = 1; public $bar = null; public $initialized = false; public $configured = false; public $called = false; public $arguments = array();
  330. function doSomething()
  331. {
  332. }
  333. }
  334. EOT,
  335. <<<'EOT'
  336. <?php
  337. class Foo
  338. {
  339. public $one = 1; public $bar = null, $initialized = false, $configured = false, $called = false, $arguments = array();
  340. function doSomething()
  341. {
  342. }
  343. }
  344. EOT,
  345. ];
  346. yield 'line_breaks_6' => [
  347. <<<'EOT'
  348. <?php
  349. class Foo
  350. {
  351. public $one = 1;public $bar = null;public $initialized = false;public $configured = false;public $called = false;public $arguments = array();
  352. function doSomething()
  353. {
  354. }
  355. }
  356. EOT,
  357. <<<'EOT'
  358. <?php
  359. class Foo
  360. {
  361. public $one = 1;public $bar = null, $initialized = false, $configured = false, $called = false, $arguments = array();
  362. function doSomething()
  363. {
  364. }
  365. }
  366. EOT,
  367. ];
  368. yield 'whitespace_1' => [
  369. <<<'EOT'
  370. <?php
  371. class Foo { public $one = 1; public $bar = null; public $initialized = false; public $configured = false; public $called = false; public $arguments = array();
  372. function doSomething()
  373. {
  374. }
  375. }
  376. EOT,
  377. <<<'EOT'
  378. <?php
  379. class Foo { public $one = 1; public $bar = null,$initialized = false,$configured = false,$called = false,$arguments = array();
  380. function doSomething()
  381. {
  382. }
  383. }
  384. EOT,
  385. ];
  386. yield 'whitespace_2' => [
  387. <<<'EOT'
  388. <?php
  389. class Foo { public $one = 1; public $bar = null; public $initialized = false; public $configured = false; public $called=false; public $arguments = array();
  390. function doSomething()
  391. {
  392. }
  393. }
  394. EOT,
  395. <<<'EOT'
  396. <?php
  397. class Foo { public $one = 1; public $bar = null,$initialized = false,$configured = false,$called=false,$arguments = array();
  398. function doSomething()
  399. {
  400. }
  401. }
  402. EOT,
  403. ];
  404. yield [
  405. <<<'EOT'
  406. <?php
  407. class Foo { protected static $foo = 1; protected static $bar; protected static $baz=1; }
  408. EOT,
  409. <<<'EOT'
  410. <?php
  411. class Foo { protected static $foo = 1, $bar, $baz=1; }
  412. EOT,
  413. ];
  414. yield [
  415. <<<'EOT'
  416. <?php
  417. class Foo { protected static $foo = 1; protected static $bar; protected static $baz=1; }
  418. EOT,
  419. <<<'EOT'
  420. <?php
  421. class Foo { protected static $foo = 1, $bar, $baz=1; }
  422. EOT,
  423. ];
  424. yield [
  425. <<<'EOT'
  426. <?php
  427. class Foo { protected $foo = 1; protected $bar; protected $baz=2; }
  428. EOT,
  429. <<<'EOT'
  430. <?php
  431. class Foo { protected $foo = 1, $bar, $baz=2; }
  432. EOT,
  433. ];
  434. yield [
  435. <<<'EOT'
  436. <?php
  437. class Foo { var $foo = 1; var $bar; var $baz=2; }
  438. EOT,
  439. <<<'EOT'
  440. <?php
  441. class Foo { var $foo = 1, $bar, $baz=2; }
  442. EOT,
  443. ];
  444. yield [
  445. <<<'EOT'
  446. <?php
  447. class Foo { var $foo = 1; var $bar; public function doSomething1() {} var $baz=2; }
  448. EOT,
  449. <<<'EOT'
  450. <?php
  451. class Foo { var $foo = 1, $bar; public function doSomething1() {} var $baz=2; }
  452. EOT,
  453. ];
  454. yield [
  455. <<<'EOT'
  456. <?php
  457. class Foo { var $foo = 1; var $bar; public function doSomething2() { global $one, $two, $three; } var $baz=2; }
  458. EOT,
  459. <<<'EOT'
  460. <?php
  461. class Foo { var $foo = 1, $bar; public function doSomething2() { global $one, $two, $three; } var $baz=2; }
  462. EOT,
  463. ];
  464. yield [
  465. <<<'EOT'
  466. <?php
  467. class Foo { public function doSomething3() {} protected $foo = 1; protected $bar; protected $baz=2; }
  468. EOT,
  469. <<<'EOT'
  470. <?php
  471. class Foo { public function doSomething3() {} protected $foo = 1, $bar, $baz=2; }
  472. EOT,
  473. ];
  474. yield [
  475. <<<'EOT'
  476. <?php
  477. class Foo { public function doSomethingElse() {} protected $foo = 1; protected $bar; protected $baz=2; private $acme =array(); }
  478. EOT,
  479. <<<'EOT'
  480. <?php
  481. class Foo { public function doSomethingElse() {} protected $foo = 1, $bar, $baz=2; private $acme =array(); }
  482. EOT,
  483. ];
  484. yield [
  485. <<<'EOT'
  486. <?php
  487. class Foo { public function doSomewhere() {} protected $foo = 1; protected $bar; protected $baz=2; private $acme1 =array(); }
  488. EOT,
  489. <<<'EOT'
  490. <?php
  491. class Foo { public function doSomewhere() {} protected $foo = 1, $bar, $baz=2; private $acme1 =array(); }
  492. EOT,
  493. ];
  494. yield [
  495. <<<'EOT'
  496. <?php
  497. class Foo { public function doThis() { global $one1, $two2, $three3; } protected $foo = 1; protected $bar; protected $baz=2; private $acme2 =array(); }
  498. EOT,
  499. <<<'EOT'
  500. <?php
  501. class Foo { public function doThis() { global $one1, $two2, $three3; } protected $foo = 1, $bar, $baz=2; private $acme2 =array(); }
  502. EOT,
  503. ];
  504. yield [
  505. '<?php
  506. class Foo
  507. {
  508. const A = 1;
  509. const #
  510. B#
  511. =#
  512. 2#
  513. ;#
  514. }
  515. echo Foo::A, Foo::B;
  516. ',
  517. '<?php
  518. class Foo
  519. {
  520. const A = 1,#
  521. B#
  522. =#
  523. 2#
  524. ;#
  525. }
  526. echo Foo::A, Foo::B;
  527. ',
  528. ];
  529. yield [
  530. '<?php
  531. class Token {
  532. const PUBLIC_CONST = 0;
  533. private const PRIVATE_CONST = 0;
  534. protected const PROTECTED_CONST = 0;
  535. public const PUBLIC_CONST_TWO = 0;
  536. public const TEST_71 = 0;
  537. }
  538. ',
  539. '<?php
  540. class Token {
  541. const PUBLIC_CONST = 0;
  542. private const PRIVATE_CONST = 0;
  543. protected const PROTECTED_CONST = 0;
  544. public const PUBLIC_CONST_TWO = 0, TEST_71 = 0;
  545. }
  546. ',
  547. ];
  548. yield [
  549. '<?php class Foo {
  550. private int $foo;
  551. private int $bar;
  552. }',
  553. '<?php class Foo {
  554. private int $foo, $bar;
  555. }',
  556. ];
  557. yield [
  558. '<?php class Foo {
  559. protected ?string $foo;
  560. protected ?string $bar;
  561. }',
  562. '<?php class Foo {
  563. protected ?string $foo, $bar;
  564. }',
  565. ];
  566. yield [
  567. '<?php class Foo {
  568. public ? string $foo;
  569. public ? string $bar;
  570. }',
  571. '<?php class Foo {
  572. public ? string $foo, $bar;
  573. }',
  574. ];
  575. yield [
  576. '<?php class Foo {
  577. var ? Foo\Bar $foo;
  578. var ? Foo\Bar $bar;
  579. }',
  580. '<?php class Foo {
  581. var ? Foo\Bar $foo, $bar;
  582. }',
  583. ];
  584. yield [
  585. '<?php class Foo {
  586. var array $foo;
  587. var array $bar;
  588. }',
  589. '<?php class Foo {
  590. var array $foo, $bar;
  591. }',
  592. ];
  593. yield [
  594. <<<'EOT'
  595. <?php
  596. class Foo
  597. {
  598. const SOME_CONST = 'a';
  599. const OTHER_CONST = 'b';
  600. protected static $foo = 1;
  601. protected static $bar = 2;
  602. }
  603. EOT,
  604. <<<'EOT'
  605. <?php
  606. class Foo
  607. {
  608. const SOME_CONST = 'a', OTHER_CONST = 'b';
  609. protected static $foo = 1, $bar = 2;
  610. }
  611. EOT,
  612. ['elements' => ['const', 'property']],
  613. ];
  614. yield [
  615. <<<'EOT'
  616. <?php
  617. class Foo
  618. {
  619. const SOME_CONST = 'a';
  620. const OTHER_CONST = 'b';
  621. protected static $foo = 1, $bar = 2;
  622. }
  623. EOT,
  624. <<<'EOT'
  625. <?php
  626. class Foo
  627. {
  628. const SOME_CONST = 'a', OTHER_CONST = 'b';
  629. protected static $foo = 1, $bar = 2;
  630. }
  631. EOT,
  632. ['elements' => ['const']],
  633. ];
  634. yield [
  635. <<<'EOT'
  636. <?php
  637. class Foo
  638. {
  639. const SOME_CONST = 'a', OTHER_CONST = 'b';
  640. protected static $foo = 1;
  641. protected static $bar = 2;
  642. }
  643. EOT,
  644. <<<'EOT'
  645. <?php
  646. class Foo
  647. {
  648. const SOME_CONST = 'a', OTHER_CONST = 'b';
  649. protected static $foo = 1, $bar = 2;
  650. }
  651. EOT,
  652. ['elements' => ['property']],
  653. ];
  654. yield 'anonymous class' => [
  655. '<?php
  656. $a = new class() {
  657. const PUBLIC_CONST_TWO = 0;
  658. const TEST_70 = 0;
  659. public function a() {
  660. }
  661. };
  662. class C
  663. {
  664. public function A()
  665. {
  666. $a = new class() {
  667. const PUBLIC_CONST_TWO = 0;
  668. const TEST_70 = 0;
  669. public function a() {}
  670. };
  671. }
  672. }
  673. ',
  674. '<?php
  675. $a = new class() {
  676. const PUBLIC_CONST_TWO = 0, TEST_70 = 0;
  677. public function a() {
  678. }
  679. };
  680. class C
  681. {
  682. public function A()
  683. {
  684. $a = new class() {
  685. const PUBLIC_CONST_TWO = 0, TEST_70 = 0;
  686. public function a() {}
  687. };
  688. }
  689. }
  690. ',
  691. ];
  692. }
  693. public function testWithWhitespacesConfig(): void
  694. {
  695. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  696. $this->doTest(
  697. "<?php\r\n\tclass Foo {\r\n\t\tconst AAA=0;\r\n\t\tconst BBB=1;\r\n\t}",
  698. "<?php\r\n\tclass Foo {\r\n\t\tconst AAA=0, BBB=1;\r\n\t}",
  699. );
  700. }
  701. /**
  702. * @dataProvider provideFix80Cases
  703. *
  704. * @requires PHP 8.0
  705. */
  706. public function testFix80(string $expected, string $input): void
  707. {
  708. $this->doTest($expected, $input);
  709. }
  710. /**
  711. * @return iterable<array{string, string}>
  712. */
  713. public static function provideFix80Cases(): iterable
  714. {
  715. yield [
  716. '<?php
  717. class Foo
  718. {
  719. private string|int $prop1;
  720. private string|int $prop2;
  721. }
  722. ',
  723. '<?php
  724. class Foo
  725. {
  726. private string|int $prop1, $prop2;
  727. }
  728. ',
  729. ];
  730. }
  731. /**
  732. * @dataProvider provideFix81Cases
  733. *
  734. * @requires PHP 8.1
  735. */
  736. public function testFix81(string $expected, ?string $input = null): void
  737. {
  738. $this->doTest($expected, $input);
  739. }
  740. /**
  741. * @return iterable<array{0: string, 1?: string}>
  742. */
  743. public static function provideFix81Cases(): iterable
  744. {
  745. yield [
  746. '<?php
  747. class Foo
  748. {
  749. readonly int $a;
  750. readonly int $b;
  751. public readonly int $c;
  752. public readonly int $d;
  753. readonly private string /*1*/$e;
  754. readonly private string /*2*/$f;
  755. readonly float $g;
  756. protected readonly float $h1;
  757. protected readonly float $h2;
  758. readonly float $z1;
  759. readonly float $z2;
  760. readonly float $z3;
  761. }',
  762. '<?php
  763. class Foo
  764. {
  765. readonly int $a, $b;
  766. public readonly int $c, $d;
  767. readonly private string /*1*/$e,/*2*/$f;
  768. readonly float $g;
  769. protected readonly float $h1, $h2;
  770. readonly float $z1, $z2, $z3;
  771. }',
  772. ];
  773. yield [
  774. '<?php
  775. class Foo
  776. {
  777. final public const B1 = "2";
  778. final public const B2 = "2";
  779. readonly float $z2;
  780. }
  781. ',
  782. ];
  783. yield [
  784. '<?php
  785. class Foo
  786. {
  787. private Foo&Bar $prop1;
  788. private Foo&Bar $prop2;
  789. }
  790. ',
  791. '<?php
  792. class Foo
  793. {
  794. private Foo&Bar $prop1, $prop2;
  795. }
  796. ',
  797. ];
  798. yield [
  799. "<?php
  800. enum Foo: string {
  801. public const A = 'A';
  802. public const B = 'B';
  803. case Hearts = 'H';
  804. case Spades = 'S';
  805. }
  806. var_dump(Foo::A.Foo::B);",
  807. "<?php
  808. enum Foo: string {
  809. public const A = 'A', B = 'B';
  810. case Hearts = 'H';
  811. case Spades = 'S';
  812. }
  813. var_dump(Foo::A.Foo::B);",
  814. ];
  815. }
  816. /**
  817. * @dataProvider provideFix82Cases
  818. *
  819. * @requires PHP 8.2
  820. */
  821. public function testFix82(string $expected, ?string $input = null): void
  822. {
  823. $this->doTest($expected, $input);
  824. }
  825. /**
  826. * @return iterable<array{string, string}>
  827. */
  828. public static function provideFix82Cases(): iterable
  829. {
  830. yield [
  831. '<?php trait Foo { public const Bar = 1; public const Baz = 1; }',
  832. '<?php trait Foo { public const Bar = 1, Baz = 1; }',
  833. ];
  834. }
  835. public function testInvalidConfiguration(): void
  836. {
  837. $this->expectException(InvalidFixerConfigurationException::class);
  838. $this->expectExceptionMessageMatches('/^\[single_class_element_per_statement\] Invalid configuration: The option "elements" .*\.$/');
  839. $this->fixer->configure(['elements' => ['foo']]); // @phpstan-ignore-line
  840. }
  841. }