VisibilityRequiredFixerTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer
  21. */
  22. final class VisibilityRequiredFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @param array<string, string> $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 'properties' => [
  37. <<<'EOF'
  38. <?php
  39. class Foo {
  40. public $var;
  41. protected $var_foo;
  42. private $FooBar;
  43. public static $var1;
  44. protected static $var_foo2;
  45. private static $FooBar1;
  46. public static $var2;
  47. protected static $var_foo3;
  48. private static $FooBar2;
  49. private static $FooBar3;
  50. public $old = 'foo';
  51. }
  52. EOF,
  53. <<<'EOF'
  54. <?php
  55. class Foo {
  56. public $var;
  57. protected $var_foo;
  58. private $FooBar;
  59. static public $var1;
  60. static protected $var_foo2;
  61. static private $FooBar1;
  62. public static $var2;
  63. protected static $var_foo3;
  64. private static $FooBar2;
  65. private static
  66. $FooBar3;
  67. var $old = 'foo';
  68. }
  69. EOF,
  70. ];
  71. yield 'properties after method' => [
  72. <<<'EOF'
  73. <?php
  74. class Foo {
  75. public function aaa() {}
  76. public $bbb;
  77. }
  78. EOF,
  79. <<<'EOF'
  80. <?php
  81. class Foo {
  82. public function aaa() {}
  83. var $bbb;
  84. }
  85. EOF,
  86. ];
  87. yield 'methods' => [
  88. <<<'EOF'
  89. <?php
  90. class MyTestWithAnonymousClass extends TestCase
  91. {
  92. public function setUp()
  93. {
  94. $provider = new class(function () {}) {};
  95. }
  96. public function testSomethingWithMoney(
  97. Money $amount
  98. ) {
  99. }
  100. }
  101. EOF
  102. ,
  103. <<<'EOF'
  104. <?php
  105. class MyTestWithAnonymousClass extends TestCase
  106. {
  107. function setUp()
  108. {
  109. $provider = new class(function () {}) {};
  110. }
  111. public function testSomethingWithMoney(
  112. Money $amount
  113. ) {
  114. }
  115. }
  116. EOF
  117. ,
  118. ];
  119. yield [
  120. <<<'EOF'
  121. <?php
  122. abstract class Foo {
  123. public function& foo0() {}
  124. public function & foo1() {}
  125. public function &foo2() {}
  126. protected function foo3($b) {}
  127. abstract protected function foo4();
  128. private function foo5() {}
  129. final public function foo6() {}
  130. abstract public function foo7();
  131. final public function foo8() {}
  132. abstract public function foo9();
  133. public static function fooA() {}
  134. public static function fooD() {}
  135. final public static function fooE() {}
  136. abstract public function fooF();
  137. public function fooG ($foo) {}
  138. public function fooH() {
  139. static $foo;
  140. $bar = function($baz) {};
  141. }
  142. }
  143. EOF
  144. ,
  145. <<<'EOF'
  146. <?php
  147. abstract class Foo {
  148. public function& foo0() {}
  149. public function & foo1() {}
  150. function &foo2() {}
  151. protected function foo3($b) {}
  152. protected
  153. abstract function foo4();
  154. private function foo5() {}
  155. final public function foo6() {}
  156. abstract public function foo7();
  157. public final function foo8() {}
  158. public abstract function foo9();
  159. public static function fooA() {}
  160. public static
  161. function fooD() {}
  162. final static function fooE() {}
  163. abstract function fooF();
  164. function fooG ($foo) {}
  165. function fooH() {
  166. static $foo;
  167. $bar = function($baz) {};
  168. }
  169. }
  170. EOF
  171. ,
  172. ];
  173. yield [
  174. <<<'EOF'
  175. <?php
  176. abstract class Foo1 {
  177. public function& foo0($a) {}
  178. }
  179. EOF
  180. ,
  181. <<<'EOF'
  182. <?php
  183. abstract class Foo1 {
  184. function& foo0($a) {}
  185. }
  186. EOF
  187. ,
  188. ];
  189. yield 'leave functions alone' => [<<<'EOF'
  190. <?php
  191. function foo() {
  192. static $foo;
  193. }
  194. EOF];
  195. yield 'leave functions alone with variables matching OOP words' => [<<<'EOF'
  196. <?php
  197. function foo() {
  198. static $class;
  199. $interface = 'foo';
  200. $trait = 'bar';
  201. }
  202. EOF];
  203. yield 'leave functions alone inside conditionals' => [<<<'EOF'
  204. <?php
  205. if (!function_exists('foo')) {
  206. function foo($arg)
  207. {
  208. return $arg;
  209. }
  210. }
  211. EOF];
  212. yield 'leave functions alone inside conditionals with OOP word in comment' => [<<<'EOF'
  213. <?php
  214. /* class <= this is just a stop-word */
  215. if (!function_exists('foo')) {
  216. function foo($arg)
  217. {
  218. return $arg;
  219. }
  220. }
  221. EOF];
  222. yield 'leave functions alone with OOP word in comment' => [<<<'EOF'
  223. <?php
  224. /* class */
  225. function foo($arg)
  226. {
  227. return $arg;
  228. }
  229. EOF];
  230. yield 'leave functions alone outside classes with OOP word in inline HTML' => [<<<'EOF'
  231. <?php
  232. if (!function_exists('foo')) {
  233. function foo($arg)
  234. {
  235. ?>
  236. <div class="test"></div>
  237. <?php
  238. return $arg;
  239. }
  240. }
  241. EOF];
  242. yield 'leave functions alone outside classes with OOP word in string value' => [<<<'EOF'
  243. <?php
  244. if (!function_exists('foo')) {
  245. function foo($arg)
  246. {
  247. return 'she has class right?';
  248. }
  249. }
  250. EOF];
  251. yield 'leave functions alone outside classes with OOP word in function name' => [<<<'EOF'
  252. <?php
  253. comment_class();
  254. if (!function_exists('foo')) {
  255. function foo($arg)
  256. {
  257. return $arg;
  258. }
  259. }
  260. EOF];
  261. yield 'leave functions alone after class' => [<<<'EOF'
  262. <?php
  263. class Foo
  264. {
  265. public $foo;
  266. }
  267. if (!function_exists('bar')) {
  268. function bar()
  269. {
  270. return 'bar';
  271. }
  272. }
  273. EOF];
  274. yield 'curly open syntax' => [<<<'EOF'
  275. <?php
  276. class Foo
  277. {
  278. private $bar;
  279. public function foo()
  280. {
  281. $foo = "foo";
  282. $fooA = "ab{$foo}cd";
  283. $bar = "bar"; // test if variable after T_CURLY_OPEN is intact
  284. }
  285. }
  286. EOF];
  287. yield 'dollar open curly braces syntax' => [<<<'EOF'
  288. <?php
  289. class Foo {
  290. public function bar()
  291. {
  292. $foo = "foo${width}foo";
  293. $bar = "bar"; // test if variable after T_DOLLAR_OPEN_CURLY_BRACES is intact
  294. }
  295. }
  296. EOF];
  297. yield 'leave JavaScript outside PHP alone' => [<<<'EOF'
  298. <?php
  299. function foo()
  300. {
  301. return "foo";
  302. }
  303. ?>
  304. <script type="text/javascript">
  305. function foo(bar) {
  306. alert(bar);
  307. }
  308. </script>
  309. EOF];
  310. yield 'leave JavaScript in string alone' => [<<<'EOF'
  311. <?php
  312. function registerJS()
  313. {
  314. echo '<script type="text/javascript">
  315. function foo(bar) {
  316. alert(bar);
  317. }
  318. </script>';
  319. }
  320. EOF];
  321. yield 'leave JavaScript in variable alone' => [<<<'EOF'
  322. <?php
  323. class Foo
  324. {
  325. public function bar()
  326. {
  327. $script = <<<JAVASCRIPT
  328. <script type="text/javascript">
  329. function foo(bar) {
  330. alert(bar);
  331. }
  332. </script>
  333. JAVASCRIPT;
  334. return $script;
  335. }
  336. }
  337. EOF];
  338. yield 'comma separated properties' => [<<<'EOF'
  339. <?php
  340. class Foo
  341. {
  342. public $foo1;
  343. private $foo2;
  344. protected $bar1, $bar2;
  345. public $baz1 = null, $baz2, $baz3 = false;
  346. public $foo, $bar;
  347. }
  348. EOF,
  349. <<<'EOF'
  350. <?php
  351. class Foo
  352. {
  353. var $foo1;
  354. private $foo2;
  355. protected $bar1, $bar2;
  356. public $baz1 = null, $baz2, $baz3 = false;
  357. var $foo, $bar;
  358. }
  359. EOF];
  360. yield 'var declarations with array value' => [<<<'EOF'
  361. <?php
  362. class Foo
  363. {
  364. public $foo1 = 1;
  365. public $foo2a = array('foo');
  366. public $foo2b = ['foo'];
  367. public $foo3a = array('foo', 'bar');
  368. public $foo3b = ['foo', 'bar'];
  369. public $foo4a = '1a', $foo5a = array(1, 2, 3), $foo6a = 10;
  370. public $foo4b = '1b', $foo5b = array(1, 2, 3), $foo6b = 10;
  371. }
  372. EOF,
  373. <<<'EOF'
  374. <?php
  375. class Foo
  376. {
  377. var $foo1 = 1;
  378. var $foo2a = array('foo');
  379. var $foo2b = ['foo'];
  380. var $foo3a = array('foo', 'bar');
  381. var $foo3b = ['foo', 'bar'];
  382. public $foo4a = '1a', $foo5a = array(1, 2, 3), $foo6a = 10;
  383. public $foo4b = '1b', $foo5b = array(1, 2, 3), $foo6b = 10;
  384. }
  385. EOF];
  386. yield [
  387. '<?php class A { public const B=1; }',
  388. '<?php class A { const B=1; }',
  389. ['elements' => ['const']],
  390. ];
  391. yield [
  392. '<?php class A { public const B=1;public const C=1;/**/public const#a
  393. D=1;public const E=1;//
  394. public const F=1; }',
  395. '<?php class A { const B=1;const C=1;/**/const#a
  396. D=1;const E=1;//
  397. const F=1; }',
  398. ['elements' => ['const']],
  399. ];
  400. yield [
  401. '<?php class A { private const B=1; protected const C=2; public const D=4; public $a; function A(){} }',
  402. '<?php class A { private const B=1; protected const C=2; const D=4; public $a; function A(){} }',
  403. ['elements' => ['const']],
  404. ];
  405. yield [
  406. '<?php
  407. class foo
  408. {
  409. public const A = 1, B =2, C =3;
  410. public const TWO = ONE * 2;
  411. public const THREE = ONE + self::TWO;
  412. public const SENTENCE = "The value of THREE is ".self::THREE;
  413. }
  414. ',
  415. '<?php
  416. class foo
  417. {
  418. const A = 1, B =2, C =3;
  419. const TWO = ONE * 2;
  420. const THREE = ONE + self::TWO;
  421. const SENTENCE = "The value of THREE is ".self::THREE;
  422. }
  423. ',
  424. ['elements' => ['const']],
  425. ];
  426. yield 'comment' => [
  427. '<?php
  428. class A
  429. {# We will have a function below
  430. # It will be static
  431. # and awesome
  432. public static function# <- this is the function
  433. AB# <- this is the name
  434. (#
  435. )#
  436. {#
  437. }#
  438. }
  439. ',
  440. '<?php
  441. class A
  442. {# We will have a function below
  443. static# It will be static
  444. # and awesome
  445. function# <- this is the function
  446. AB# <- this is the name
  447. (#
  448. )#
  449. {#
  450. }#
  451. }
  452. ',
  453. ];
  454. yield 'anonymous class' => [
  455. '<?php
  456. $a = new class() {
  457. public function a() {
  458. }
  459. };
  460. class C
  461. {
  462. public function A()
  463. {
  464. $a = new class() {public function a() {}};
  465. }
  466. }
  467. ',
  468. '<?php
  469. $a = new class() {
  470. function a() {
  471. }
  472. };
  473. class C
  474. {
  475. function A()
  476. {
  477. $a = new class() {function a() {}};
  478. }
  479. }
  480. ',
  481. ];
  482. yield 'removing newlines between keywords' => [
  483. '<?php
  484. class Foo
  485. {
  486. public $bar;
  487. final public static function bar() {}
  488. final public static function baz() {}
  489. }',
  490. '<?php
  491. class Foo
  492. {
  493. var
  494. $bar;
  495. final
  496. public
  497. static
  498. function bar() {}
  499. static
  500. final
  501. function baz() {}
  502. }',
  503. ];
  504. yield 'keeping comment' => [
  505. '<?php
  506. class Foo
  507. {
  508. /* constant */ private const BAR = 3;
  509. /* variable */ private $bar;
  510. /* function */ private function bar() {}
  511. }',
  512. '<?php
  513. class Foo
  514. {
  515. private /* constant */ const BAR = 3;
  516. private /* variable */ $bar;
  517. private /* function */ function bar() {}
  518. }',
  519. ['elements' => ['property', 'method', 'const']],
  520. ];
  521. yield 'fixing with all keywords' => [
  522. '<?php
  523. abstract class Foo
  524. {
  525. abstract protected static function fooA();
  526. abstract protected static function fooB();
  527. abstract protected static function fooC();
  528. abstract protected static function fooD();
  529. abstract protected static function fooE();
  530. abstract protected static function fooF();
  531. abstract public static function fooG();
  532. abstract public static function fooH();
  533. }
  534. ',
  535. '<?php
  536. abstract class Foo
  537. {
  538. abstract protected static function fooA();
  539. abstract static protected function fooB();
  540. protected abstract static function fooC();
  541. protected static abstract function fooD();
  542. static abstract protected function fooE();
  543. static protected abstract function fooF();
  544. abstract static function fooG();
  545. static abstract function fooH();
  546. }
  547. ',
  548. ];
  549. yield [
  550. '<?php class Foo { private int $foo; }',
  551. ];
  552. yield [
  553. '<?php class Foo { protected ?string $foo; }',
  554. ];
  555. yield [
  556. '<?php class Foo { public ? string $foo; }',
  557. ];
  558. yield [
  559. '<?php class Foo { public ? string $foo; }',
  560. '<?php class Foo { var ? string $foo; }',
  561. ];
  562. yield [
  563. '<?php class Foo { public static Foo\Bar $foo; }',
  564. '<?php class Foo { static public Foo\Bar $foo; }',
  565. ];
  566. yield [
  567. '<?php class Foo { public array $foo; }',
  568. ];
  569. yield [
  570. '<?php class Foo { public ?array $foo; }',
  571. ];
  572. yield [
  573. '<?php class Foo { public static ?array $foo; }',
  574. '<?php class Foo { static public ?array $foo; }',
  575. ];
  576. }
  577. /**
  578. * @requires PHP 8.0
  579. *
  580. * @dataProvider provideFix80Cases
  581. */
  582. public function testFix80(string $expected, ?string $input = null): void
  583. {
  584. $this->doTest($expected, $input);
  585. }
  586. public static function provideFix80Cases(): iterable
  587. {
  588. yield [
  589. '<?php class Foo { private int|float|null $foo; }',
  590. ];
  591. yield [
  592. '<?php class Foo { private int | /* or empty */ null $foo; }',
  593. ];
  594. yield [
  595. '<?php class Foo { private array|null $foo; }',
  596. ];
  597. yield [
  598. '<?php class Foo { private null|array $foo; }',
  599. ];
  600. yield [
  601. '<?php class Foo { public static null|array $foo; }',
  602. '<?php class Foo { static null|array $foo; }',
  603. ];
  604. }
  605. /**
  606. * @dataProvider provideFix81Cases
  607. *
  608. * @requires PHP 8.1
  609. */
  610. public function testFix81(string $expected, ?string $input = null): void
  611. {
  612. $this->doTest($expected, $input);
  613. }
  614. public static function provideFix81Cases(): iterable
  615. {
  616. yield [
  617. '<?php class Foo { public Foo1&Bar $foo; }',
  618. ];
  619. yield [
  620. '<?php
  621. class Foo
  622. {
  623. public readonly string $prop2a;
  624. }
  625. ',
  626. '<?php
  627. class Foo
  628. {
  629. readonly public string $prop2a;
  630. }
  631. ',
  632. ];
  633. yield [
  634. '<?php
  635. class Foo
  636. {
  637. public readonly string $prop1;
  638. public readonly string $prop2;
  639. }
  640. ',
  641. '<?php
  642. class Foo
  643. {
  644. readonly string $prop1;
  645. public readonly string $prop2;
  646. }
  647. ',
  648. ];
  649. yield [
  650. '<?php
  651. class Foo
  652. {
  653. final public const B = "2";
  654. }
  655. ',
  656. '<?php
  657. class Foo
  658. {
  659. public final const B = "2";
  660. }
  661. ',
  662. ];
  663. yield [
  664. '<?php
  665. class Foo
  666. {
  667. final public const B = "2";
  668. }
  669. ',
  670. '<?php
  671. class Foo
  672. {
  673. final const B = "2";
  674. }
  675. ',
  676. ];
  677. yield [
  678. '<?php
  679. enum Foo {
  680. case CAT;
  681. public function test(): self { return $this; }
  682. }
  683. var_dump(Foo::CAT->test());',
  684. '<?php
  685. enum Foo {
  686. case CAT;
  687. function test(): self { return $this; }
  688. }
  689. var_dump(Foo::CAT->test());',
  690. ];
  691. }
  692. /**
  693. * @requires PHP 8.2
  694. *
  695. * @dataProvider provideFix82Cases
  696. */
  697. public function testFix82(string $expected, ?string $input = null): void
  698. {
  699. $this->doTest($expected, $input);
  700. }
  701. public static function provideFix82Cases(): iterable
  702. {
  703. yield [
  704. '<?php trait Foo { public const Bar = 1; }',
  705. '<?php trait Foo { const Bar = 1; }',
  706. ];
  707. yield [
  708. '<?php class Foo {
  709. public (A&B)|C|D $x;
  710. protected A|(B&C)|D $y;
  711. private A|B|(C&D) $z;
  712. }',
  713. ];
  714. }
  715. /**
  716. * @param array<string, mixed> $config
  717. *
  718. * @dataProvider provideInvalidConfigurationCases
  719. */
  720. public function testInvalidConfiguration(array $config, string $expectedMessage): void
  721. {
  722. $this->expectException(InvalidFixerConfigurationException::class);
  723. $this->expectExceptionMessageMatches($expectedMessage);
  724. $this->fixer->configure($config);
  725. }
  726. /**
  727. * @return iterable<string, array{array<string, mixed>, string}>
  728. */
  729. public static function provideInvalidConfigurationCases(): iterable
  730. {
  731. yield 'invalid type' => [
  732. ['elements' => [null]],
  733. '/^\[visibility_required\] Invalid configuration: The option "elements" .*\.$/',
  734. ];
  735. yield 'invalid value' => [
  736. ['elements' => ['_unknown_']],
  737. '/^\[visibility_required\] Invalid configuration: The option "elements" .*\.$/',
  738. ];
  739. }
  740. }