CurlyBracesPositionFixerTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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\Basic;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Basic\CurlyBracesPositionFixer
  18. */
  19. final class CurlyBracesPositionFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @param array<string, mixed> $configuration
  23. *
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  27. {
  28. $this->fixer->configure($configuration);
  29. $this->doTest($expected, $input);
  30. }
  31. public static function provideFixCases(): iterable
  32. {
  33. yield 'if (default)' => [
  34. '<?php
  35. if ($foo) {
  36. foo();
  37. }',
  38. '<?php
  39. if ($foo)
  40. {
  41. foo();
  42. }',
  43. ];
  44. yield 'if (next line)' => [
  45. '<?php
  46. if ($foo)
  47. {
  48. foo();
  49. }',
  50. '<?php
  51. if ($foo) {
  52. foo();
  53. }',
  54. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  55. ];
  56. yield 'else (default)' => [
  57. '<?php
  58. if ($foo) {
  59. foo();
  60. }
  61. else {
  62. bar();
  63. }',
  64. '<?php
  65. if ($foo)
  66. {
  67. foo();
  68. }
  69. else
  70. {
  71. bar();
  72. }',
  73. ];
  74. yield 'else (next line)' => [
  75. '<?php
  76. if ($foo)
  77. {
  78. foo();
  79. }
  80. else
  81. {
  82. bar();
  83. }',
  84. '<?php
  85. if ($foo) {
  86. foo();
  87. }
  88. else {
  89. bar();
  90. }',
  91. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  92. ];
  93. yield 'elseif (default)' => [
  94. '<?php
  95. if ($foo) {
  96. foo();
  97. }
  98. elseif ($bar) {
  99. bar();
  100. }',
  101. '<?php
  102. if ($foo)
  103. {
  104. foo();
  105. }
  106. elseif ($bar)
  107. {
  108. bar();
  109. }',
  110. ];
  111. yield 'elseif (next line)' => [
  112. '<?php
  113. if ($foo)
  114. {
  115. foo();
  116. }
  117. elseif ($bar)
  118. {
  119. bar();
  120. }',
  121. '<?php
  122. if ($foo) {
  123. foo();
  124. }
  125. elseif ($bar) {
  126. bar();
  127. }',
  128. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  129. ];
  130. yield 'else if (default)' => [
  131. '<?php
  132. if ($foo) {
  133. foo();
  134. }
  135. else if ($bar) {
  136. bar();
  137. }',
  138. '<?php
  139. if ($foo)
  140. {
  141. foo();
  142. }
  143. else if ($bar)
  144. {
  145. bar();
  146. }',
  147. ];
  148. yield 'else if (next line)' => [
  149. '<?php
  150. if ($foo)
  151. {
  152. foo();
  153. }
  154. else if ($bar)
  155. {
  156. bar();
  157. }',
  158. '<?php
  159. if ($foo) {
  160. foo();
  161. }
  162. else if ($bar) {
  163. bar();
  164. }',
  165. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  166. ];
  167. yield 'for (default)' => [
  168. '<?php
  169. for (;;) {
  170. foo();
  171. }',
  172. '<?php
  173. for (;;)
  174. {
  175. foo();
  176. }',
  177. ];
  178. yield 'for (next line)' => [
  179. '<?php
  180. for (;;)
  181. {
  182. foo();
  183. }',
  184. '<?php
  185. for (;;) {
  186. foo();
  187. }',
  188. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  189. ];
  190. yield 'foreach (default)' => [
  191. '<?php
  192. foreach ($foo as $bar) {
  193. foo();
  194. }',
  195. '<?php
  196. foreach ($foo as $bar)
  197. {
  198. foo();
  199. }',
  200. ];
  201. yield 'foreach (next line)' => [
  202. '<?php
  203. foreach ($foo as $bar)
  204. {
  205. foo();
  206. }',
  207. '<?php
  208. foreach ($foo as $bar) {
  209. foo();
  210. }',
  211. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  212. ];
  213. yield 'while (default)' => [
  214. '<?php
  215. while ($foo) {
  216. foo();
  217. }',
  218. '<?php
  219. while ($foo)
  220. {
  221. foo();
  222. }',
  223. ];
  224. yield 'while (next line)' => [
  225. '<?php
  226. while ($foo)
  227. {
  228. foo();
  229. }',
  230. '<?php
  231. while ($foo) {
  232. foo();
  233. }',
  234. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  235. ];
  236. yield 'do while (default)' => [
  237. '<?php
  238. do {
  239. foo();
  240. } while ($foo);',
  241. '<?php
  242. do
  243. {
  244. foo();
  245. } while ($foo);',
  246. ];
  247. yield 'do while (next line)' => [
  248. '<?php
  249. do
  250. {
  251. foo();
  252. } while ($foo);',
  253. '<?php
  254. do {
  255. foo();
  256. } while ($foo);',
  257. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  258. ];
  259. yield 'switch (default)' => [
  260. '<?php
  261. switch ($foo) {
  262. case 1:
  263. foo();
  264. }',
  265. '<?php
  266. switch ($foo)
  267. {
  268. case 1:
  269. foo();
  270. }',
  271. ];
  272. yield 'switch (next line)' => [
  273. '<?php
  274. switch ($foo)
  275. {
  276. case 1:
  277. foo();
  278. }',
  279. '<?php
  280. switch ($foo) {
  281. case 1:
  282. foo();
  283. }',
  284. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  285. ];
  286. yield 'try catch finally (default)' => [
  287. '<?php
  288. switch ($foo) {
  289. case 1:
  290. foo();
  291. }',
  292. '<?php
  293. switch ($foo)
  294. {
  295. case 1:
  296. foo();
  297. }',
  298. ];
  299. yield 'try catch finally (next line)' => [
  300. '<?php
  301. switch ($foo)
  302. {
  303. case 1:
  304. foo();
  305. }',
  306. '<?php
  307. switch ($foo) {
  308. case 1:
  309. foo();
  310. }',
  311. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  312. ];
  313. yield 'class (default)' => [
  314. '<?php
  315. class Foo
  316. {
  317. }',
  318. '<?php
  319. class Foo {
  320. }',
  321. ];
  322. yield 'class (same line)' => [
  323. '<?php
  324. class Foo {
  325. }',
  326. '<?php
  327. class Foo
  328. {
  329. }',
  330. ['classes_opening_brace' => 'same_line'],
  331. ];
  332. yield 'function (default)' => [
  333. '<?php
  334. function foo()
  335. {
  336. }',
  337. '<?php
  338. function foo() {
  339. }',
  340. ];
  341. yield 'function (same line)' => [
  342. '<?php
  343. function foo() {
  344. }',
  345. '<?php
  346. function foo()
  347. {
  348. }',
  349. ['functions_opening_brace' => 'same_line'],
  350. ];
  351. yield 'anonymous function (default)' => [
  352. '<?php
  353. $foo = function () {
  354. };',
  355. '<?php
  356. $foo = function ()
  357. {
  358. };',
  359. ];
  360. yield 'anonymous function (next line)' => [
  361. '<?php
  362. $foo = function ()
  363. {
  364. };',
  365. '<?php
  366. $foo = function () {
  367. };',
  368. ['anonymous_functions_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  369. ];
  370. yield 'with blank lines inside braces' => [
  371. '<?php
  372. class Foo
  373. {
  374. public function foo()
  375. {
  376. if (true) {
  377. echo "foo";
  378. }
  379. }
  380. }',
  381. '<?php
  382. class Foo {
  383. public function foo() {
  384. if (true)
  385. {
  386. echo "foo";
  387. }
  388. }
  389. }',
  390. ];
  391. yield 'with comment after opening brace (default)' => [
  392. '<?php
  393. function foo() /* foo */ // foo
  394. {
  395. }',
  396. '<?php
  397. function foo() /* foo */ { // foo
  398. }',
  399. ];
  400. yield 'with comment after opening brace (same line)' => [
  401. '<?php
  402. function foo() { // foo
  403. /* foo */
  404. }',
  405. '<?php
  406. function foo() // foo
  407. { /* foo */
  408. }',
  409. ['functions_opening_brace' => 'same_line'],
  410. ];
  411. yield 'next line with multiline signature' => [
  412. '<?php
  413. if (
  414. $foo
  415. ) {
  416. foo();
  417. }',
  418. '<?php
  419. if (
  420. $foo
  421. )
  422. {
  423. foo();
  424. }',
  425. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  426. ];
  427. yield 'next line with newline before closing parenthesis' => [
  428. '<?php
  429. if ($foo
  430. ) {
  431. foo();
  432. }',
  433. '<?php
  434. if ($foo
  435. )
  436. {
  437. foo();
  438. }',
  439. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  440. ];
  441. yield 'next line with newline in signature but not before closing parenthesis' => [
  442. '<?php
  443. if (
  444. $foo)
  445. {
  446. foo();
  447. }',
  448. '<?php
  449. if (
  450. $foo) {
  451. foo();
  452. }',
  453. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  454. ];
  455. yield 'anonymous class (same line)' => [
  456. '<?php
  457. $foo = new class() {
  458. };',
  459. '<?php
  460. $foo = new class()
  461. {
  462. };',
  463. ];
  464. yield 'anonymous class (next line)' => [
  465. '<?php
  466. $foo = new class()
  467. {
  468. };',
  469. '<?php
  470. $foo = new class() {
  471. };',
  472. ['anonymous_classes_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  473. ];
  474. yield 'next line with multiline signature and return type' => [
  475. '<?php
  476. function foo(
  477. $foo
  478. ): int {
  479. foo();
  480. }',
  481. '<?php
  482. function foo(
  483. $foo
  484. ): int
  485. {
  486. foo();
  487. }',
  488. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  489. ];
  490. yield 'next line with multiline signature and return type (nullable)' => [
  491. '<?php
  492. function foo(
  493. $foo
  494. ): ?int {
  495. foo();
  496. }',
  497. '<?php
  498. function foo(
  499. $foo
  500. ): ?int
  501. {
  502. foo();
  503. }',
  504. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  505. ];
  506. yield 'next line with multiline signature and return type (array)' => [
  507. '<?php
  508. function foo(
  509. $foo
  510. ): array {
  511. foo();
  512. }',
  513. '<?php
  514. function foo(
  515. $foo
  516. ): array
  517. {
  518. foo();
  519. }',
  520. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  521. ];
  522. yield 'next line with multiline signature and return type (class name)' => [
  523. '<?php
  524. function foo(
  525. $foo
  526. ): \Foo\Bar {
  527. foo();
  528. }',
  529. '<?php
  530. function foo(
  531. $foo
  532. ): \Foo\Bar
  533. {
  534. foo();
  535. }',
  536. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  537. ];
  538. yield 'next line with newline before closing parenthesis and return type' => [
  539. '<?php
  540. function foo($foo
  541. ): int {
  542. foo();
  543. }',
  544. '<?php
  545. function foo($foo
  546. ): int
  547. {
  548. foo();
  549. }',
  550. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  551. ];
  552. yield 'next line with newline before closing parenthesis and callable type' => [
  553. '<?php
  554. function foo($foo
  555. ): callable {
  556. return function (): void {};
  557. }',
  558. '<?php
  559. function foo($foo
  560. ): callable
  561. {
  562. return function (): void {};
  563. }',
  564. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  565. ];
  566. yield 'next line with newline in signature but not before closing parenthesis and return type' => [
  567. '<?php
  568. function foo(
  569. $foo): int
  570. {
  571. foo();
  572. }',
  573. '<?php
  574. function foo(
  575. $foo): int {
  576. foo();
  577. }',
  578. ['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
  579. ];
  580. yield 'multiple elseifs' => [
  581. '<?php if ($foo) {
  582. } elseif ($foo) {
  583. } elseif ($foo) {
  584. } elseif ($foo) {
  585. } elseif ($foo) {
  586. } elseif ($foo) {
  587. } elseif ($foo) {
  588. } elseif ($foo) {
  589. } elseif ($foo) {
  590. }',
  591. '<?php if ($foo){
  592. } elseif ($foo){
  593. } elseif ($foo){
  594. } elseif ($foo){
  595. } elseif ($foo){
  596. } elseif ($foo){
  597. } elseif ($foo){
  598. } elseif ($foo){
  599. } elseif ($foo){
  600. }',
  601. ];
  602. yield 'open brace preceded by comment and whitespace' => [
  603. '<?php
  604. if (true) { /* foo */
  605. foo();
  606. }',
  607. '<?php
  608. if (true) /* foo */ {
  609. foo();
  610. }',
  611. ];
  612. yield 'open brace surrounded by comment and whitespace' => [
  613. '<?php
  614. if (true) { /* foo */ /* bar */
  615. foo();
  616. }',
  617. '<?php
  618. if (true) /* foo */ { /* bar */
  619. foo();
  620. }',
  621. ];
  622. yield 'open brace not preceded by space and followed by a comment' => [
  623. '<?php class test
  624. {
  625. public function example()// example
  626. {
  627. }
  628. }
  629. ',
  630. '<?php class test
  631. {
  632. public function example(){// example
  633. }
  634. }
  635. ',
  636. ];
  637. yield 'open brace not preceded by space and followed by a space and comment' => [
  638. '<?php class test
  639. {
  640. public function example() // example
  641. {
  642. }
  643. }
  644. ',
  645. '<?php class test
  646. {
  647. public function example(){ // example
  648. }
  649. }
  650. ',
  651. ];
  652. }
  653. /**
  654. * @dataProvider provideFix80Cases
  655. *
  656. * @requires PHP 8.0
  657. */
  658. public function testFix80(string $expected, ?string $input = null): void
  659. {
  660. $this->doTest($expected, $input);
  661. }
  662. public static function provideFix80Cases(): iterable
  663. {
  664. yield 'function (multiline + union return)' => [
  665. '<?php
  666. function sum(
  667. int|float $first,
  668. int|float $second,
  669. ): int|float {
  670. }',
  671. '<?php
  672. function sum(
  673. int|float $first,
  674. int|float $second,
  675. ): int|float
  676. {
  677. }',
  678. ];
  679. yield 'function (multiline + union return with whitespace)' => [
  680. '<?php
  681. function sum(
  682. int|float $first,
  683. int|float $second,
  684. ): int | float {
  685. }',
  686. '<?php
  687. function sum(
  688. int|float $first,
  689. int|float $second,
  690. ): int | float
  691. {
  692. }',
  693. ];
  694. yield 'method with static return type' => [
  695. '<?php
  696. class Foo
  697. {
  698. function sum(
  699. $foo
  700. ): static {
  701. }
  702. }',
  703. '<?php
  704. class Foo
  705. {
  706. function sum(
  707. $foo
  708. ): static
  709. {
  710. }
  711. }',
  712. ];
  713. }
  714. /**
  715. * @dataProvider provideFix81Cases
  716. *
  717. * @requires PHP 8.1
  718. */
  719. public function testFix81(string $expected, ?string $input = null): void
  720. {
  721. $this->doTest($expected, $input);
  722. }
  723. public static function provideFix81Cases(): iterable
  724. {
  725. yield 'function (multiline + intersection return)' => [
  726. '<?php
  727. function foo(
  728. mixed $bar,
  729. mixed $baz,
  730. ): Foo&Bar {
  731. }',
  732. ];
  733. }
  734. /**
  735. * @dataProvider provideFix82Cases
  736. *
  737. * @requires PHP 8.2
  738. */
  739. public function testFix82(string $expected, ?string $input = null): void
  740. {
  741. $this->doTest($expected, $input);
  742. }
  743. public static function provideFix82Cases(): iterable
  744. {
  745. yield 'function (multiline + DNF return)' => [
  746. '<?php
  747. function foo(
  748. mixed $bar,
  749. mixed $baz,
  750. ): (Foo&Bar)|int|null {
  751. }',
  752. ];
  753. }
  754. }