123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- <?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\FunctionNotation;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @group phpdoc
- *
- * @covers \PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer
- */
- final class PhpdocToPropertyTypeFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, array $config = []): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield 'no phpdoc return' => [
- '<?php class Foo { private $foo; }',
- ];
- yield 'invalid return' => [
- '<?php class Foo { /** @var */ private $foo; }',
- ];
- yield 'invalid class 1' => [
- '<?php class Foo { /** @var \9 */ private $foo; }',
- ];
- yield 'invalid class 2' => [
- '<?php class Foo { /** @var \Foo\\\Bar */ private $foo; }',
- ];
- yield 'multiple returns' => [
- '<?php
- class Foo {
- /**
- * @var Bar
- * @var Baz
- */
- private $foo;
- }
- ',
- ];
- yield 'non-root class' => [
- '<?php class Foo { /** @var Bar */ private Bar $foo; }',
- '<?php class Foo { /** @var Bar */ private $foo; }',
- ];
- yield 'non-root namespaced class' => [
- '<?php class Foo { /** @var My\Bar */ private My\Bar $foo; }',
- '<?php class Foo { /** @var My\Bar */ private $foo; }',
- ];
- yield 'root class' => [
- '<?php class Foo { /** @var \My\Bar */ private \My\Bar $foo; }',
- '<?php class Foo { /** @var \My\Bar */ private $foo; }',
- ];
- yield 'void' => [
- '<?php class Foo { /** @var void */ private $foo; }',
- ];
- yield 'never' => [
- '<?php class Foo { /** @var never */ private $foo; }',
- ];
- yield 'iterable' => [
- '<?php class Foo { /** @var iterable */ private iterable $foo; }',
- '<?php class Foo { /** @var iterable */ private $foo; }',
- ];
- yield 'object' => [
- '<?php class Foo { /** @var object */ private object $foo; }',
- '<?php class Foo { /** @var object */ private $foo; }',
- ];
- yield 'fix scalar types by default, int' => [
- '<?php class Foo { /** @var int */ private int $foo; }',
- '<?php class Foo { /** @var int */ private $foo; }',
- ];
- yield 'fix scalar types by default, float' => [
- '<?php class Foo { /** @var float */ private float $foo; }',
- '<?php class Foo { /** @var float */ private $foo; }',
- ];
- yield 'fix scalar types by default, string' => [
- '<?php class Foo { /** @var string */ private string $foo; }',
- '<?php class Foo { /** @var string */ private $foo; }',
- ];
- yield 'fix scalar types by default, bool' => [
- '<?php class Foo { /** @var bool */ private bool $foo; }',
- '<?php class Foo { /** @var bool */ private $foo; }',
- ];
- yield 'fix scalar types by default, false' => [
- '<?php class Foo { /** @var false */ private bool $foo; }',
- '<?php class Foo { /** @var false */ private $foo; }',
- ];
- yield 'fix scalar types by default, true' => [
- '<?php class Foo { /** @var true */ private bool $foo; }',
- '<?php class Foo { /** @var true */ private $foo; }',
- ];
- yield 'do not fix scalar types when configured as such' => [
- '<?php class Foo { /** @var int */ private $foo; }',
- null,
- ['scalar_types' => false],
- ];
- yield 'do not fix union types when configured as such' => [
- '<?php class Foo { /** @var int|string */ private $foo; }',
- null,
- ['union_types' => false],
- ];
- yield 'array native type' => [
- '<?php class Foo { /** @var array */ private array $foo; }',
- '<?php class Foo { /** @var array */ private $foo; }',
- ];
- yield 'callable type' => [
- '<?php class Foo { /** @var callable */ private $foo; }',
- ];
- yield 'self accessor' => [
- '<?php class Foo { /** @var self */ private self $foo; }',
- '<?php class Foo { /** @var self */ private $foo; }',
- ];
- yield 'report static as self' => [
- '<?php class Foo { /** @var static */ private self $foo; }',
- '<?php class Foo { /** @var static */ private $foo; }',
- ];
- yield 'skip resource special type' => [
- '<?php class Foo { /** @var resource */ private $foo; }',
- ];
- yield 'null alone cannot be a property type' => [
- '<?php class Foo { /** @var null */ private $foo; }',
- ];
- yield 'nullable type' => [
- '<?php class Foo { /** @var null|Bar */ private ?Bar $foo; }',
- '<?php class Foo { /** @var null|Bar */ private $foo; }',
- ];
- yield 'nullable type with ? notation in phpDoc' => [
- '<?php class Foo { /** @var ?Bar */ private ?Bar $foo; }',
- '<?php class Foo { /** @var ?Bar */ private $foo; }',
- ];
- yield 'nullable type reverse order' => [
- '<?php class Foo { /** @var Bar|null */ private ?Bar $foo; }',
- '<?php class Foo { /** @var Bar|null */ private $foo; }',
- ];
- yield 'nullable native type' => [
- '<?php class Foo { /** @var null|array */ private ?array $foo; }',
- '<?php class Foo { /** @var null|array */ private $foo; }',
- ];
- yield 'nullable type with short notation' => [
- '<?php class Foo { /** @var ?array */ private ?array $foo; }',
- '<?php class Foo { /** @var ?array */ private $foo; }',
- ];
- yield 'nullable type with existing default value' => [
- '<?php class Foo { /** @var ?array */ private ?array $foo = []; }',
- '<?php class Foo { /** @var ?array */ private $foo = []; }',
- ];
- yield 'generics' => [
- '<?php class Foo { /** @var array<int, bool> */ private array $foo; }',
- '<?php class Foo { /** @var array<int, bool> */ private $foo; }',
- ];
- yield 'array of types' => [
- '<?php class Foo { /** @var Foo[] */ private array $foo; }',
- '<?php class Foo { /** @var Foo[] */ private $foo; }',
- ];
- yield 'array of array of types' => [
- '<?php class Foo { /** @var Foo[][] */ private array $foo; }',
- '<?php class Foo { /** @var Foo[][] */ private $foo; }',
- ];
- yield 'nullable array of types' => [
- '<?php class Foo { /** @var null|Foo[] */ private ?array $foo; }',
- '<?php class Foo { /** @var null|Foo[] */ private $foo; }',
- ];
- yield 'comments' => [
- '<?php
- class Foo
- {
- // comment 0
- /** @var Foo */ # comment 1
- public/**/Foo $foo/**/;# comment 2
- }
- ',
- '<?php
- class Foo
- {
- // comment 0
- /** @var Foo */ # comment 1
- public/**/$foo/**/;# comment 2
- }
- ',
- ];
- yield 'array and traversable' => [
- '<?php class Foo { /** @var array|Traversable */ private iterable $foo; }',
- '<?php class Foo { /** @var array|Traversable */ private $foo; }',
- ];
- yield 'array and traversable with leading slash' => [
- '<?php class Foo { /** @var array|\Traversable */ private iterable $foo; }',
- '<?php class Foo { /** @var array|\Traversable */ private $foo; }',
- ];
- yield 'array and traversable in a namespace' => [
- '<?php
- namespace App;
- class Foo {
- /** @var array|Traversable */
- private $foo;
- }
- ',
- ];
- yield 'array and traversable with leading slash in a namespace' => [
- '<?php
- namespace App;
- class Foo {
- /** @var array|\Traversable */
- private iterable $foo;
- }
- ',
- '<?php
- namespace App;
- class Foo {
- /** @var array|\Traversable */
- private $foo;
- }
- ',
- ];
- yield 'array and imported traversable in a namespace' => [
- '<?php
- namespace App;
- use Traversable;
- class Foo {
- /** @var array|Traversable */
- private iterable $foo;
- }
- ',
- '<?php
- namespace App;
- use Traversable;
- class Foo {
- /** @var array|Traversable */
- private $foo;
- }
- ',
- ];
- yield 'array and object aliased as traversable in a namespace' => [
- '<?php
- namespace App;
- use Bar as Traversable;
- class Foo {
- /** @var array|Traversable */
- private $foo;
- }
- ',
- null,
- ];
- yield 'array of object and traversable' => [
- '<?php class Foo { /** @var Foo[]|Traversable */ private iterable $foo; }',
- '<?php class Foo { /** @var Foo[]|Traversable */ private $foo; }',
- ];
- yield 'array of object and iterable' => [
- '<?php class Foo { /** @var Foo[]|iterable */ private iterable $foo; }',
- '<?php class Foo { /** @var Foo[]|iterable */ private $foo; }',
- ];
- yield 'array of string and array of int' => [
- '<?php class Foo { /** @var string[]|int[] */ private array $foo; }',
- '<?php class Foo { /** @var string[]|int[] */ private $foo; }',
- ];
- yield 'trait' => [
- '<?php trait Foo { /** @var int */ private int $foo; }',
- '<?php trait Foo { /** @var int */ private $foo; }',
- ];
- yield 'static property' => [
- '<?php class Foo { /** @var int */ private static int $foo; }',
- '<?php class Foo { /** @var int */ private static $foo; }',
- ];
- yield 'static property reverse order' => [
- '<?php class Foo { /** @var int */ static private int $foo; }',
- '<?php class Foo { /** @var int */ static private $foo; }',
- ];
- yield 'var' => [
- '<?php class Foo { /** @var int */ var int $foo; }',
- '<?php class Foo { /** @var int */ var $foo; }',
- ];
- yield 'with default value' => [
- '<?php class Foo { /** @var int */ public int $foo = 1; }',
- '<?php class Foo { /** @var int */ public $foo = 1; }',
- ];
- yield 'multiple properties of the same type' => [
- '<?php class Foo {
- /**
- * @var int $foo
- * @var int $bar
- */
- public int $foo, $bar;
- }',
- '<?php class Foo {
- /**
- * @var int $foo
- * @var int $bar
- */
- public $foo, $bar;
- }',
- ];
- yield 'multiple properties of different types' => [
- '<?php class Foo {
- /**
- * @var int $foo
- * @var string $bar
- */
- public $foo, $bar;
- }',
- ];
- yield 'single property with different annotations' => [
- '<?php class Foo {
- /**
- * @var int $foo
- * @var string $foo
- */
- public $foo;
- }',
- ];
- yield 'multiple properties with missing annotation' => [
- '<?php class Foo {
- /**
- * @var int $foo
- */
- public $foo, $bar;
- }',
- ];
- yield 'multiple properties with annotation without name' => [
- '<?php class Foo {
- /**
- * @var int
- * @var int $bar
- */
- public $foo, $bar;
- }',
- ];
- yield 'multiple properties with annotation without name reverse order' => [
- '<?php class Foo {
- /**
- * @var int $foo
- * @var int
- */
- public $foo, $bar;
- }',
- ];
- yield 'multiple properties with extra annotations' => [
- '<?php class Foo {
- /**
- * @var string
- * @var int $foo
- * @var int $bar
- * @var int
- */
- public int $foo, $bar;
- }',
- '<?php class Foo {
- /**
- * @var string
- * @var int $foo
- * @var int $bar
- * @var int
- */
- public $foo, $bar;
- }',
- ];
- yield 'abstract method' => [
- '<?php abstract class Foo {
- /** @var Bar */ private Bar $foo;
- public abstract function getFoo();
- }',
- '<?php abstract class Foo {
- /** @var Bar */ private $foo;
- public abstract function getFoo();
- }',
- ];
- yield 'great number of properties' => [
- '<?php class Foo {
- /** @var string */
- private string $foo1;
- /** @var string */
- private string $foo2;
- /** @var int */
- private int $foo3;
- /** @var string */
- private string $foo4;
- /** @var string */
- private string $foo5;
- /** @var string */
- private string $foo6;
- /** @var string */
- private string $foo7;
- /** @var int */
- private int $foo8;
- /** @var string */
- private string $foo9;
- /** @var int|null */
- private ?int $foo10;
- }',
- '<?php class Foo {
- /** @var string */
- private $foo1;
- /** @var string */
- private $foo2;
- /** @var int */
- private $foo3;
- /** @var string */
- private $foo4;
- /** @var string */
- private $foo5;
- /** @var string */
- private $foo6;
- /** @var string */
- private $foo7;
- /** @var int */
- private $foo8;
- /** @var string */
- private $foo9;
- /** @var int|null */
- private $foo10;
- }',
- ];
- yield 'anonymous class' => [
- '<?php new class { /** @var int */ private int $foo; };',
- '<?php new class { /** @var int */ private $foo; };',
- ];
- yield 'intersection types' => [
- '<?php class Foo { /** @var Bar&Baz */ private $x; }',
- ];
- yield 'very long class name before ampersand' => [
- '<?php class Foo { /** @var Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar&Baz */ private $x; }',
- ];
- yield 'very long class name after ampersand' => [
- '<?php class Foo { /** @var Bar&Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz */ private $x; }',
- ];
- }
- /**
- * @dataProvider provideFixPre80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPre80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string}>
- */
- public static function provideFixPre80Cases(): iterable
- {
- yield 'skip mixed type' => [
- '<?php class Foo { /** @var mixed */ private $foo; }',
- ];
- yield 'skip union types' => [
- '<?php class Foo { /** @var Foo|Bar */ private $foo; }',
- ];
- yield 'skip union nullable types' => [
- '<?php class Foo { /** @var null|Foo|Bar */ private $foo; }',
- ];
- }
- /**
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string, string}>
- */
- public static function provideFix80Cases(): iterable
- {
- yield 'fix mixed type' => [
- '<?php class Foo { /** @var mixed */ private mixed $foo; }',
- '<?php class Foo { /** @var mixed */ private $foo; }',
- ];
- yield 'union types' => [
- '<?php class Foo { /** @var Foo|Bar */ private Foo|Bar $foo; }',
- '<?php class Foo { /** @var Foo|Bar */ private $foo; }',
- ];
- yield 'union types including nullable' => [
- '<?php class Foo { /** @var null|Foo|Bar */ private Foo|Bar|null $foo; }',
- '<?php class Foo { /** @var null|Foo|Bar */ private $foo; }',
- ];
- yield 'union types including generics' => [
- '<?php class Foo { /** @var string|array<int, bool> */ private string|array $foo; }',
- '<?php class Foo { /** @var string|array<int, bool> */ private $foo; }',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected): void
- {
- $this->doTest($expected);
- }
- /**
- * @return iterable<string, array{string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'readonly properties are always typed, make sure the fixer does not crash' => [
- '<?php class Foo { /** @var int */ private readonly string $foo; }',
- ];
- }
- }
|