ClassDefinitionFixerTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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\ClassDefinitionFixer;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. use PhpCsFixer\WhitespacesFixerConfig;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer
  26. */
  27. final class ClassDefinitionFixerTest extends AbstractFixerTestCase
  28. {
  29. public function testConfigure(): void
  30. {
  31. $defaultConfig = [
  32. 'inline_constructor_arguments' => true,
  33. 'multi_line_extends_each_single_line' => false,
  34. 'single_item_single_line' => false,
  35. 'single_line' => false,
  36. 'space_before_parenthesis' => false,
  37. ];
  38. $fixer = new ClassDefinitionFixer();
  39. $fixer->configure($defaultConfig);
  40. self::assertConfigurationSame($defaultConfig, $fixer);
  41. $fixer->configure([]);
  42. self::assertConfigurationSame($defaultConfig, $fixer);
  43. }
  44. /**
  45. * @param _AutogeneratedInputConfiguration $config
  46. *
  47. * @dataProvider provideInvalidConfigurationCases
  48. */
  49. public function testInvalidConfiguration(array $config, string $exceptionExpression): void
  50. {
  51. $this->expectException(InvalidFixerConfigurationException::class);
  52. $this->expectExceptionMessageMatches($exceptionExpression);
  53. $this->fixer->configure($config);
  54. }
  55. /**
  56. * @return iterable<array{array<string, mixed>, string}>
  57. */
  58. public static function provideInvalidConfigurationCases(): iterable
  59. {
  60. yield 'invalid configuration key' => [
  61. ['a' => false],
  62. '/^\[class_definition\] Invalid configuration: The option "a" does not exist\. Defined options are: "inline_constructor_arguments", "multi_line_extends_each_single_line", "single_item_single_line", "single_line", "space_before_parenthesis"\.$/',
  63. ];
  64. yield 'invalid configuration value' => [
  65. ['single_line' => 'z'],
  66. '/^\[class_definition\] Invalid configuration: The option "single_line" with value "z" is expected to be of type "bool", but is of type "string"\.$/',
  67. ];
  68. }
  69. /**
  70. * @param _AutogeneratedInputConfiguration $configuration
  71. *
  72. * @dataProvider provideFixCases
  73. */
  74. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  75. {
  76. $this->fixer->configure($configuration);
  77. $this->doTest($expected, $input);
  78. }
  79. public static function provideFixCases(): iterable
  80. {
  81. yield [
  82. '<?php $a = new class(0) extends SomeClass implements SomeInterface, D {};',
  83. "<?php \$a = new class(0) extends\nSomeClass\timplements SomeInterface, D {};",
  84. ];
  85. yield [
  86. '<?php $a = new class(1) extends SomeClass implements SomeInterface, D {};',
  87. "<?php \$a = new class(1) extends\nSomeClass\timplements SomeInterface, D {};",
  88. ['single_line' => true],
  89. ];
  90. yield [
  91. "<?php \$a = new class('1a') implements\nA\n{};",
  92. "<?php \$a = new class('1a') implements\nA{};",
  93. ];
  94. yield [
  95. "<?php \$a = new class('1a') implements A {};",
  96. "<?php \$a = new class('1a') implements\nA{};",
  97. ['single_item_single_line' => true],
  98. ];
  99. yield [
  100. '<?php $a = new class {};',
  101. '<?php $a = new class{};',
  102. ];
  103. yield [
  104. '<?php $a = new class {};',
  105. "<?php \$a = new class\n{};",
  106. ];
  107. yield [
  108. '<?php $a = new class() {};',
  109. "<?php \$a = new\n class ( ){};",
  110. ];
  111. yield [
  112. '<?php $a = new class( ) {};',
  113. "<?php \$a = new\n class ( ){};",
  114. ['inline_constructor_arguments' => false],
  115. ];
  116. yield [
  117. '<?php $a = new class implements Foo {};',
  118. "<?php \$a = new\n class implements Foo {};",
  119. ['inline_constructor_arguments' => false],
  120. ];
  121. yield [
  122. '<?php $a = new class( $this->foo() , bar ( $a) ) {};',
  123. "<?php \$a = new\n class ( \$this->foo() , bar ( \$a) ){};",
  124. ['inline_constructor_arguments' => false],
  125. ];
  126. yield [
  127. '<?php $a = new class(10, 1, /**/ 2) {};',
  128. '<?php $a = new class( 10, 1,/**/2 ){};',
  129. ];
  130. yield [
  131. '<?php $a = new class( 10, 1,/**/2 ) {};',
  132. '<?php $a = new class( 10, 1,/**/2 ){};',
  133. ['inline_constructor_arguments' => false],
  134. ];
  135. yield [
  136. '<?php $a = new class(2) {};',
  137. '<?php $a = new class(2){};',
  138. ];
  139. yield [
  140. '<?php $a = new class($this->prop) {};',
  141. '<?php $a = new class( $this->prop ){};',
  142. ];
  143. yield [
  144. '<?php $a = new class( $this->prop ) {};',
  145. '<?php $a = new class( $this->prop ){};',
  146. ['inline_constructor_arguments' => false],
  147. ];
  148. yield [
  149. "<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, B {};",
  150. "<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, \t B{};",
  151. ['inline_constructor_arguments' => false],
  152. ];
  153. yield [
  154. "<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, B {};",
  155. "<?php \$a = new class (\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, \t B{};",
  156. ['inline_constructor_arguments' => false],
  157. ];
  158. yield [
  159. '<?php $a = new class($this->prop, $v[3], 4) {};',
  160. '<?php $a = new class( $this->prop,$v[3], 4) {};',
  161. ];
  162. yield 'PSR-12 Extends/Implements Parenthesis on the next line.' => [
  163. '<?php
  164. $instance = new class extends \Foo implements
  165. \ArrayAccess,
  166. \Countable,
  167. \Serializable
  168. {};',
  169. '<?php
  170. $instance = new class extends \Foo implements
  171. \ArrayAccess,\Countable,\Serializable{};',
  172. ];
  173. yield 'PSR-12 Implements Parenthesis on the next line.' => [
  174. '<?php
  175. $instance = new class implements
  176. \ArrayAccess,
  177. \Countable,
  178. \Serializable
  179. {};',
  180. '<?php
  181. $instance = new class implements
  182. \ArrayAccess,\Countable,\Serializable{};',
  183. ];
  184. yield 'PSR-12 Extends Parenthesis on the next line.' => [
  185. '<?php
  186. $instance = new class extends
  187. ArrayAccess
  188. {};',
  189. '<?php
  190. $instance = new class
  191. extends
  192. ArrayAccess
  193. {};',
  194. ];
  195. yield [
  196. "<?php \$a = new #
  197. class #
  198. ( #
  199. '1a', #
  200. 1 #
  201. ) #
  202. implements#
  203. A, #
  204. B,
  205. C #
  206. {#
  207. #
  208. }#
  209. ;",
  210. "<?php \$a = new#
  211. class#
  212. (#
  213. '1a',#
  214. 1 #
  215. )#
  216. implements#
  217. A, #
  218. B,C#
  219. {#
  220. #
  221. }#
  222. ;",
  223. ];
  224. yield [
  225. "<?php \$a = new #
  226. class #
  227. ( #
  228. '1a', #
  229. 1 #
  230. ) #
  231. implements #
  232. A #
  233. {#
  234. #
  235. }#
  236. ;",
  237. "<?php \$a = new#
  238. class#
  239. (#
  240. '1a',#
  241. 1 #
  242. )#
  243. implements#
  244. A#
  245. {#
  246. #
  247. }#
  248. ;",
  249. ['single_item_single_line' => true],
  250. ];
  251. yield [
  252. '<?php $a = new class() #
  253. {};',
  254. '<?php $a = new class()#
  255. {};',
  256. ];
  257. yield 'space_before_parenthesis 1' => [
  258. '<?php $z = new class () {};',
  259. '<?php $z = new class() {};',
  260. ['space_before_parenthesis' => true],
  261. ];
  262. yield 'space_before_parenthesis 2' => [
  263. '<?php $z = new class () {};',
  264. '<?php $z = new class () {};',
  265. ['space_before_parenthesis' => true],
  266. ];
  267. yield 'space_before_parenthesis and inline_constructor_arguments' => [
  268. '<?php $z = new class ( static::foo($this->bar()) ,baz() ) {};',
  269. '<?php $z = new class ( static::foo($this->bar()) ,baz() ) {};',
  270. ['space_before_parenthesis' => true, 'inline_constructor_arguments' => false],
  271. ];
  272. yield 'single attribute on separate line' => [
  273. <<<'EOF'
  274. <?php
  275. $a = new
  276. #[FOO]
  277. class() {};
  278. EOF,
  279. ];
  280. yield 'multiple attributes on separate line' => [
  281. <<<'EOF'
  282. <?php
  283. $a = new
  284. #[FOO]
  285. #[\Ns\Bar]
  286. class() {};
  287. EOF,
  288. ];
  289. yield 'single line phpdoc on separate line' => [
  290. <<<'EOF'
  291. <?php
  292. $a = new
  293. /** @property string $x */
  294. class() {};
  295. EOF,
  296. ];
  297. yield 'multi line phpdoc on separate line' => [
  298. <<<'EOF'
  299. <?php
  300. $a = new
  301. /**
  302. @property string $x
  303. */
  304. class() {};
  305. EOF,
  306. ];
  307. yield 'phpdoc and single attribute on separate line' => [
  308. <<<'EOF'
  309. <?php
  310. $a = new
  311. /**
  312. @property string $x
  313. */
  314. #[FOO]
  315. class() {};
  316. EOF,
  317. ];
  318. yield 'phpdoc and multiple attributes on separate line' => [
  319. <<<'EOF'
  320. <?php
  321. $a = new
  322. /** @property string $x */
  323. #[FOO] #[\Ns\Bar]
  324. class() {};
  325. EOF,
  326. ];
  327. yield from self::provideClassyCases('class');
  328. yield from self::provideClassyExtendingCases('class');
  329. yield from self::provideClassyImplementsCases();
  330. yield [
  331. "<?php class configA implements B, C\n{}",
  332. "<?php class configA implements\nB, C{}",
  333. ['single_line' => true],
  334. ];
  335. yield [
  336. "<?php class configA1 extends B\n{}",
  337. "<?php class configA1\n extends\nB{}",
  338. ['single_line' => true],
  339. ];
  340. yield [
  341. "<?php class configA1a extends B\n{}",
  342. "<?php class configA1a\n extends\nB{}",
  343. ['single_line' => false, 'single_item_single_line' => true],
  344. ];
  345. yield [
  346. "<?php class configA2 extends D implements B, C\n{}",
  347. "<?php class configA2 extends D implements\nB,\nC{}",
  348. ['single_line' => true],
  349. ];
  350. yield [
  351. "<?php class configA3 extends D implements B, C\n{}",
  352. "<?php class configA3\n extends\nD\n\t implements\nB,\nC{}",
  353. ['single_line' => true],
  354. ];
  355. yield [
  356. "<?php class configA4 extends D implements B, #\nC\n{}",
  357. "<?php class configA4\n extends\nD\n\t implements\nB,#\nC{}",
  358. ['single_line' => true],
  359. ];
  360. yield [
  361. "<?php class configA5 implements A\n{}",
  362. "<?php class configA5 implements\nA{}",
  363. ['single_line' => false, 'single_item_single_line' => true],
  364. ];
  365. yield [
  366. "<?php interface TestWithMultiExtendsMultiLine extends\n A,\nAb,\n C,\n D\n{}",
  367. "<?php interface TestWithMultiExtendsMultiLine extends A,\nAb,C,D\n{}",
  368. [
  369. 'single_line' => false,
  370. 'single_item_single_line' => false,
  371. 'multi_line_extends_each_single_line' => true,
  372. ],
  373. ];
  374. yield from self::provideClassyCases('interface');
  375. yield from self::provideClassyExtendingCases('interface');
  376. yield [
  377. '<?php
  378. interface Test extends
  379. /*a*/ /*b*/TestInterface1 , \A\B\C , /* test */
  380. TestInterface2 , // test
  381. '.'
  382. // Note: PSR does not have a rule for multiple extends
  383. TestInterface3, /**/ TestInterface4 ,
  384. TestInterface5 , '.'
  385. /**/TestInterface65
  386. {}
  387. ',
  388. '<?php
  389. interface Test
  390. extends
  391. /*a*/ /*b*/TestInterface1 , \A\B\C , /* test */
  392. TestInterface2 , // test
  393. '.'
  394. // Note: PSR does not have a rule for multiple extends
  395. TestInterface3, /**/ TestInterface4 ,
  396. TestInterface5 , '.'
  397. /**/TestInterface65 {}
  398. ',
  399. ];
  400. yield from self::provideClassyCases('trait');
  401. yield [
  402. '<?php
  403. $a = new class implements
  404. \RFb,
  405. \Fcc,
  406. \GFddZz
  407. {
  408. };',
  409. '<?php
  410. $a = new class implements
  411. \RFb,
  412. \Fcc, \GFddZz
  413. {
  414. };',
  415. ];
  416. yield [
  417. '<?php
  418. $a = new class implements
  419. \RFb,
  420. \Fcc,
  421. \GFddZz
  422. {
  423. }?>',
  424. '<?php
  425. $a = new class implements
  426. \RFb,
  427. \Fcc, \GFddZz
  428. {
  429. }?>',
  430. ];
  431. yield [
  432. '<?php new class(1, 2, 3, ) {};',
  433. '<?php new class(1, 2, 3,) {};',
  434. ];
  435. yield [
  436. '<?php new class(1, 2, 3, ) {};',
  437. '<?php new class(
  438. 1,
  439. 2,
  440. 3,
  441. ) {};',
  442. ];
  443. }
  444. /**
  445. * @param array<string, mixed> $expected
  446. *
  447. * @dataProvider provideClassyDefinitionInfoCases
  448. */
  449. public function testClassyDefinitionInfo(string $source, array $expected): void
  450. {
  451. Tokens::clearCache();
  452. $tokens = Tokens::fromCode($source);
  453. $method = new \ReflectionMethod($this->fixer, 'getClassyDefinitionInfo');
  454. $method->setAccessible(true);
  455. $result = $method->invoke($this->fixer, $tokens, $expected['classy']);
  456. ksort($expected);
  457. ksort($result);
  458. self::assertSame($expected, $result);
  459. }
  460. public static function provideClassyDefinitionInfoCases(): iterable
  461. {
  462. yield [
  463. '<?php class A{}',
  464. [
  465. 'start' => 1,
  466. 'classy' => 1,
  467. 'open' => 4,
  468. 'extends' => false,
  469. 'implements' => false,
  470. 'anonymousClass' => false,
  471. 'final' => false,
  472. 'abstract' => false,
  473. 'readonly' => false,
  474. ],
  475. ];
  476. yield [
  477. '<?php final class A{}',
  478. [
  479. 'start' => 1,
  480. 'classy' => 3,
  481. 'open' => 6,
  482. 'extends' => false,
  483. 'implements' => false,
  484. 'anonymousClass' => false,
  485. 'final' => 1,
  486. 'abstract' => false,
  487. 'readonly' => false,
  488. ],
  489. ];
  490. yield [
  491. '<?php abstract /**/ class A{}',
  492. [
  493. 'start' => 1,
  494. 'classy' => 5,
  495. 'open' => 8,
  496. 'extends' => false,
  497. 'implements' => false,
  498. 'anonymousClass' => false,
  499. 'final' => false,
  500. 'abstract' => 1,
  501. 'readonly' => false,
  502. ],
  503. ];
  504. yield [
  505. '<?php class A extends B {}',
  506. [
  507. 'start' => 1,
  508. 'classy' => 1,
  509. 'open' => 9,
  510. 'extends' => [
  511. 'start' => 5,
  512. 'numberOfExtends' => 1,
  513. 'multiLine' => false,
  514. ],
  515. 'implements' => false,
  516. 'anonymousClass' => false,
  517. 'final' => false,
  518. 'abstract' => false,
  519. 'readonly' => false,
  520. ],
  521. ];
  522. yield [
  523. '<?php interface A extends B,C,D {}',
  524. [
  525. 'start' => 1,
  526. 'classy' => 1,
  527. 'open' => 13,
  528. 'extends' => [
  529. 'start' => 5,
  530. 'numberOfExtends' => 3,
  531. 'multiLine' => false,
  532. ],
  533. 'implements' => false,
  534. 'anonymousClass' => false,
  535. 'final' => false,
  536. 'abstract' => false,
  537. 'readonly' => false,
  538. ],
  539. ];
  540. }
  541. /**
  542. * @param array<string, mixed> $expected
  543. *
  544. * @dataProvider provideClassyInheritanceInfoCases
  545. */
  546. public function testClassyInheritanceInfo(string $source, string $label, array $expected): void
  547. {
  548. $this->doTestClassyInheritanceInfo($source, $label, $expected);
  549. }
  550. public static function provideClassyInheritanceInfoCases(): iterable
  551. {
  552. yield '1' => [
  553. '<?php
  554. class X11 implements Z , T,R
  555. {
  556. }',
  557. 'numberOfImplements',
  558. ['start' => 5, 'numberOfImplements' => 3, 'multiLine' => false],
  559. ];
  560. yield '2' => [
  561. '<?php
  562. class X10 implements Z , T,R //
  563. {
  564. }',
  565. 'numberOfImplements',
  566. ['start' => 5, 'numberOfImplements' => 3, 'multiLine' => false],
  567. ];
  568. yield '3' => [
  569. '<?php class A implements B {}',
  570. 'numberOfImplements',
  571. ['start' => 5, 'numberOfImplements' => 1, 'multiLine' => false],
  572. ];
  573. yield '4' => [
  574. "<?php class A implements B,\n I{}",
  575. 'numberOfImplements',
  576. ['start' => 5, 'numberOfImplements' => 2, 'multiLine' => true],
  577. ];
  578. yield '5' => [
  579. "<?php class A implements Z\\C\\B,C,D {\n\n\n}",
  580. 'numberOfImplements',
  581. ['start' => 5, 'numberOfImplements' => 3, 'multiLine' => false],
  582. ];
  583. yield [
  584. '<?php
  585. namespace A {
  586. interface X {}
  587. }
  588. namespace {
  589. class B{}
  590. class A extends //
  591. B implements /* */ \A\C, Z{
  592. public function test()
  593. {
  594. echo 1;
  595. }
  596. }
  597. $a = new A();
  598. $a->test();
  599. }',
  600. 'numberOfImplements',
  601. ['start' => 36, 'numberOfImplements' => 2, 'multiLine' => false],
  602. ];
  603. yield [
  604. "<?php \$a = new class(3) extends\nSomeClass\timplements SomeInterface, D {};",
  605. 'numberOfExtends',
  606. ['start' => 12, 'numberOfExtends' => 1, 'multiLine' => true],
  607. ];
  608. yield [
  609. "<?php \$a = new class(4) extends\nSomeClass\timplements SomeInterface, D\n\n{};",
  610. 'numberOfImplements',
  611. ['start' => 16, 'numberOfImplements' => 2, 'multiLine' => false],
  612. ];
  613. yield [
  614. "<?php \$a = new class(5) extends SomeClass\nimplements SomeInterface, D {};",
  615. 'numberOfExtends',
  616. ['start' => 12, 'numberOfExtends' => 1, 'multiLine' => true],
  617. ];
  618. }
  619. /**
  620. * @param array<string, mixed> $expected
  621. *
  622. * @dataProvider provideClassyInheritanceInfoPre80Cases
  623. *
  624. * @requires PHP <8.0
  625. */
  626. public function testClassyInheritanceInfoPre80(string $source, string $label, array $expected): void
  627. {
  628. $this->doTestClassyInheritanceInfo($source, $label, $expected);
  629. }
  630. public static function provideClassyInheritanceInfoPre80Cases(): iterable
  631. {
  632. yield [
  633. '<?php
  634. namespace A {
  635. interface X {}
  636. }
  637. namespace {
  638. class B{}
  639. class A extends //
  640. B implements /* */ \A
  641. \C, Z{
  642. public function test()
  643. {
  644. echo 1;
  645. }
  646. }
  647. $a = new A();
  648. $a->test();
  649. }',
  650. 'numberOfImplements',
  651. ['start' => 36, 'numberOfImplements' => 2, 'multiLine' => true],
  652. ];
  653. }
  654. /**
  655. * @dataProvider provideWithWhitespacesConfigCases
  656. */
  657. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  658. {
  659. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  660. $this->doTest($expected, $input);
  661. }
  662. /**
  663. * @return iterable<array{string, string}>
  664. */
  665. public static function provideWithWhitespacesConfigCases(): iterable
  666. {
  667. yield [
  668. "<?php\r\nclass Aaa implements\r\n\tBbb,\r\n\tCcc,\r\n\tDdd\r\n\t{\r\n\t}",
  669. "<?php\r\nclass Aaa implements\r\n\tBbb, Ccc,\r\n\tDdd\r\n\t{\r\n\t}",
  670. ];
  671. }
  672. /**
  673. * @dataProvider provideFix80Cases
  674. *
  675. * @requires PHP 8.0
  676. */
  677. public function testFix80(string $expected, ?string $input = null): void
  678. {
  679. $this->doTest($expected, $input);
  680. }
  681. /**
  682. * @return iterable<string, array{string, string}>
  683. */
  684. public static function provideFix80Cases(): iterable
  685. {
  686. yield 'anonymous class, single attribute' => [
  687. '<?php $a = new #[FOO] class(2) {};',
  688. '<?php $a = new #[FOO] class(2){};',
  689. ];
  690. yield 'anonymous class, multiple attributes' => [
  691. '<?php $a = new #[FOO] #[BAR] class {};',
  692. '<?php $a = new #[FOO] #[BAR] class {};',
  693. ];
  694. }
  695. /**
  696. * @dataProvider provideFix81Cases
  697. *
  698. * @requires PHP 8.1
  699. */
  700. public function testFix81(string $expected, ?string $input = null): void
  701. {
  702. $this->doTest($expected, $input);
  703. }
  704. /**
  705. * @return iterable<array{string, string}>
  706. */
  707. public static function provideFix81Cases(): iterable
  708. {
  709. yield [
  710. "<?php enum SomeEnum implements SomeInterface, D\n{};",
  711. "<?php enum SomeEnum \timplements SomeInterface, D {};",
  712. ];
  713. yield [
  714. "<?php enum SomeEnum : int\n{}",
  715. '<?php enum SomeEnum : int {}',
  716. ];
  717. yield [
  718. "<?php enum SomeEnum\n{}",
  719. "<?php enum\tSomeEnum{}",
  720. ];
  721. }
  722. /**
  723. * @dataProvider provideFix82Cases
  724. *
  725. * @requires PHP 8.2
  726. */
  727. public function testFix82(string $expected, string $input): void
  728. {
  729. $this->doTest($expected, $input);
  730. }
  731. /**
  732. * @return iterable<string, array{string, string}>
  733. */
  734. public static function provideFix82Cases(): iterable
  735. {
  736. yield 'final readonly works' => [
  737. '<?php final readonly class a
  738. {}',
  739. '<?php final readonly class a
  740. {}',
  741. ];
  742. yield 'final - readonly modifiers get sorted' => [
  743. '<?php final readonly class a
  744. {}',
  745. '<?php readonly final class a
  746. {}',
  747. ];
  748. yield 'abstract - readonly modifiers get sorted' => [
  749. '<?php abstract readonly class a
  750. {}',
  751. '<?php readonly abstract class a
  752. {}',
  753. ];
  754. }
  755. /**
  756. * @dataProvider provideFix83Cases
  757. *
  758. * @requires PHP 8.3
  759. */
  760. public function testFix83(string $expected, ?string $input = null): void
  761. {
  762. $this->doTest($expected, $input);
  763. }
  764. /**
  765. * @return iterable<string, array{string, string}>
  766. */
  767. public static function provideFix83Cases(): iterable
  768. {
  769. yield 'anonymous class, readonly, missing spacing' => [
  770. '<?php $a = new readonly class {};',
  771. '<?php $a = new readonly class{};',
  772. ];
  773. yield 'anonymous class, readonly, to much spacing' => [
  774. '<?php $a = new readonly class {};',
  775. '<?php $a = new readonly class {};',
  776. ];
  777. yield 'anonymous class, single attribute' => [
  778. '<?php $a = new #[BAR] readonly class {};',
  779. '<?php $a = new #[BAR] readonly class{};',
  780. ];
  781. yield 'anonymous class, multiple attributes' => [
  782. '<?php $a = new #[FOO] #[BAR] readonly class {};',
  783. '<?php $a = new #[FOO] #[BAR] readonly class {};',
  784. ];
  785. }
  786. /**
  787. * @param array<string, mixed> $expected
  788. */
  789. private function doTestClassyInheritanceInfo(string $source, string $label, array $expected): void
  790. {
  791. Tokens::clearCache();
  792. $tokens = Tokens::fromCode($source);
  793. self::assertTrue($tokens[$expected['start']]->isGivenKind([T_IMPLEMENTS, T_EXTENDS]), \sprintf('Token must be "implements" or "extends", got "%s".', $tokens[$expected['start']]->getContent()));
  794. $method = new \ReflectionMethod($this->fixer, 'getClassyInheritanceInfo');
  795. $method->setAccessible(true);
  796. $result = $method->invoke($this->fixer, $tokens, $expected['start'], $label);
  797. self::assertSame($expected, $result);
  798. }
  799. /**
  800. * @param array<string, mixed> $expected
  801. */
  802. private static function assertConfigurationSame(array $expected, ClassDefinitionFixer $fixer): void
  803. {
  804. $reflectionProperty = new \ReflectionProperty($fixer, 'configuration');
  805. $reflectionProperty->setAccessible(true);
  806. self::assertSame($expected, $reflectionProperty->getValue($fixer));
  807. }
  808. /**
  809. * @return iterable<array{string, string}>
  810. */
  811. private static function provideClassyCases(string $classy): iterable
  812. {
  813. return [
  814. [
  815. \sprintf("<?php %s A\n{}", $classy),
  816. \sprintf('<?php %s A {}', $classy),
  817. ],
  818. [
  819. \sprintf("<?php %s B\n{}", $classy),
  820. \sprintf('<?php %s B{}', $classy),
  821. ],
  822. [
  823. \sprintf("<?php %s C\n{}", $classy),
  824. \sprintf("<?php %s\n\tC{}", $classy),
  825. ],
  826. [
  827. \sprintf("<?php %s D //\n{}", $classy),
  828. \sprintf("<?php %s D//\n{}", $classy),
  829. ],
  830. [
  831. \sprintf("<?php %s /**/ E //\n{}", $classy),
  832. \sprintf("<?php %s/**/E//\n{}", $classy),
  833. ],
  834. [
  835. \sprintf(
  836. "<?php
  837. %s A
  838. {}
  839. %s /**/ B //
  840. /**/\n{}",
  841. $classy,
  842. $classy
  843. ),
  844. \sprintf(
  845. '<?php
  846. %s
  847. A
  848. {}
  849. %s/**/B //
  850. /**/ {}',
  851. $classy,
  852. $classy
  853. ),
  854. ],
  855. [
  856. \sprintf(
  857. '<?php
  858. namespace {
  859. %s IndentedNameSpacedClass
  860. {
  861. }
  862. }',
  863. $classy
  864. ),
  865. \sprintf(
  866. '<?php
  867. namespace {
  868. %s IndentedNameSpacedClass {
  869. }
  870. }',
  871. $classy
  872. ),
  873. ],
  874. ];
  875. }
  876. /**
  877. * @return iterable<array{string, string}>
  878. */
  879. private static function provideClassyExtendingCases(string $classy): iterable
  880. {
  881. return [
  882. [
  883. \sprintf("<?php %s AE0 extends B\n{}", $classy),
  884. \sprintf('<?php %s AE0 extends B {}', $classy),
  885. ],
  886. [
  887. \sprintf("<?php %s /**/ AE1 /**/ extends /**/ B /**/\n{}", $classy),
  888. \sprintf('<?php %s/**/AE1/**/extends/**/B/**/{}', $classy),
  889. ],
  890. [
  891. \sprintf("<?php %s /*%s*/ AE2 extends\nB\n{}", $classy, $classy),
  892. \sprintf("<?php %s /*%s*/ AE2 extends\nB{}", $classy, $classy),
  893. ],
  894. [
  895. \sprintf('<?php
  896. %s Test124 extends
  897. \Exception
  898. {}', $classy),
  899. \sprintf('<?php
  900. %s
  901. Test124
  902. extends
  903. \Exception {}', $classy),
  904. ],
  905. ];
  906. }
  907. /**
  908. * @return iterable<int|string, array{string, string}>
  909. */
  910. private static function provideClassyImplementsCases(): iterable
  911. {
  912. return [
  913. [
  914. '<?php class LotOfImplements implements A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q
  915. {}',
  916. '<?php class LotOfImplements implements A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q{}',
  917. ],
  918. [
  919. "<?php class E implements B\n{}",
  920. "<?php class E \nimplements B \t{}",
  921. ],
  922. [
  923. "<?php abstract class F extends B implements C\n{}",
  924. '<?php abstract class F extends B implements C {}',
  925. ],
  926. 'multiline abstract extends implements with comments' => [
  927. "<?php abstract class G extends //
  928. B /* */ implements C\n{}",
  929. '<?php abstract class G extends //
  930. B/* */implements C{}',
  931. ],
  932. 'final extends implement' => [
  933. "<?php final class G extends //
  934. B /* */ implements C\n{}",
  935. '<?php final class G extends //
  936. B/* */implements C{}',
  937. ],
  938. 'final' => [
  939. '<?php final class G //
  940. /* */
  941. {}',
  942. '<?php final class G //
  943. /* */{}',
  944. ],
  945. [
  946. '<?php
  947. class Aaa IMPLEMENTS
  948. \RFb,
  949. \Fcc,
  950. \GFddZz
  951. {
  952. }',
  953. '<?php
  954. class Aaa IMPLEMENTS
  955. \RFb,
  956. \Fcc, \GFddZz
  957. {
  958. }',
  959. ],
  960. [
  961. '<?php
  962. class //
  963. X //
  964. extends //
  965. Y //
  966. implements //
  967. Z, //
  968. U //
  969. {} //',
  970. '<?php
  971. class //
  972. X //
  973. extends //
  974. Y //
  975. implements //
  976. Z , //
  977. U //
  978. {} //',
  979. ],
  980. [
  981. '<?php
  982. class Aaa implements
  983. PhpCsFixer\Tests\Fixer,
  984. \RFb,
  985. \Fcc1,
  986. \GFdd
  987. {
  988. }',
  989. '<?php
  990. class Aaa implements
  991. PhpCsFixer\Tests\Fixer,\RFb,
  992. \Fcc1, \GFdd
  993. {
  994. }',
  995. ],
  996. [
  997. '<?php
  998. class /**/ Test123 EXtends /**/ \RuntimeException implements
  999. TestZ
  1000. {
  1001. }',
  1002. '<?php
  1003. class/**/Test123
  1004. EXtends /**/ \RuntimeException implements
  1005. TestZ
  1006. {
  1007. }',
  1008. ],
  1009. [
  1010. '<?php
  1011. class Aaa implements Ebb, \Ccc
  1012. {
  1013. }',
  1014. '<?php
  1015. class Aaa implements Ebb, \Ccc
  1016. {
  1017. }',
  1018. ],
  1019. [
  1020. '<?php
  1021. class X2 IMPLEMENTS
  1022. Z, //
  1023. U,
  1024. D
  1025. {
  1026. }',
  1027. '<?php
  1028. class X2 IMPLEMENTS
  1029. Z , //
  1030. U, D
  1031. {
  1032. }',
  1033. ],
  1034. [
  1035. '<?php
  1036. class VeryLongClassNameWithLotsOfLetters extends AnotherVeryLongClassName implements
  1037. VeryLongInterfaceNameThatIDontWantOnTheSameLine
  1038. {
  1039. }',
  1040. '<?php
  1041. class VeryLongClassNameWithLotsOfLetters extends AnotherVeryLongClassName implements
  1042. VeryLongInterfaceNameThatIDontWantOnTheSameLine
  1043. {
  1044. }',
  1045. ],
  1046. [
  1047. '<?php
  1048. class /**/ Test125 //aaa
  1049. extends /*
  1050. */
  1051. //
  1052. \Exception //
  1053. {}',
  1054. '<?php
  1055. class/**/Test125 //aaa
  1056. extends /*
  1057. */
  1058. //
  1059. \Exception //
  1060. {}',
  1061. ],
  1062. [
  1063. '<?php
  1064. class Test extends TestInterface8 implements /*a*/ /*b*/
  1065. TestInterface1, /* test */
  1066. TestInterface2, // test
  1067. '.'
  1068. // test
  1069. TestInterface3, /**/
  1070. TestInterface4,
  1071. TestInterface5, '.'
  1072. /**/TestInterface6c
  1073. {
  1074. }',
  1075. '<?php
  1076. class Test
  1077. extends
  1078. TestInterface8
  1079. implements /*a*/ /*b*/TestInterface1 , /* test */
  1080. TestInterface2 , // test
  1081. '.'
  1082. // test
  1083. TestInterface3, /**/ TestInterface4 ,
  1084. TestInterface5 , '.'
  1085. /**/TestInterface6c
  1086. {
  1087. }',
  1088. ],
  1089. ];
  1090. }
  1091. }