Skip to content

Param: fix phpdoc with reference hint #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions src/DocBlock/Tags/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ final class Param extends TagWithType implements Factory\StaticMethod
/** @var bool determines whether this is a variadic argument */
private $isVariadic;

/** @var bool determines whether this is passed by reference */
private $isReference;

public function __construct(
?string $variableName,
?Type $type = null,
bool $isVariadic = false,
?Description $description = null
?Description $description = null,
bool $isReference = false
) {
$this->name = 'param';
$this->variableName = $variableName;
$this->type = $type;
$this->isVariadic = $isVariadic;
$this->description = $description;
$this->isReference = $isReference;
}

public static function create(
Expand All @@ -67,6 +72,7 @@ public static function create(
$parts = Utils::pregSplit('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
$variableName = '';
$isVariadic = false;
$isReference = false;

// if the first item that is encountered is not a variable; it is a type
if ($firstPart && $firstPart[0] !== '$') {
Expand All @@ -76,26 +82,42 @@ public static function create(
array_unshift($parts, $firstPart);
}

// if the next item starts with a $ or ...$ it must be the variable name
if (isset($parts[0]) && (strpos($parts[0], '$') === 0 || strpos($parts[0], '...$') === 0)) {
// if the next item starts with a $ or ...$ or &$ or &...$ it must be the variable name
if (isset($parts[0])
&&
(
strpos($parts[0], '$') === 0
||
strpos($parts[0], '...$') === 0
||
strpos($parts[0], '&$') === 0
||
strpos($parts[0], '&...$') === 0
)
) {
$variableName = array_shift($parts);
array_shift($parts);

Assert::notNull($variableName);

if (strpos($variableName, '...') === 0) {
$isVariadic = true;
$variableName = substr($variableName, 3);
}

if (strpos($variableName, '$') === 0) {
$variableName = substr($variableName, 1);
} elseif (strpos($variableName, '&$') === 0) {
$isReference = true;
$variableName = substr($variableName, 2);
} elseif (strpos($variableName, '...$') === 0) {
$isVariadic = true;
$variableName = substr($variableName, 4);
} elseif (strpos($variableName, '&...$') === 0) {
$isVariadic = true;
$isReference = true;
$variableName = substr($variableName, 5);
}
}

$description = $descriptionFactory->create(implode('', $parts), $context);

return new static($variableName, $type, $isVariadic, $description);
return new static($variableName, $type, $isVariadic, $description, $isReference);
}

/**
Expand All @@ -114,12 +136,21 @@ public function isVariadic() : bool
return $this->isVariadic;
}

/**
* Returns whether this tag is passed by reference.
*/
public function isReference() : bool
{
return $this->isReference;
}

/**
* Returns a string representation for this tag.
*/
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. ($this->isReference() ? '&' : '')
. ($this->isVariadic() ? '...' : '')
. ($this->variableName !== null ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
Expand Down
87 changes: 87 additions & 0 deletions tests/unit/DocBlock/Tags/ParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,99 @@ public function testFactoryMethod() : void
$description = new Description('My Description');
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);

$fixture = Param::create('string $myParameter My Description', $typeResolver, $descriptionFactory, $context);

$this->assertSame('string $myParameter My Description', (string) $fixture);
$this->assertSame('myParameter', $fixture->getVariableName());
$this->assertInstanceOf(String_::class, $fixture->getType());
$this->assertFalse($fixture->isVariadic());
$this->assertFalse($fixture->isReference());
$this->assertSame($description, $fixture->getDescription());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::create
*/
public function testFactoryMethodWithVariadic() : void
{
$typeResolver = new TypeResolver();
$descriptionFactory = m::mock(DescriptionFactory::class);
$context = new Context('');

$description = new Description('My Description');
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);

$fixture = Param::create('string ...$myParameter My Description', $typeResolver, $descriptionFactory, $context);

$this->assertSame('string ...$myParameter My Description', (string) $fixture);
$this->assertSame('myParameter', $fixture->getVariableName());
$this->assertInstanceOf(String_::class, $fixture->getType());
$this->assertTrue($fixture->isVariadic());
$this->assertFalse($fixture->isReference());
$this->assertSame($description, $fixture->getDescription());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::create
*/
public function testFactoryMethodWithReference() : void
{
$typeResolver = new TypeResolver();
$descriptionFactory = m::mock(DescriptionFactory::class);
$context = new Context('');

$description = new Description('My Description');
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);

$fixture = Param::create('string &$myParameter My Description', $typeResolver, $descriptionFactory, $context);

$this->assertSame('string &$myParameter My Description', (string) $fixture);
$this->assertSame('myParameter', $fixture->getVariableName());
$this->assertInstanceOf(String_::class, $fixture->getType());
$this->assertFalse($fixture->isVariadic());
$this->assertTrue($fixture->isReference());
$this->assertSame($description, $fixture->getDescription());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::create
*/
public function testFactoryMethodWithVariadicReference() : void
{
$typeResolver = new TypeResolver();
$descriptionFactory = m::mock(DescriptionFactory::class);
$context = new Context('');

$description = new Description('My Description');
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);

$fixture = Param::create(
'string &...$myParameter My Description',
$typeResolver,
$descriptionFactory,
$context
);

$this->assertSame('string &...$myParameter My Description', (string) $fixture);
$this->assertSame('myParameter', $fixture->getVariableName());
$this->assertInstanceOf(String_::class, $fixture->getType());
$this->assertTrue($fixture->isVariadic());
$this->assertTrue($fixture->isReference());
$this->assertSame($description, $fixture->getDescription());
}

Expand Down