Skip to content

Commit 1dd491c

Browse files
committed
Add return tag factory
1 parent b12a339 commit 1dd491c

File tree

12 files changed

+113
-2
lines changed

12 files changed

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

src/DocBlock/Tags/Factory/TypeFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
3737
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
3838

39+
use function array_filter;
3940
use function array_map;
4041
use function array_reverse;
4142
use function get_class;
@@ -78,6 +79,7 @@ public function createType(TypeNode $type, ?Context $context): ?Type
7879

7980
case ConstTypeNode::class:
8081
return null;
82+
8183
case GenericTypeNode::class:
8284
return $this->createFromGeneric($type, $context);
8385

src/DocBlock/Tags/Method.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
use function sort;
3232
use function strpos;
3333
use function substr;
34+
use function trigger_error;
3435
use function trim;
3536
use function var_export;
3637

38+
use const E_USER_DEPRECATED;
39+
3740
/**
3841
* Reflection class for an {@}method in a Docblock.
3942
*/

src/DocBlock/Tags/Param.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use function implode;
2727
use function strpos;
2828
use function substr;
29+
use function trigger_error;
2930

31+
use const E_USER_DEPRECATED;
3032
use const PREG_SPLIT_DELIM_CAPTURE;
3133

3234
/**

src/DocBlock/Tags/Property.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use function implode;
2727
use function strpos;
2828
use function substr;
29+
use function trigger_error;
2930

31+
use const E_USER_DEPRECATED;
3032
use const PREG_SPLIT_DELIM_CAPTURE;
3133

3234
/**

src/DocBlock/Tags/PropertyRead.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use function implode;
2727
use function strpos;
2828
use function substr;
29+
use function trigger_error;
2930

31+
use const E_USER_DEPRECATED;
3032
use const PREG_SPLIT_DELIM_CAPTURE;
3133

3234
/**

src/DocBlock/Tags/PropertyWrite.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use function implode;
2727
use function strpos;
2828
use function substr;
29+
use function trigger_error;
2930

31+
use const E_USER_DEPRECATED;
3032
use const PREG_SPLIT_DELIM_CAPTURE;
3133

3234
/**

src/DocBlock/Tags/Return_.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
use phpDocumentor\Reflection\Types\Context as TypeContext;
2121
use Webmozart\Assert\Assert;
2222

23+
use function trigger_error;
24+
25+
use const E_USER_DEPRECATED;
26+
2327
/**
2428
* Reflection class for a {@}return tag in a Docblock.
2529
*/

src/DocBlock/Tags/Var_.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use function implode;
2727
use function strpos;
2828
use function substr;
29+
use function trigger_error;
2930

31+
use const E_USER_DEPRECATED;
3032
use const PREG_SPLIT_DELIM_CAPTURE;
3133

3234
/**

src/DocBlockFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
use phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory;
2323
use phpDocumentor\Reflection\DocBlock\Tags\Factory\Factory;
2424
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory;
25+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory;
2526
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory;
27+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory;
2628
use Webmozart\Assert\Assert;
2729

2830
use function array_shift;
@@ -68,13 +70,16 @@ public static function createInstance(array $additionalTags = []): self
6870
$typeFactory = new TypeFactory($typeResolver);
6971

7072
$phpstanTagFactory = new AbstractPHPStanFactory(
71-
new ParamFactory($typeFactory, $descriptionFactory)
73+
new ParamFactory($typeFactory, $descriptionFactory),
74+
new VarFactory($typeFactory, $descriptionFactory),
75+
new ReturnFactory($typeFactory, $descriptionFactory),
7276
);
7377

7478
$tagFactory->addService($descriptionFactory);
7579
$tagFactory->addService($typeResolver);
7680
$tagFactory->registerTagHandler('param', $phpstanTagFactory);
7781
$tagFactory->registerTagHandler('var', $phpstanTagFactory);
82+
$tagFactory->registerTagHandler('return', $phpstanTagFactory);
7883

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

0 commit comments

Comments
 (0)