123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?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\Casing;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Kuba Werłos <werlos@gmail.com>
- *
- * @covers \PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer
- *
- * @internal
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer>
- */
- final class LowercaseStaticReferenceFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php class Foo extends Bar { public function baz() { self::qux(); } }',
- '<?php class Foo extends Bar { public function baz() { SELF::qux(); } }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() { static::qux(); } }',
- '<?php class Foo extends Bar { public function baz() { STATIC::qux(); } }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
- '<?php class Foo extends Bar { public function baz() { PARENT::baz(); } }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
- '<?php class Foo extends Bar { public function baz() { Parent::baz(); } }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() { return new self(); } }',
- '<?php class Foo extends Bar { public function baz() { return new Self(); } }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() { return SelfFoo::FOO; } }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() { return FooSelf::FOO; } }',
- ];
- yield [
- '<?php class Foo extends Bar { private STATIC $baz; }',
- ];
- yield [
- '<?php class Foo extends Bar { STATIC private $baz; }',
- ];
- yield [
- '<?php class Foo extends Bar { public function paRent() {} }',
- ];
- yield [
- '<?php $foo->Self();',
- ];
- yield [
- '<?php Foo::Self();',
- ];
- yield [
- '<?php if ($foo instanceof self) { return true; }',
- '<?php if ($foo instanceof Self) { return true; }',
- ];
- yield [
- '<?php if ($foo instanceof static) { return true; }',
- '<?php if ($foo instanceof Static) { return true; }',
- ];
- yield [
- '<?php if ($foo instanceof parent) { return true; }',
- '<?php if ($foo instanceof Parent) { return true; }',
- ];
- yield [
- '<?php if ($foo instanceof Self\Bar) { return true; }',
- ];
- yield [
- '<?php if ($foo instanceof MySelf) { return true; }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz(self $x) {} }',
- '<?php class Foo extends Bar { public function baz(Self $x) {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz(parent $x) {} }',
- '<?php class Foo extends Bar { public function baz(Parent $x) {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz(MySelf $x) {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz(Self\Qux $x) {} }',
- ];
- yield [
- '<?php $a = STATIC function() {};',
- ];
- yield [
- '<?php class A { public function B() { STATIC $a; echo $a; }}',
- ];
- yield [
- '<?php class A { public function B() { $collection = $static ? new static($b) : new self(); } }',
- '<?php class A { public function B() { $collection = $static ? new STATIC($b) : new self(); } }',
- ];
- yield [
- '<?php class A { STATIC public function B() {} }',
- ];
- yield [
- '<?php
- $a = function () {
- STATIC $B = false;
- if ($B) {
- echo 1;
- }
- $B = true;
- };
- ',
- ];
- yield [
- '<?php class A { const PARENT = 42; }',
- ];
- yield [
- '<?php namespace Foo\Parent;',
- ];
- yield [
- '<?php namespace Parent\Foo;',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : self {} }',
- '<?php class Foo extends Bar { public function baz() : Self {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : parent {} }',
- '<?php class Foo extends Bar { public function baz() : Parent {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : MySelf {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : Self\Qux {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz(?self $x) {} }',
- '<?php class Foo extends Bar { public function baz(?Self $x) {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz(?parent $x) {} }',
- '<?php class Foo extends Bar { public function baz(?Parent $x) {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : ?self {} }',
- '<?php class Foo extends Bar { public function baz() : ?Self {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : ?parent {} }',
- '<?php class Foo extends Bar { public function baz() : ?Parent {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : ?MySelf {} }',
- ];
- yield [
- '<?php class Foo extends Bar { public function baz() : ?Self\Qux {} }',
- ];
- yield [
- '<?php class Foo {
- private STATIC int $baz1;
- private STATIC ?int $baz2;
- }',
- ];
- yield [
- '<?php
- class Foo { public function bar() {} }
- class FooChild extends Foo
- {
- public function bar()
- {
- switch (true) {
- case parent::bar():
- }
- }
- }',
- '<?php
- class Foo { public function bar() {} }
- class FooChild extends Foo
- {
- public function bar()
- {
- switch (true) {
- case PARENT::bar():
- }
- }
- }',
- ];
- }
- /**
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFix80Cases(): iterable
- {
- yield ['<?php $foo?->Self();'];
- yield [
- '<?php class Foo extends A {
- public function baz1() : int|parent {}
- public function baz2() : parent|int {}
- public function baz3() : ?parent {}
- }',
- '<?php class Foo extends A {
- public function baz1() : int|Parent {}
- public function baz2() : Parent|int {}
- public function baz3() : ?Parent {}
- }',
- ];
- yield [
- '<?php class Foo extends A {
- public function baz1() : int|static {}
- public function baz2() : static|int {}
- public function baz3() : ?static {}
- }',
- '<?php class Foo extends A {
- public function baz1() : int|STATIC {}
- public function baz2() : STATIC|int {}
- public function baz3() : ?STATIC {}
- }',
- ];
- yield [
- '<?php
- class Foo
- {
- private int|self $prop1, $prop2;
- private self|int $prop3, $prop4;
- }
- ',
- '<?php
- class Foo
- {
- private int|SELF $prop1, $prop2;
- private SELF|int $prop3, $prop4;
- }
- ',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield [
- '<?php class A { final const PARENT = 42; }',
- ];
- yield [
- '<?php enum Foo: string { case PARENT = \'parent\'; }',
- ];
- }
- /**
- * @dataProvider provideFix83Cases
- *
- * @requires PHP 8.3
- */
- public function testFix83(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: null|string}>
- */
- public static function provideFix83Cases(): iterable
- {
- yield [<<<'PHP'
- <?php
- class Foo {
- private const array PARENT = ['parent'];
- private const array SELF = ['self'];
- private const array STATIC = ['static'];
- }
- PHP];
- yield [<<<'PHP'
- <?php
- class Foo {
- private const int PARENT = 1;
- private const int SELF = 2;
- private const int STATIC = 3;
- }
- PHP];
- yield [<<<'PHP'
- <?php
- class Foo {
- private const int|static PARENT = 1;
- private const int|static SELF = 2;
- private const int|static STATIC = 3;
- }
- PHP];
- yield [<<<'PHP'
- <?php
- class Foo {
- private const string|(Bar&Baz) PARENT = 'parent';
- private const string|(Bar&Baz) SELF = 'self';
- private const string|(Bar&Baz) STATIC = 'static';
- }
- PHP];
- }
- }
|