Skip to content
Open
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
38 changes: 38 additions & 0 deletions src/DocBlock/Tags/Reference/Variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\DocBlock\Tags\Reference;

use Webmozart\Assert\Assert;

/**
* Variable reference used by {@see \phpDocumentor\Reflection\DocBlock\Tags\See} to refer to a variable that
* is not addressable through an FQSEN, typically a global variable referenced with {@}see $varname.
*/
final class Variable implements Reference
{
private string $name;

public function __construct(string $name)
{
Assert::stringNotEmpty($name);
Assert::startsWith($name, '$');

$this->name = $name;
}

public function __toString(): string
{
return $this->name;
}
}
6 changes: 6 additions & 0 deletions src/DocBlock/Tags/See.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Fqsen as FqsenRef;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Reference;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Url;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Variable;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
Expand Down Expand Up @@ -62,6 +63,11 @@ public static function create(
return new static(new Url($parts[0]), $description);
}

// Variables are not addressable through an FQSEN but are a valid target for {@}see, e.g. a global `$varname`.
if (preg_match('/^\$[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $parts[0])) {
return new static(new Variable($parts[0]), $description);
}

return new static(new FqsenRef(self::resolveFqsen($parts[0], $typeResolver, $context)), $description);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/DocBlock/Tags/SeeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Fqsen as FqsenRef;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Fqsen as TagsFqsen;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Url as UrlRef;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Variable as VariableRef;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context;
Expand Down Expand Up @@ -253,6 +254,37 @@ public function testFactoryMethodWithUrl(): void
$this->assertSame($description, $fixture->getDescription());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\See::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\FqsenResolver
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Reference\Variable
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::create
*/
public function testFactoryMethodWithVariable(): void
{
$descriptionFactory = m::mock(DescriptionFactory::class);
$resolver = m::mock(FqsenResolver::class);
$context = new Context('');

$description = new Description('My Description');

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

$resolver->shouldNotReceive('resolve');

$fixture = See::create('$varname My Description', $resolver, $descriptionFactory, $context);

$this->assertSame('$varname My Description', (string) $fixture);
$this->assertInstanceOf(VariableRef::class, $fixture->getReference());
$this->assertSame('$varname', (string) $fixture->getReference());
$this->assertSame($description, $fixture->getDescription());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\See::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
Expand Down