123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?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\NativeFunctionTypeDeclarationCasingFixer
- */
- final class NativeFunctionTypeDeclarationCasingFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- 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;',
- ];
- }
- /**
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- 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;',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- 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;}}',
- ];
- }
- /**
- * @dataProvider provideFix82Cases
- *
- * @requires PHP 8.2
- */
- public function testFix82(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- 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;',
- ];
- 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)),
- ];
- }
- }
- }
|