123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\ClassNotation;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Filippo Tessarotto <zoeslam@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\ClassNotation\ProtectedToPrivateFixer
- */
- final class ProtectedToPrivateFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public function provideFixCases(): array
- {
- $attributesAndMethodsOriginal = $this->getAttributesAndMethods(true);
- $attributesAndMethodsFixed = $this->getAttributesAndMethods(false);
- return [
- 'final-extends' => [
- "<?php final class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }",
- ],
- 'normal-extends' => [
- "<?php class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }",
- ],
- 'abstract' => [
- "<?php abstract class MyAbstractClass { {$attributesAndMethodsOriginal} }",
- ],
- 'normal' => [
- "<?php class MyClass { {$attributesAndMethodsOriginal} }",
- ],
- 'trait' => [
- "<?php trait MyTrait { {$attributesAndMethodsOriginal} }",
- ],
- 'final-with-trait' => [
- "<?php final class MyClass { use MyTrait; {$attributesAndMethodsOriginal} }",
- ],
- 'multiline-comment' => [
- '<?php final class MyClass { /* public protected private */ }',
- ],
- 'inline-comment' => [
- "<?php final class MyClass { \n // public protected private \n }",
- ],
- 'final' => [
- "<?php final class MyClass { {$attributesAndMethodsFixed} } class B {use C;}",
- "<?php final class MyClass { {$attributesAndMethodsOriginal} } class B {use C;}",
- ],
- 'final-implements' => [
- "<?php final class MyClass implements MyInterface { {$attributesAndMethodsFixed} }",
- "<?php final class MyClass implements MyInterface { {$attributesAndMethodsOriginal} }",
- ],
- 'final-with-use-before' => [
- "<?php use stdClass; final class MyClass { {$attributesAndMethodsFixed} }",
- "<?php use stdClass; final class MyClass { {$attributesAndMethodsOriginal} }",
- ],
- 'final-with-use-after' => [
- "<?php final class MyClass { {$attributesAndMethodsFixed} } use stdClass;",
- "<?php final class MyClass { {$attributesAndMethodsOriginal} } use stdClass;",
- ],
- 'multiple-classes' => [
- "<?php final class MyFirstClass { {$attributesAndMethodsFixed} } class MySecondClass { {$attributesAndMethodsOriginal} } final class MyThirdClass { {$attributesAndMethodsFixed} } ",
- "<?php final class MyFirstClass { {$attributesAndMethodsOriginal} } class MySecondClass { {$attributesAndMethodsOriginal} } final class MyThirdClass { {$attributesAndMethodsOriginal} } ",
- ],
- 'minimal-set' => [
- '<?php final class MyClass { private $v1; }',
- '<?php final class MyClass { protected $v1; }',
- ],
- 'anonymous-class-inside' => [
- "<?php
- final class Foo
- {
- {$attributesAndMethodsFixed}
- private function bar()
- {
- new class {
- {$attributesAndMethodsOriginal}
- };
- }
- }
- ",
- "<?php
- final class Foo
- {
- {$attributesAndMethodsOriginal}
- protected function bar()
- {
- new class {
- {$attributesAndMethodsOriginal}
- };
- }
- }
- ",
- ],
- [
- '<?php $a = new class{protected function A(){ echo 123; }};',
- ],
- ];
- }
- /**
- * @dataProvider provideFix74Cases
- * @requires PHP 7.4
- */
- public function test74Fix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public function provideFix74Cases(): \Generator
- {
- yield [
- '<?php final class Foo { private int $foo; }',
- '<?php final class Foo { protected int $foo; }',
- ];
- yield [
- '<?php final class Foo { private ?string $foo; }',
- '<?php final class Foo { protected ?string $foo; }',
- ];
- yield [
- '<?php final class Foo { private array $foo; }',
- '<?php final class Foo { protected array $foo; }',
- ];
- }
- /**
- * @dataProvider provideFix80Cases
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFix80Cases(): \Generator
- {
- yield [
- '<?php
- final class Foo2 {
- private int|float $a;
- }
- ',
- '<?php
- final class Foo2 {
- protected int|float $a;
- }
- ',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFix81Cases(): \Generator
- {
- yield [
- '<?php
- final class Foo { private readonly int $d; }
- ',
- '<?php
- final class Foo { protected readonly int $d; }
- ',
- ];
- yield [
- // '<?php final class Foo { final private const Y = "i"; }', 'Fatal error: Private constant Foo::Y cannot be final as it is not visible to other classes on line 1.
- '<?php
- final class Foo1 { final protected const Y = "abc"; }
- final class Foo2 { protected final const Y = "def"; }
- ',
- ];
- yield [
- '<?php final class Foo { private Foo1&Bar $foo; }',
- '<?php final class Foo { protected Foo1&Bar $foo; }',
- ];
- }
- private function getAttributesAndMethods(bool $original): string
- {
- $attributesAndMethodsOriginal = '
- public $v1;
- protected $v2;
- private $v3;
- public static $v4;
- protected static $v5;
- private static $v6;
- public function f1(){}
- protected function f2(){}
- private function f3(){}
- public static function f4(){}
- protected static function f5(){}
- private static function f6(){}
- ';
- if ($original) {
- return $attributesAndMethodsOriginal;
- }
- return str_replace('protected', 'private', $attributesAndMethodsOriginal);
- }
- }
|