Skip to content

Commit

Permalink
minor #6236 Annotation - improve getting variable name (kubawerlos)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch.

Discussion
----------

Annotation - improve getting variable name

Commits
-------

199fb6f Annotation - improve getting variable name
  • Loading branch information
keradus committed Jan 14, 2022
2 parents 6fe4c69 + 199fb6f commit 75d26d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/DocBlock/Annotation.php
Expand Up @@ -176,7 +176,7 @@ public function getTypeExpression(): TypeExpression
public function getVariableName()
{
$type = preg_quote($this->getTypesContent(), '/');
$regex = "/@{$this->tag->getName()}\\s+{$type}\\s+(?<variable>\\$.+?)(?:[\\s*]|$)/";
$regex = "/@{$this->tag->getName()}\\s+({$type}\\s*)?(&\\s*)?(\\.{3}\\s*)?(?<variable>\\$.+?)(?:[\\s*]|$)/";

if (Preg::match($regex, $this->lines[0]->getContent(), $matches)) {
return $matches['variable'];
Expand Down
45 changes: 26 additions & 19 deletions tests/DocBlock/AnnotationTest.php
Expand Up @@ -503,7 +503,7 @@ public function testGetTypesOnBadTag(): void
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('This tag does not support types');

$tag = new Annotation([new Line(' * @deprecated since 1.2')]);
$tag = new Annotation([new Line(' * @deprecated since Symfony 1.2')]);

$tag->getTypes();
}
Expand All @@ -529,14 +529,13 @@ public function testGetTagsWithTypes(): void
}

/**
* @param Line[] $lines
* @param NamespaceUseAnalysis[] $namespaceUses
*
* @dataProvider provideTypeExpressionCases
*/
public function testGetTypeExpression(array $lines, ?NamespaceAnalysis $namespace, array $namespaceUses, ?string $expectedCommonType): void
public function testGetTypeExpression(string $line, ?NamespaceAnalysis $namespace, array $namespaceUses, ?string $expectedCommonType): void
{
$annotation = new Annotation($lines, $namespace, $namespaceUses);
$annotation = new Annotation([new Line($line)], $namespace, $namespaceUses);
$result = $annotation->getTypeExpression();

static::assertSame($expectedCommonType, $result->getCommonType());
Expand All @@ -547,31 +546,39 @@ public function provideTypeExpressionCases(): \Generator
$appNamespace = new NamespaceAnalysis('App', 'App', 0, 999, 0, 999);
$useTraversable = new NamespaceUseAnalysis('Traversable', 'Traversable', false, 0, 999, NamespaceUseAnalysis::TYPE_CLASS);

yield [[new Line('* @param array|Traversable $foo')], null, [], 'iterable'];
yield [[new Line('* @param array|Traversable $foo')], $appNamespace, [], null];
yield [[new Line('* @param array|Traversable $foo')], $appNamespace, [$useTraversable], 'iterable'];
yield ['* @param array|Traversable $foo', null, [], 'iterable'];
yield ['* @param array|Traversable $foo', $appNamespace, [], null];
yield ['* @param array|Traversable $foo', $appNamespace, [$useTraversable], 'iterable'];
}

/**
* @param Line[] $lines
*
* @dataProvider provideGetVariableCases
*/
public function testGetVariableName(array $lines, ?string $expectedVariableName): void
public function testGetVariableName(string $line, ?string $expectedVariableName): void
{
$annotation = new Annotation($lines);
$annotation = new Annotation([new Line($line)]);
static::assertSame($expectedVariableName, $annotation->getVariableName());
}

public function provideGetVariableCases(): \Generator
{
yield [[new Line('* @param int $foo')], '$foo'];
yield [[new Line('* @param int $foo some description')], '$foo'];
yield [[new Line('/** @param int $foo*/')], '$foo'];
yield [[new Line('* @param int')], null];
yield [[new Line('* @var int $foo')], '$foo'];
yield [[new Line('* @var int $foo some description')], '$foo'];
yield [[new Line('/** @var int $foo*/')], '$foo'];
yield [[new Line('* @var int')], null];
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'];
}
}

0 comments on commit 75d26d5

Please sign in to comment.