Skip to content

Commit bbb6a02

Browse files
committed
Add implements
1 parent aa53f8d commit bbb6a02

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
6+
7+
use Webmozart\Assert\Assert;
8+
use phpDocumentor\Reflection\TypeResolver;
9+
use phpDocumentor\Reflection\Types\Context;
10+
use phpDocumentor\Reflection\DocBlock\Tag;
11+
use phpDocumentor\Reflection\DocBlock\Tags\Implements_;
12+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
13+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
14+
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
15+
16+
/**
17+
* @internal This class is not part of the BC promise of this library.
18+
*/
19+
class ImplementsFactory implements PHPStanFactory
20+
{
21+
private DescriptionFactory $descriptionFactory;
22+
private TypeResolver $typeResolver;
23+
24+
public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
25+
{
26+
$this->descriptionFactory = $descriptionFactory;
27+
$this->typeResolver = $typeResolver;
28+
}
29+
30+
public function create(PhpDocTagNode $node, Context $context): Tag
31+
{
32+
$tagValue = $node->value;
33+
Assert::isInstanceOf($tagValue, ImplementsTagValueNode::class);
34+
35+
return new Implements_(
36+
$this->typeResolver->createType($tagValue->type, $context),
37+
$this->descriptionFactory->create($tagValue->description, $context)
38+
);
39+
}
40+
41+
public function supports(PhpDocTagNode $node, Context $context): bool
42+
{
43+
return $node->value instanceof ImplementsTagValueNode && $node->name === '@implements';
44+
}
45+
}

src/DocBlock/Tags/Implements_.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\DocBlock\Tags;
15+
16+
use phpDocumentor\Reflection\Type;
17+
use phpDocumentor\Reflection\DocBlock\Description;
18+
use phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
19+
20+
/**
21+
* Reflection class for a {@}implements tag in a Docblock.
22+
*/
23+
final class Implements_ extends TagWithType
24+
{
25+
public function __construct(Type $type, ?Description $description = null)
26+
{
27+
$this->name = 'implements';
28+
$this->type = $type;
29+
$this->description = $description;
30+
}
31+
32+
public static function create(string $body)
33+
{
34+
trigger_error(
35+
'Create using static factory is deprecated, this method should not be called directly
36+
by library consumers',
37+
E_USER_DEPRECATED
38+
);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
if ($this->description) {
44+
$description = $this->description->render();
45+
} else {
46+
$description = '';
47+
}
48+
49+
$type = $this->type;
50+
51+
return $type . ($description !== '' ? ' ' . $description : '');
52+
}
53+
}

src/DocBlockFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory;
2929
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory;
3030
use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory;
31+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ImplementsFactory;
3132
use Webmozart\Assert\Assert;
3233

3334
use function array_shift;
@@ -76,7 +77,8 @@ public static function createInstance(array $additionalTags = []): DocBlockFacto
7677
new PropertyFactory($typeResolver, $descriptionFactory),
7778
new PropertyReadFactory($typeResolver, $descriptionFactory),
7879
new PropertyWriteFactory($typeResolver, $descriptionFactory),
79-
new MethodFactory($typeResolver, $descriptionFactory)
80+
new MethodFactory($typeResolver, $descriptionFactory),
81+
new ImplementsFactory($typeResolver, $descriptionFactory)
8082
);
8183

8284
$tagFactory->addService($descriptionFactory);
@@ -88,6 +90,7 @@ public static function createInstance(array $additionalTags = []): DocBlockFacto
8890
$tagFactory->registerTagHandler('property-read', $phpstanTagFactory);
8991
$tagFactory->registerTagHandler('property-write', $phpstanTagFactory);
9092
$tagFactory->registerTagHandler('method', $phpstanTagFactory);
93+
$tagFactory->registerTagHandler('implements', $phpstanTagFactory);
9194

9295
$docBlockFactory = new self($descriptionFactory, $tagFactory);
9396
foreach ($additionalTags as $tagName => $tagHandler) {

0 commit comments

Comments
 (0)