mirror of
https://github.com/barryvdh/ReflectionDocBlock.git
synced 2026-08-18 10:07:12 +00:00
Add tests for factories
This commit is contained in:
@@ -28,9 +28,8 @@ use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateExtendsFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateCovariantFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateImplementsFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ThrowsFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
|
||||
@@ -156,8 +155,7 @@ final class StandardTagFactory implements TagFactory
|
||||
new ImplementsFactory($typeResolver, $descriptionFactory),
|
||||
new ExtendsFactory($typeResolver, $descriptionFactory),
|
||||
new TemplateFactory($typeResolver, $descriptionFactory),
|
||||
new TemplateImplementsFactory($typeResolver, $descriptionFactory),
|
||||
new TemplateExtendsFactory($typeResolver, $descriptionFactory),
|
||||
new TemplateCovariantFactory($typeResolver, $descriptionFactory),
|
||||
new ThrowsFactory($typeResolver, $descriptionFactory),
|
||||
);
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
|
||||
use phpDocumentor\Reflection\Types\Context as TypeContext;
|
||||
use PHPStan\PhpDocParser\Lexer\Lexer;
|
||||
use PHPStan\PhpDocParser\Parser\ConstExprParser;
|
||||
use PHPStan\PhpDocParser\Parser\ParserException;
|
||||
use PHPStan\PhpDocParser\Parser\PhpDocParser;
|
||||
use PHPStan\PhpDocParser\Parser\TokenIterator;
|
||||
use PHPStan\PhpDocParser\Parser\TypeParser;
|
||||
@@ -59,13 +60,17 @@ class AbstractPHPStanFactory implements Factory
|
||||
|
||||
public function create(string $tagLine, ?TypeContext $context = null): Tag
|
||||
{
|
||||
$tokens = $this->tokenizeLine($tagLine . "\n");
|
||||
$ast = $this->parser->parseTag($tokens);
|
||||
if (property_exists($ast->value, 'description') === true) {
|
||||
$ast->value->setAttribute(
|
||||
'description',
|
||||
rtrim($ast->value->description . $tokens->joinUntil(Lexer::TOKEN_END), "\n")
|
||||
);
|
||||
try {
|
||||
$tokens = $this->tokenizeLine($tagLine . "\n");
|
||||
$ast = $this->parser->parseTag($tokens);
|
||||
if (property_exists($ast->value, 'description') === true) {
|
||||
$ast->value->setAttribute(
|
||||
'description',
|
||||
rtrim($ast->value->description . $tokens->joinUntil(Lexer::TOKEN_END), "\n")
|
||||
);
|
||||
}
|
||||
} catch (ParserException $e) {
|
||||
return InvalidTag::create($tagLine, '')->withError($e);
|
||||
}
|
||||
|
||||
if ($context === null) {
|
||||
@@ -80,6 +85,8 @@ class AbstractPHPStanFactory implements Factory
|
||||
}
|
||||
} catch (RuntimeException $e) {
|
||||
return InvalidTag::create((string) $ast->value, 'method')->withError($e);
|
||||
} catch (ParserException $e) {
|
||||
return InvalidTag::create((string) $ast->value, $ast->name)->withError($e);
|
||||
}
|
||||
|
||||
return InvalidTag::create(
|
||||
|
||||
+8
-7
@@ -6,11 +6,12 @@ namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
|
||||
|
||||
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tag;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\TemplateExtends;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\TemplateCovariant;
|
||||
use phpDocumentor\Reflection\TypeResolver;
|
||||
use phpDocumentor\Reflection\Types\Context;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
use function is_string;
|
||||
@@ -18,7 +19,7 @@ use function is_string;
|
||||
/**
|
||||
* @internal This class is not part of the BC promise of this library.
|
||||
*/
|
||||
final class TemplateExtendsFactory implements PHPStanFactory
|
||||
final class TemplateCovariantFactory implements PHPStanFactory
|
||||
{
|
||||
private DescriptionFactory $descriptionFactory;
|
||||
private TypeResolver $typeResolver;
|
||||
@@ -31,21 +32,21 @@ final class TemplateExtendsFactory implements PHPStanFactory
|
||||
|
||||
public function supports(PhpDocTagNode $node, Context $context): bool
|
||||
{
|
||||
return $node->value instanceof ExtendsTagValueNode && $node->name === '@template-extends';
|
||||
return $node->value instanceof TemplateTagValueNode && $node->name === '@template-covariant';
|
||||
}
|
||||
|
||||
public function create(PhpDocTagNode $node, Context $context): Tag
|
||||
{
|
||||
$tagValue = $node->value;
|
||||
Assert::isInstanceOf($tagValue, ExtendsTagValueNode::class);
|
||||
Assert::isInstanceOf($tagValue, TemplateTagValueNode::class);
|
||||
|
||||
$description = $tagValue->getAttribute('description');
|
||||
if (is_string($description) === false) {
|
||||
$description = $tagValue->description;
|
||||
}
|
||||
|
||||
return new TemplateExtends(
|
||||
$this->typeResolver->createType($tagValue->type, $context),
|
||||
return new TemplateCovariant(
|
||||
$this->typeResolver->createType(new IdentifierTypeNode($tagValue->name), $context),
|
||||
$this->descriptionFactory->create($description, $context)
|
||||
);
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
|
||||
|
||||
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
|
||||
use phpDocumentor\Reflection\DocBlock\Tag;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\TemplateImplements;
|
||||
use phpDocumentor\Reflection\TypeResolver;
|
||||
use phpDocumentor\Reflection\Types\Context;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* @internal This class is not part of the BC promise of this library.
|
||||
*/
|
||||
final class TemplateImplementsFactory implements PHPStanFactory
|
||||
{
|
||||
private DescriptionFactory $descriptionFactory;
|
||||
private TypeResolver $typeResolver;
|
||||
|
||||
public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
|
||||
{
|
||||
$this->descriptionFactory = $descriptionFactory;
|
||||
$this->typeResolver = $typeResolver;
|
||||
}
|
||||
|
||||
public function supports(PhpDocTagNode $node, Context $context): bool
|
||||
{
|
||||
return $node->value instanceof ImplementsTagValueNode && $node->name === '@template-implements';
|
||||
}
|
||||
|
||||
public function create(PhpDocTagNode $node, Context $context): Tag
|
||||
{
|
||||
$tagValue = $node->value;
|
||||
Assert::isInstanceOf($tagValue, ImplementsTagValueNode::class);
|
||||
|
||||
$description = $tagValue->getAttribute('description');
|
||||
if (is_string($description) === false) {
|
||||
$description = $tagValue->description;
|
||||
}
|
||||
|
||||
return new TemplateImplements(
|
||||
$this->typeResolver->createType($tagValue->type, $context),
|
||||
$this->descriptionFactory->create($description, $context)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user