123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?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
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\ProtectedToPrivateFixer>
- */
- final class ProtectedToPrivateFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- $attributesAndMethodsOriginal = self::getAttributesAndMethods(true);
- $attributesAndMethodsFixed = self::getAttributesAndMethods(false);
- yield 'final-extends' => [
- "<?php final class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }",
- ];
- yield 'normal-extends' => [
- "<?php class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }",
- ];
- yield 'abstract' => [
- "<?php abstract class MyAbstractClass { {$attributesAndMethodsOriginal} }",
- ];
- yield 'normal' => [
- "<?php class MyClass { {$attributesAndMethodsOriginal} }",
- ];
- yield 'trait' => [
- "<?php trait MyTrait { {$attributesAndMethodsOriginal} }",
- ];
- yield 'final-with-trait' => [
- "<?php final class MyClass { use MyTrait; {$attributesAndMethodsOriginal} }",
- ];
- yield 'multiline-comment' => [
- '<?php final class MyClass { /* public protected private */ }',
- ];
- yield 'inline-comment' => [
- "<?php final class MyClass { \n // public protected private \n }",
- ];
- yield 'final' => [
- "<?php final class MyClass { {$attributesAndMethodsFixed} } class B {use C;}",
- "<?php final class MyClass { {$attributesAndMethodsOriginal} } class B {use C;}",
- ];
- yield 'final-implements' => [
- "<?php final class MyClass implements MyInterface { {$attributesAndMethodsFixed} }",
- "<?php final class MyClass implements MyInterface { {$attributesAndMethodsOriginal} }",
- ];
- yield 'final-with-use-before' => [
- "<?php use stdClass; final class MyClass { {$attributesAndMethodsFixed} }",
- "<?php use stdClass; final class MyClass { {$attributesAndMethodsOriginal} }",
- ];
- yield 'final-with-use-after' => [
- "<?php final class MyClass { {$attributesAndMethodsFixed} } use stdClass;",
- "<?php final class MyClass { {$attributesAndMethodsOriginal} } use stdClass;",
- ];
- yield '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} } ",
- ];
- yield 'minimal-set' => [
- '<?php final class MyClass { private $v1; }',
- '<?php final class MyClass { protected $v1; }',
- ];
- yield '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}
- };
- }
- }
- ",
- ];
- yield [
- '<?php $a = new class{protected function A(){ echo 123; }};',
- ];
- 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);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFix80Cases(): iterable
- {
- 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);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield [
- '<?php
- final class Foo { private readonly int $d; }
- ',
- '<?php
- final class Foo { protected readonly int $d; }
- ',
- ];
- yield 'protected final const' => [
- // '<?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 Foo2 { private const X = "tty"; }',
- '<?php final class Foo2 { protected const X = "tty"; }',
- ];
- yield [
- '<?php final class Foo { private Foo1&Bar $foo; }',
- '<?php final class Foo { protected Foo1&Bar $foo; }',
- ];
- // https://wiki.php.net/rfc/enumerations
- // Methods may be public, private, or protected, although in practice private and protected are equivalent as inheritance is not allowed.
- yield 'enum' => [
- '<?php
- enum Foo: string
- {
- private const Spades = 123;
- case Hearts = "H";
- private function test() {
- echo 123;
- }
- }
- Foo::Hearts->test();
- ',
- '<?php
- enum Foo: string
- {
- protected const Spades = 123;
- case Hearts = "H";
- protected function test() {
- echo 123;
- }
- }
- Foo::Hearts->test();
- ',
- ];
- yield 'enum with trait' => [
- '<?php
- trait NamedDocumentStatus
- {
- public function getStatusName(): string
- {
- return $this->getFoo();
- }
- }
- enum DocumentStats {
- use NamedDocumentStatus;
- case DRAFT;
- private function getFoo(): string {
- return "X";
- }
- }
- echo DocumentStats::DRAFT->getStatusName();
- ',
- '<?php
- trait NamedDocumentStatus
- {
- public function getStatusName(): string
- {
- return $this->getFoo();
- }
- }
- enum DocumentStats {
- use NamedDocumentStatus;
- case DRAFT;
- protected function getFoo(): string {
- return "X";
- }
- }
- echo DocumentStats::DRAFT->getStatusName();
- ',
- ];
- }
- /**
- * @dataProvider provideFix82Cases
- *
- * @requires PHP 8.2
- */
- public function testFix82(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string, string}>
- */
- public static function provideFix82Cases(): iterable
- {
- yield 'final readonly' => [
- '<?php
- final readonly class Foo {
- private function noop(): void{}
- }',
- '<?php
- final readonly class Foo {
- protected function noop(): void{}
- }',
- ];
- yield 'final readonly reversed' => [
- '<?php
- readonly final class Foo {
- private function noop(): void{}
- }',
- '<?php
- readonly final class Foo {
- protected function noop(): void{}
- }',
- ];
- }
- private static 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);
- }
- }
|