123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- <?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;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Casing\NativeTypeDeclarationCasingFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\NativeTypeDeclarationCasingFixer>
- */
- final class NativeTypeDeclarationCasingFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php
- function A(int $a): void {}
- class Foo
- {
- private bool $c = false;
- private bool $d = false;
- public function B(int $a): bool { return $this->c || $this->d; }
- }
- function C(float $a): array { return [$a];}
- function D(array $a): array { return [$a];}
- ',
- '<?php
- function A(INT $a): VOID {}
- class Foo
- {
- private BOOL $c = false;
- private BOOL $d = false;
- public function B(INT $a): BOOL { return $this->c || $this->d; }
- }
- function C(FLOAT $a): ARRAY { return [$a];}
- function D(ARRAY $a): ARRAY { return [$a];}
- ',
- ];
- yield [
- '<?php
- class Foo
- {
- private function Bar(array $bar) {
- return false;
- }
- }
- ',
- '<?php
- class Foo
- {
- private function Bar(ARRAY $bar) {
- return false;
- }
- }
- ',
- ];
- yield [
- '<?php
- interface Foo
- {
- public function Bar(array $bar);
- }
- ',
- '<?php
- interface Foo
- {
- public function Bar(ArrAY $bar);
- }
- ',
- ];
- yield [
- '<?php
- function Foo(/**/array/**/$bar) {
- return false;
- }
- ',
- '<?php
- function Foo(/**/ARRAY/**/$bar) {
- return false;
- }
- ',
- ];
- yield [
- '<?php
- class Bar { function Foo(array $a, callable $b, self $c) {} }
- ',
- '<?php
- class Bar { function Foo(ARRAY $a, CALLABLE $b, Self $c) {} }
- ',
- ];
- yield [
- '<?php
- function Foo(INTEGER $a) {}
- ',
- ];
- yield [
- '<?php function Foo(
- String\A $x,
- B\String\C $y
- ) {}',
- ];
- yield [
- '<?php final class Foo1 { final public function Foo(bool $A, float $B, int $C, string $D): int {} }',
- '<?php final class Foo1 { final public function Foo(BOOL $A, FLOAT $B, INT $C, STRING $D): INT {} }',
- ];
- yield [
- '<?php function Foo(bool $A, float $B, int $C, string $D): int {}',
- '<?php function Foo(BOOL $A, FLOAT $B, INT $C, STRING $D): INT {}',
- ];
- yield [
- '<?php function Foo(): Foo\A { return new Foo(); }',
- ];
- yield [
- '<?php trait XYZ { function Foo(iterable $A): void {} }',
- '<?php trait XYZ { function Foo(ITERABLE $A): VOID {} }',
- ];
- yield [
- '<?php function Foo(iterable $A): void {}',
- '<?php function Foo(ITERABLE $A): VOID {}',
- ];
- yield [
- '<?php function Foo(?int $A): void {}',
- '<?php function Foo(?INT $A): VOID {}',
- ];
- yield [
- '<?php function Foo(string $A): ?/* */int {}',
- '<?php function Foo(STRING $A): ?/* */INT {}',
- ];
- yield [
- '<?php function Foo(object $A): void {}',
- '<?php function Foo(OBJECT $A): VOID {}',
- ];
- yield [
- '<?php return function (callable $c) {};',
- '<?php return function (CALLABLE $c) {};',
- ];
- yield [
- '<?php return fn (callable $c): int => 1;',
- '<?php return fn (CALLABLE $c): INT => 1;',
- ];
- yield [
- '<?php
- class Foo
- {
- const A = 1;
- const B = [];
- const INT = "A"; // class constant; INT is the name of the const, not the type
- const FLOAT=1.2;
- }
- const INT = "A"; // outside class; INT is the name of the const, not the type
- ',
- ];
- yield 'class properties single type' => [
- '<?php
- class D{}
- $a = new class extends D {
- private array $ax;
- private bool $bx = false;
- private float $cx = 3.14;
- private int $dx = 667;
- private iterable $ex = [];
- private mixed $f;
- private object $g;
- private parent $h;
- private self $i;
- private static $j;
- private ?string $k;
- private $INT = 1;
- private FOO $bar;
- private A\INT\B $z;
- };
- ',
- '<?php
- class D{}
- $a = new class extends D {
- private ARRAY $ax;
- private BOOL $bx = false;
- private FLOAT $cx = 3.14;
- private INT $dx = 667;
- private ITERABLE $ex = [];
- private MIXED $f;
- private OBJECT $g;
- private PARENT $h;
- private Self $i;
- private STatic $j;
- private ?STRIng $k;
- private $INT = 1;
- private FOO $bar;
- private A\INT\B $z;
- };
- ',
- ];
- yield 'var keyword' => [
- '<?php class Foo {
- var $bar;
- }',
- ];
- yield 'static property without type' => [
- '<?php class Foo { static $bar; }',
- '<?php class Foo { STATIC $bar; }',
- ];
- }
- /**
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{string, string}>
- */
- public static function provideFix80Cases(): iterable
- {
- yield [
- '<?php class T { public function Foo(object $A): static {}}',
- '<?php class T { public function Foo(object $A): StatiC {}}',
- ];
- yield [
- '<?php class T { public function Foo(object $A): ?static {}}',
- '<?php class T { public function Foo(object $A): ?StatiC {}}',
- ];
- yield [
- '<?php class T { public function Foo(mixed $A): mixed {}}',
- '<?php class T { public function Foo(Mixed $A): MIXED {}}',
- ];
- yield 'mixed in arrow function' => [
- '<?php return fn (mixed $c): mixed => 1;',
- '<?php return fn (MiXeD $c): MIXED => 1;',
- ];
- yield [
- '<?php function foo(int|bool $x) {}',
- '<?php function foo(INT|BOOL $x) {}',
- ];
- yield [
- '<?php function foo(int | bool $x) {}',
- '<?php function foo(INT | BOOL $x) {}',
- ];
- yield [
- '<?php function foo(): int|bool {}',
- '<?php function foo(): INT|BOOL {}',
- ];
- yield 'return type string|false' => [
- '<?php function foo(): string|false {}',
- '<?php function foo(): string|FALSE {}',
- ];
- yield 'return type string|null' => [
- '<?php function foo(): string|null {}',
- '<?php function foo(): string|NULL {}',
- ];
- yield 'union types in arrow function' => [
- '<?php return fn (string|null $c): int|null => 1;',
- '<?php return fn (string|NULL $c): INT|NULL => 1;',
- ];
- yield 'union Types' => [
- '<?php $a = new class {
- private null|int|bool $a4 = false;
- };',
- '<?php $a = new class {
- private NULL|INT|BOOL $a4 = false;
- };',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string, string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'return type `never`' => [
- '<?php class T { public function Foo(object $A): never {die;}}',
- '<?php class T { public function Foo(object $A): NEVER {die;}}',
- ];
- yield 'class readonly property' => [
- '<?php class Z {
- private readonly array $ax;
- };',
- '<?php class Z {
- private readonly ARRAY $ax;
- };',
- ];
- }
- /**
- * @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 'disjunctive normal form types in arrow function' => [
- '<?php return fn ((A&B)|C|null $c): (X&Y)|Z|null => 1;',
- '<?php return fn ((A&B)|C|Null $c): (X&Y)|Z|NULL => 1;',
- ];
- yield 'iterable: disjunctive normal form types in arrow function' => [
- '<?php return fn (iterable|C|null $c): (X&Y)|Z|null => 1;',
- '<?php return fn (ITERABLE|C|Null $c): (X&Y)|Z|NULL => 1;',
- ];
- foreach (['true', 'false', 'null'] as $type) {
- yield \sprintf('standalone type `%s` in class method', $type) => [
- \sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', $type),
- \sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', strtoupper($type)),
- ];
- yield \sprintf('standalone type `%s` in function', $type) => [
- \sprintf('<?php function Foo(%s $A): %1$s {return $A;}', $type),
- \sprintf('<?php function Foo(%s $A): %1$s {return $A;}', strtoupper($type)),
- ];
- yield \sprintf('standalone type `%s` in closure', $type) => [
- \sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', $type),
- \sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', strtoupper($type)),
- ];
- yield \sprintf('standalone type `%s` in arrow function', $type) => [
- \sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', $type),
- \sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', strtoupper($type)),
- ];
- }
- yield 'intersection Types' => [
- '<?php $a = new class {
- private (A&B)|int|D $d5;
- private (A\STRING\B&B\INT\C)|int|(A&B) $e6;
- };',
- '<?php $a = new class {
- private (A&B)|INT|D $d5;
- private (A\STRING\B&B\INT\C)|int|(A&B) $e6;
- };',
- ];
- }
- /**
- * @dataProvider provideFix83Cases
- *
- * @requires PHP 8.3
- */
- public function testFix83(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{0: string, 1?: string}>
- */
- public static function provideFix83Cases(): iterable
- {
- yield 'simple case' => [
- '<?php
- class Foo
- {
- const int FOO = 6;
- }
- ',
- '<?php
- class Foo
- {
- const INT FOO = 6;
- }
- ',
- ];
- yield 'single types' => [
- '<?php
- class Foo
- {
- const int SOME_INT = 3;
- const array SOME_ARRAY = [7];
- const float SOME_FLOAT = 1.23;
- const iterable SOME_ITERABLE = [1, 2];
- const mixed SOME_MIXED = 1;
- const null SOME_NULL = NULL;
- const string SOME_STRING = "X";
- }
- ',
- '<?php
- class Foo
- {
- const INT SOME_INT = 3;
- const ARRAY SOME_ARRAY = [7];
- const Float SOME_FLOAT = 1.23;
- const ITERABLE SOME_ITERABLE = [1, 2];
- const MIXED SOME_MIXED = 1;
- const NULL SOME_NULL = NULL;
- const STRING SOME_STRING = "X";
- }
- ',
- ];
- yield 'nullables `self`, `parent` and `object`' => [
- '<?php
- class Foo extends FooParent
- {
- const ?object SOME_OBJECT = NULL;
- const ?parent SOME_PARENT = NULL;
- const ?self SOME_SELF = NULL;
- const ?int/* x */A/* y */= 3;
- const ?BAR B = null;
- const ?BAR C = null;
- const ?\BAR D = null;
- const ?\INT\B E = null;
- public const ?INT\A/* x */X=C;
- }
- ',
- '<?php
- class Foo extends FooParent
- {
- const ?OBJECT SOME_OBJECT = NULL;
- const ?PARENT SOME_PARENT = NULL;
- const ?Self SOME_SELF = NULL;
- const ?INT/* x */A/* y */= 3;
- const ?BAR B = null;
- const ?BAR C = null;
- const ?\BAR D = null;
- const ?\INT\B E = null;
- public const ?INT\A/* x */X=C;
- }
- ',
- ];
- yield 'simple `|`' => [
- '<?php class Foo1 {
- const D|float BAR = 1.0;
- }',
- '<?php class Foo1 {
- const D|Float BAR = 1.0;
- }',
- ];
- yield 'multiple `|`' => [
- '<?php class Foo2 {
- const int|array|null|A\B|\C\D|float BAR_1 = NULL;
- }',
- '<?php class Foo2 {
- const INT|ARRAY|NULL|A\B|\C\D|FLOAT BAR_1 = NULL;
- }',
- ];
- yield 'handle mix of `|` and `(X&Y)`' => [
- '<?php
- class Foo extends FooParent
- {
- private const Z|A\S\T\R|int|float|iterable SOMETHING = 123;
- protected const \A\B|(Foo & Stringable )|null|\X D = null;
- public const Foo&Stringable G = V::A;
- }
- ',
- '<?php
- class Foo extends FooParent
- {
- private const Z|A\S\T\R|INT|FLOAT|ITERABLE SOMETHING = 123;
- protected const \A\B|(Foo & Stringable )|NULL|\X D = null;
- public const Foo&Stringable G = V::A;
- }
- ',
- ];
- yield 'interface, nullable int' => [
- '<?php interface SomeInterface {
- const ?int FOO = 1;
- }',
- '<?php interface SomeInterface {
- const ?INT FOO = 1;
- }',
- ];
- yield 'trait, string' => [
- '<?php trait T {
- const string TEST = E::TEST;
- }',
- '<?php trait T {
- const STRING TEST = E::TEST;
- }',
- ];
- yield 'enum, int' => [
- '<?php enum E: string {
- case Hearts = "H";
- const int TEST = 789;
- const self A = self::Hearts;
- const static B = self::Hearts;
- }',
- '<?php enum E: string {
- case Hearts = "H";
- const INT TEST = 789;
- const SELF A = self::Hearts;
- const STATIC B = self::Hearts;
- }',
- ];
- yield 'do not fix' => [
- '<?php class Foo {
- PUBLIC CONST FOO&STRINGABLE G = A::B;
- }
- CONST A = 1;',
- ];
- }
- }
|