123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680 |
- <?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\DocBlock;
- use PhpCsFixer\DocBlock\Annotation;
- use PhpCsFixer\DocBlock\DocBlock;
- use PhpCsFixer\DocBlock\Line;
- use PhpCsFixer\Tests\TestCase;
- use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
- use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
- /**
- * @author Graham Campbell <hello@gjcampbell.co.uk>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\DocBlock\Annotation
- */
- final class AnnotationTest extends TestCase
- {
- /**
- * This represents the content an entire docblock.
- *
- * @var string
- */
- private static $sample = '/**
- * Test docblock.
- *
- * @param string $hello
- * @param bool $test Description
- * extends over many lines
- *
- * @param adkjbadjasbdand $asdnjkasd
- *
- * @throws \Exception asdnjkasd
- *
- * asdasdasdasdasdasdasdasd
- * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb
- *
- * @return void
- */';
- /**
- * This represents the content of each annotation.
- *
- * @var string[]
- */
- private static $content = [
- " * @param string \$hello\n",
- " * @param bool \$test Description\n * extends over many lines\n",
- " * @param adkjbadjasbdand \$asdnjkasd\n",
- " * @throws \\Exception asdnjkasd\n *\n * asdasdasdasdasdasdasdasd\n * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb\n",
- " * @return void\n",
- ];
- /**
- * This represents the start indexes of each annotation.
- *
- * @var int[]
- */
- private static $start = [3, 4, 7, 9, 14];
- /**
- * This represents the start indexes of each annotation.
- *
- * @var int[]
- */
- private static $end = [3, 5, 7, 12, 14];
- /**
- * This represents the tag type of each annotation.
- *
- * @var string[]
- */
- private static $tags = ['param', 'param', 'param', 'throws', 'return'];
- /**
- * @dataProvider provideGetContentCases
- */
- public function testGetContent(int $index, string $content): void
- {
- $doc = new DocBlock(self::$sample);
- $annotation = $doc->getAnnotation($index);
- self::assertSame($content, $annotation->getContent());
- self::assertSame($content, (string) $annotation);
- }
- public static function provideGetContentCases(): iterable
- {
- foreach (self::$content as $index => $content) {
- yield [$index, $content];
- }
- }
- /**
- * @dataProvider provideStartCases
- */
- public function testStart(int $index, int $start): void
- {
- $doc = new DocBlock(self::$sample);
- $annotation = $doc->getAnnotation($index);
- self::assertSame($start, $annotation->getStart());
- }
- public static function provideStartCases(): iterable
- {
- foreach (self::$start as $index => $start) {
- yield [$index, $start];
- }
- }
- /**
- * @dataProvider provideEndCases
- */
- public function testEnd(int $index, int $end): void
- {
- $doc = new DocBlock(self::$sample);
- $annotation = $doc->getAnnotation($index);
- self::assertSame($end, $annotation->getEnd());
- }
- public static function provideEndCases(): iterable
- {
- foreach (self::$end as $index => $end) {
- yield [$index, $end];
- }
- }
- /**
- * @dataProvider provideGetTagCases
- */
- public function testGetTag(int $index, string $tag): void
- {
- $doc = new DocBlock(self::$sample);
- $annotation = $doc->getAnnotation($index);
- self::assertSame($tag, $annotation->getTag()->getName());
- }
- public static function provideGetTagCases(): iterable
- {
- foreach (self::$tags as $index => $tag) {
- yield [$index, $tag];
- }
- }
- /**
- * @dataProvider provideRemoveCases
- */
- public function testRemove(int $index, int $start, int $end): void
- {
- $doc = new DocBlock(self::$sample);
- $annotation = $doc->getAnnotation($index);
- $annotation->remove();
- self::assertSame('', $annotation->getContent());
- self::assertSame('', $doc->getLine($start)->getContent());
- self::assertSame('', $doc->getLine($end)->getContent());
- }
- public static function provideRemoveCases(): iterable
- {
- foreach (self::$start as $index => $start) {
- yield [$index, $start, self::$end[$index]];
- }
- }
- /**
- * @dataProvider provideRemoveEdgeCasesCases
- */
- public function testRemoveEdgeCases(string $expected, string $input): void
- {
- $doc = new DocBlock($input);
- $annotation = $doc->getAnnotation(0);
- $annotation->remove();
- self::assertSame($expected, $doc->getContent());
- }
- public static function provideRemoveEdgeCasesCases(): iterable
- {
- // Single line
- yield ['', '/** @return null*/'];
- yield ['', '/** @return null */'];
- yield ['', '/** @return null */'];
- // Multi line, annotation on start line
- yield [
- '/**
- */',
- '/** @return null
- */',
- ];
- yield [
- '/**
- */',
- '/** @return null '.'
- */',
- ];
- // Multi line, annotation on end line
- yield [
- '/**
- */',
- '/**
- * @return null*/',
- ];
- yield [
- '/**
- */',
- '/**
- * @return null */',
- ];
- }
- /**
- * @param string[] $expected
- *
- * @dataProvider provideTypeParsingCases
- */
- public function testTypeParsing(array $expected, string $input): void
- {
- $tag = new Annotation([new Line($input)]);
- self::assertSame($expected, $tag->getTypes());
- }
- public static function provideTypeParsingCases(): iterable
- {
- yield [
- ['int'],
- ' * @method int method()',
- ];
- yield [
- ['int[]'],
- " * @return int[]\r",
- ];
- yield [
- ['int[]'],
- " * @return int[]\r\n",
- ];
- yield [
- ['Foo[][]'],
- ' * @method Foo[][] method()',
- ];
- yield [
- ['int[]'],
- ' * @method int[] method()',
- ];
- yield [
- ['int[]', 'null'],
- ' * @method int[]|null method()',
- ];
- yield [
- ['int[]', 'null', '?int', 'array'],
- ' * @method int[]|null|?int|array method()',
- ];
- yield [
- ['null', 'Foo\Bar', '\Baz\Bax', 'int[]'],
- ' * @method null|Foo\Bar|\Baz\Bax|int[] method()',
- ];
- yield [
- ['gen<int>'],
- ' * @method gen<int> method()',
- ];
- yield [
- ['int', 'gen<int>'],
- ' * @method int|gen<int> method()',
- ];
- yield [
- ['\int', '\gen<\int, \bool>'],
- ' * @method \int|\gen<\int, \bool> method()',
- ];
- yield [
- ['gen<int, int>'],
- ' * @method gen<int, int> method()',
- ];
- yield [
- ['gen<int, bool|string>'],
- ' * @method gen<int, bool|string> method()',
- ];
- yield [
- ['gen<int, string[]>'],
- ' * @method gen<int, string[]> method() <> a',
- ];
- yield [
- ['gen<int, gener<string, bool>>'],
- ' * @method gen<int, gener<string, bool>> method() foo <a >',
- ];
- yield [
- ['gen<int, gener<string, null|bool>>'],
- ' * @method gen<int, gener<string, null|bool>> method()',
- ];
- yield [
- ['null', 'gen<int, gener<string, bool>>', 'int', 'string[]'],
- ' * @method null|gen<int, gener<string, bool>>|int|string[] method() foo <a >',
- ];
- yield [
- ['null', 'gen<int, gener<string, bool>>', 'int', 'array<int, string>', 'string[]'],
- ' * @method null|gen<int, gener<string, bool>>|int|array<int, string>|string[] method() foo <a >',
- ];
- yield [
- ['this'],
- '/** @return this */',
- ];
- yield [
- ['@this'],
- '/** @return @this */',
- ];
- yield [
- ['$SELF', 'int'],
- '/** @return $SELF|int */',
- ];
- yield [
- ['array<string|int, string>'],
- '/** @var array<string|int, string>',
- ];
- yield [
- ['int'],
- " * @return int\n",
- ];
- yield [
- ['int'],
- " * @return int\r\n",
- ];
- yield [
- ['Collection<Foo<Bar>, Foo<Baz>>'],
- '/** @var Collection<Foo<Bar>, Foo<Baz>>',
- ];
- yield [
- ['int', 'string'],
- '/** @var int | string',
- ];
- yield [
- ['Foo::*'],
- '/** @var Foo::*',
- ];
- yield [
- ['Foo::A'],
- '/** @var Foo::A',
- ];
- yield [
- ['Foo::A', 'Foo::B'],
- '/** @var Foo::A|Foo::B',
- ];
- yield [
- ['Foo::A*'],
- '/** @var Foo::A*',
- ];
- yield [
- ['array<Foo::A*>', 'null'],
- '/** @var array<Foo::A*>|null',
- ];
- yield [
- ['null', 'true', 'false', '1', '-1', '1.5', '-1.5', '.5', '1.', "'a'", '"b"'],
- '/** @var null|true|false|1|-1|1.5|-1.5|.5|1.|\'a\'|"b"',
- ];
- yield [
- ['int', '"a"', 'A<B<C, D>, E<F::*|G[]>>'],
- '/** @param int | "a" | A<B<C, D>, E<F::*|G[]>> $foo */',
- ];
- yield [
- ['class-string<Foo>'],
- '/** @var class-string<Foo> */',
- ];
- yield [
- ['A', 'B'],
- '/** @var A&B */',
- ];
- yield [
- ['A', 'B'],
- '/** @var A & B */',
- ];
- yield [
- ['array{1: bool, 2: bool}'],
- '/** @var array{1: bool, 2: bool} */',
- ];
- yield [
- ['array{a: int|string, b?: bool}'],
- '/** @var array{a: int|string, b?: bool} */',
- ];
- yield [
- ['array{\'a\': "a", "b"?: \'b\'}'],
- '/** @var array{\'a\': "a", "b"?: \'b\'} */',
- ];
- yield [
- ['array { a : int | string , b ? : A<B, C> }'],
- '/** @var array { a : int | string , b ? : A<B, C> } */',
- ];
- yield [
- ['callable(string)'],
- '/** @param callable(string) $function',
- ];
- yield [
- ['callable(string): bool'],
- '/** @param callable(string): bool $function',
- ];
- yield [
- ['callable(array<int, string>, array<int, Foo>): bool'],
- '/** @param callable(array<int, string>, array<int, Foo>): bool $function',
- ];
- yield [
- ['array<int, callable(string): bool>'],
- '/** @param array<int, callable(string): bool> $function',
- ];
- yield [
- ['callable(string): callable(int)'],
- '/** @param callable(string): callable(int) $function',
- ];
- yield [
- ['callable(string) : callable(int) : bool'],
- '/** @param callable(string) : callable(int) : bool $function',
- ];
- yield [
- ['TheCollection<callable(Foo, Bar,Baz): Foo[]>', 'string[]', 'null'],
- '* @param TheCollection<callable(Foo, Bar,Baz): Foo[]>|string[]|null $x',
- ];
- yield [
- ['Closure(string)'],
- '/** @param Closure(string) $function',
- ];
- yield [
- ['closure()'],
- '/** @param closure() $function',
- ];
- yield [
- ['array < int , callable ( string ) : bool >'],
- '/** @param array < int , callable ( string ) : bool > $function',
- ];
- }
- /**
- * @param string[] $expected
- * @param string[] $new
- *
- * @dataProvider provideTypesCases
- */
- public function testTypes(array $expected, array $new, string $input, string $output): void
- {
- $line = new Line($input);
- $tag = new Annotation([$line]);
- self::assertSame($expected, $tag->getTypes());
- $tag->setTypes($new);
- self::assertSame($new, $tag->getTypes());
- self::assertSame($output, $line->getContent());
- }
- public static function provideTypesCases(): iterable
- {
- yield [['Foo', 'null'], ['Bar[]'], ' * @param Foo|null $foo', ' * @param Bar[] $foo'];
- yield [['false'], ['bool'], '* @return false', '* @return bool'];
- yield [['RUNTIMEEEEeXCEPTION'], [\Throwable::class], "* \t@throws\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n", "* \t@throws\t \t Throwable\t\t\t\t\t\t\t\n\n\n"];
- yield [['RUNTIMEEEEeXCEPTION'], [\Throwable::class], "*\t@throws\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n", "*\t@throws\t \t Throwable\t\t\t\t\t\t\t\n\n\n"];
- yield [['RUNTIMEEEEeXCEPTION'], [\Throwable::class], "*@throws\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n", "*@throws\t \t Throwable\t\t\t\t\t\t\t\n\n\n"];
- yield [['string'], ['string', 'null'], ' * @method string getString()', ' * @method string|null getString()'];
- yield [['Foo', 'Bar'], ['Bar', 'Foo'], ' * @param Foo&Bar $x', ' * @param Bar&Foo $x'];
- }
- /**
- * @param string[] $expected
- *
- * @dataProvider provideNormalizedTypesCases
- */
- public function testNormalizedTypes(array $expected, string $input): void
- {
- $line = new Line($input);
- $tag = new Annotation([$line]);
- self::assertSame($expected, $tag->getNormalizedTypes());
- }
- public static function provideNormalizedTypesCases(): iterable
- {
- yield [['null', 'string'], '* @param StRiNg|NuLl $foo'];
- yield [['void'], '* @return Void'];
- yield [['bar', 'baz', 'foo', 'null', 'qux'], '* @return Foo|Bar|Baz|Qux|null'];
- yield [['bool', 'int'], '* @param bool|int $foo'];
- yield [['bool', 'int'], '* @param bool|int ...$foo'];
- yield [['bool', 'int'], '* @param bool|int &$foo'];
- yield [['bool', 'int'], '* @param bool|int &...$foo'];
- yield [['bool', 'int'], '* @param bool|int$foo'];
- yield [['bool', 'int'], '* @param bool|int&$foo'];
- yield [['bool', 'int'], '* @param bool|int&...$foo'];
- yield [['bar', 'baz', 'foo'], '* @param Foo|Bar&Baz&$param'];
- }
- public function testGetTypesOnBadTag(): void
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('This tag does not support types');
- $tag = new Annotation([new Line(' * @deprecated since Symfony 1.2')]);
- $tag->getTypes();
- }
- public function testSetTypesOnBadTag(): void
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('This tag does not support types');
- $tag = new Annotation([new Line(' * @author Chuck Norris')]);
- $tag->setTypes(['string']);
- }
- public function testGetTagsWithTypes(): void
- {
- $tags = Annotation::getTagsWithTypes();
- foreach ($tags as $tag) {
- self::assertIsString($tag);
- self::assertNotEmpty($tag);
- }
- }
- /**
- * @param NamespaceUseAnalysis[] $namespaceUses
- *
- * @dataProvider provideGetTypeExpressionCases
- */
- public function testGetTypeExpression(string $line, ?NamespaceAnalysis $namespace, array $namespaceUses, ?string $expectedCommonType): void
- {
- $annotation = new Annotation([new Line($line)], $namespace, $namespaceUses);
- $result = $annotation->getTypeExpression();
- self::assertSame($expectedCommonType, $result->getCommonType());
- }
- public static function provideGetTypeExpressionCases(): iterable
- {
- $appNamespace = new NamespaceAnalysis('App', 'App', 0, 999, 0, 999);
- $useTraversable = new NamespaceUseAnalysis('Traversable', 'Traversable', false, 0, 999, NamespaceUseAnalysis::TYPE_CLASS);
- yield ['* @param array|Traversable $foo', null, [], 'iterable'];
- yield ['* @param array|Traversable $foo', $appNamespace, [], null];
- yield ['* @param array|Traversable $foo', $appNamespace, [$useTraversable], 'iterable'];
- }
- /**
- * @dataProvider provideGetVariableNameCases
- */
- public function testGetVariableName(string $line, ?string $expectedVariableName): void
- {
- $annotation = new Annotation([new Line($line)]);
- self::assertSame($expectedVariableName, $annotation->getVariableName());
- }
- public static function provideGetVariableNameCases(): iterable
- {
- yield ['* @param int $foo', '$foo'];
- yield ['* @param int $foo some description', '$foo'];
- yield ['/** @param int $foo*/', '$foo'];
- yield ['* @param int', null];
- yield ['* @var int $foo', '$foo'];
- yield ['* @var int $foo some description', '$foo'];
- yield ['/** @var int $foo*/', '$foo'];
- yield ['* @var int', null];
- yield ['* @param $foo', '$foo'];
- yield ['* @param &$foo', '$foo'];
- yield ['* @param & $foo', '$foo'];
- yield ['* @param int &$foo', '$foo'];
- yield ['* @param int & $foo', '$foo'];
- yield ['* @param int ...$foo', '$foo'];
- yield ['* @param int ... $foo', '$foo'];
- yield ['* @param int&...$foo', '$foo'];
- yield ['* @param int &...$foo', '$foo'];
- yield ['* @param int & ...$foo', '$foo'];
- yield ['* @param int & ... $foo', '$foo'];
- }
- }
|