123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 |
- <?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\Phpdoc;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Graham Campbell <hello@gjcampbell.co.uk>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocVarWithoutNameFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocVarWithoutNameFixer>
- */
- final class PhpdocVarWithoutNameFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixVarCases
- */
- public function testFixVar(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @dataProvider provideFixVarCases
- */
- public function testFixType(string $expected, ?string $input = null): void
- {
- $expected = str_replace('@var', '@type', $expected);
- if (null !== $input) {
- $input = str_replace('@var', '@type', $input);
- }
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixVarCases(): iterable
- {
- yield 'testFixVar' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var string Hello!
- */
- public $foo;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var string $foo Hello!
- */
- public $foo;
- }
- EOF,
- ];
- yield 'testFixType' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var int|null
- */
- public $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var int|null $bar
- */
- public $bar;
- }
- EOF,
- ];
- yield 'testDoNothing' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var Foo\Bar This is a variable.
- */
- public $bar;
- }
- EOF,
- ];
- yield 'testFixVarWithNestedKeys' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var array {
- * @var bool $required Whether this element is required
- * @var string $label The display name for this element
- * }
- */
- public $options;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var array $options {
- * @var bool $required Whether this element is required
- * @var string $label The display name for this element
- * }
- */
- public $options;
- }
- EOF,
- ];
- yield 'testSingleLine' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar */
- public $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar $bar */
- public $bar;
- }
- EOF,
- ];
- yield 'testSingleLineProtected' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar */
- protected $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar $bar */
- protected $bar;
- }
- EOF,
- ];
- yield 'testSingleLinePrivate' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar */
- private $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar $bar */
- private $bar;
- }
- EOF,
- ];
- yield 'testSingleLineVar' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar */
- var $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar $bar */
- var $bar;
- }
- EOF,
- ];
- yield 'testSingleLineStatic' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar */
- static public $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar $bar */
- static public $bar;
- }
- EOF,
- ];
- yield 'testSingleLineNoSpace' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar*/
- public $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var Foo\Bar $bar*/
- public $bar;
- }
- EOF,
- ];
- yield 'testInlineDoc' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * Initializes this class with the given options.
- *
- * @param array $options {
- * @var bool $required Whether this element is required
- * @var string $label The display name for this element
- * }
- */
- public function init($options)
- {
- // Do something
- }
- }
- EOF,
- ];
- yield 'testSingleLineNoProperty' => [
- <<<'EOF'
- <?php
- /** @var Foo\Bar $bar */
- $bar;
- EOF,
- ];
- yield 'testMultiLineNoProperty' => [
- <<<'EOF'
- <?php
- /**
- * @var Foo\Bar $bar
- */
- $bar;
- EOF,
- ];
- yield 'testVeryNestedInlineDoc' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var array {
- * @var array $secondLevelOne {
- * {@internal This should not break}
- * @var int $thirdLevel
- * }
- * @var array $secondLevelTwo {
- * @var array $thirdLevel {
- * @var string $fourthLevel
- * }
- * @var int $moreThirdLevel
- * }
- * @var int $secondLevelThree
- * }
- */
- public $nestedFoo;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var array $nestedFoo {
- * @var array $secondLevelOne {
- * {@internal This should not break}
- * @var int $thirdLevel
- * }
- * @var array $secondLevelTwo {
- * @var array $thirdLevel {
- * @var string $fourthLevel
- * }
- * @var int $moreThirdLevel
- * }
- * @var int $secondLevelThree
- * }
- */
- public $nestedFoo;
- }
- EOF,
- ];
- yield [
- '<?php
- class Foo
- {
- /**
- * @no_candidate string Hello!
- */
- public $foo;
- }
- ',
- ];
- yield [
- '<?php
- class Foo{}
- /** */',
- ];
- yield 'anonymousClass' => [
- <<<'EOF'
- <?php
- class Anon
- {
- public function getNewAnon()
- {
- return new class()
- {
- /**
- * @var string
- */
- public $stringVar;
- public function getNewAnon()
- {
- return new class()
- {
- /**
- * @var string
- */
- public $stringVar;
- };
- }
- };
- }
- }
- EOF,
- <<<'EOF'
- <?php
- class Anon
- {
- public function getNewAnon()
- {
- return new class()
- {
- /**
- * @var $stringVar string
- */
- public $stringVar;
- public function getNewAnon()
- {
- return new class()
- {
- /**
- * @var $stringVar string
- */
- public $stringVar;
- };
- }
- };
- }
- }
- EOF,
- ];
- yield [
- '<?php
- /**
- * Header
- */
- class A {} // for the candidate check
- /**
- * @var ClassLoader $loader
- */
- $loader = require __DIR__.\'/../vendor/autoload.php\';
- /**
- * @var \Foo\Bar $bar
- */
- $bar->doSomething(1);
- /**
- * @var $bar \Foo\Bar
- */
- $bar->doSomething(2);
- /**
- * @var User $bar
- */
- ($bar = tmp())->doSomething(3);
- /**
- * @var User $bar
- */
- list($bar) = a();
- ',
- ];
- yield 'const are not handled by this fixer' => [
- '<?php
- class A
- {
- /**
- * @var array<string, true> SKIPPED_TYPES
- */
- private const SKIPPED_TYPES = ["a" => true];
- }
- ',
- ];
- yield 'trait' => [
- '<?php
- trait StaticExample {
- /**
- * @var string Hello!
- */
- public static $static = "foo";
- }',
- '<?php
- trait StaticExample {
- /**
- * @var string $static Hello!
- */
- public static $static = "foo";
- }',
- ];
- yield 'complex type with union containing callable that has `$this` in signature' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var array<string, string|array{ string|\Closure(mixed, string, $this): int|float }>|false Hello!
- */
- public $foo;
- /** @var int Hello! */
- public $foo2;
- /** @var int Hello! */
- public $foo3;
- /** @var int Hello! */
- public $foo4;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /**
- * @var array<string, string|array{ string|\Closure(mixed, string, $this): int|float }>|false $foo Hello!
- */
- public $foo;
- /** @var int $thi Hello! */
- public $foo2;
- /** @var int $thiss Hello! */
- public $foo3;
- /** @var int $this2 Hello! */
- public $foo4;
- }
- EOF,
- ];
- yield 'testFixMultibyteVariableName' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var int Hello! */
- public $foo;
- /** @var ๐ ๐ */
- public $foo2;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var int $my๐ Hello! */
- public $foo;
- /** @var ๐ $my ๐ */
- public $foo2;
- }
- EOF,
- ];
- yield '@var with callable syntax' => [
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var array<callable(string, Buzz): void> */
- protected $bar;
- }
- EOF,
- <<<'EOF'
- <?php
- class Foo
- {
- /** @var array<callable(string $baz, Buzz $buzz): void> */
- protected $bar;
- }
- EOF,
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{0: string, 1?: string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'readonly' => [
- '<?php
- class Foo
- {
- /** @var Foo */
- public $bar1;
- /** @var Foo */
- public readonly int $bar2;
- /** @var Foo */
- readonly public int $bar3;
- /** @var Foo */
- readonly int $bar4;
- }',
- '<?php
- class Foo
- {
- /** @var Foo $bar1 */
- public $bar1;
- /** @var Foo $bar2 */
- public readonly int $bar2;
- /** @var Foo $bar3 */
- readonly public int $bar3;
- /** @var Foo $bar4 */
- readonly int $bar4;
- }',
- ];
- yield 'final public const are not handled by this fixer' => [
- '<?php
- class A
- {
- /**
- * @var array<string, true> SKIPPED_TYPES
- */
- final public const SKIPPED_TYPES = ["a" => true];
- }
- ',
- ];
- }
- }
|