VisibilityRequiredFixerTest.php 18 KB

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