Skip to content

Commit

Permalink
Seprate template-* tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ahjdev committed Jul 27, 2024
1 parent f93a6b7 commit f17d984
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 34 deletions.
32 changes: 32 additions & 0 deletions src/DocBlock/Tags/Factory/AbstractExtendsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;

use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;

/**
* @internal This class is not part of the BC promise of this library.
*/
abstract class AbstractExtendsFactory implements PHPStanFactory
{
private DescriptionFactory $descriptionFactory;
private TypeResolver $typeResolver;
protected string $tagName;

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 ExtendsTagValueNode && $node->name === $this->tagName;
}
}
32 changes: 32 additions & 0 deletions src/DocBlock/Tags/Factory/AbstractImplementsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;

use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;

/**
* @internal This class is not part of the BC promise of this library.
*/
abstract class AbstractImplementsFactory implements PHPStanFactory
{
private DescriptionFactory $descriptionFactory;
private TypeResolver $typeResolver;
protected string $tagName;

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 === $this->tagName;
}
}
21 changes: 5 additions & 16 deletions src/DocBlock/Tags/Factory/ExtendsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@
use phpDocumentor\Reflection\DocBlock\Tags\Extends_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateExtends;

/**
* @internal This class is not part of the BC promise of this library.
*/
class ExtendsFactory implements PHPStanFactory
final class ExtendsFactory extends AbstractExtendsFactory
{
private DescriptionFactory $descriptionFactory;
private TypeResolver $typeResolver;

public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
{
$this->descriptionFactory = $descriptionFactory;
$this->typeResolver = $typeResolver;
parent::__construct($typeResolver, $descriptionFactory);
$this->tagName = '@extends';
}

public function create(PhpDocTagNode $node, Context $context): Tag
Expand All @@ -38,16 +34,9 @@ public function create(PhpDocTagNode $node, Context $context): Tag
$description = $tagValue->description;
}

$class = $node->name === '@extends' ? Extends_::class : TemplateExtends::class;

return new $class(
return new Extends_(
$this->typeResolver->createType($tagValue->type, $context),
$this->descriptionFactory->create($tagValue->description, $context)
$this->descriptionFactory->create($description, $context)
);
}

public function supports(PhpDocTagNode $node, Context $context): bool
{
return $node->value instanceof ExtendsTagValueNode && ($node->name === '@extends' || $node->name === '@template-extends');
}
}
21 changes: 5 additions & 16 deletions src/DocBlock/Tags/Factory/ImplementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@
use phpDocumentor\Reflection\DocBlock\Tags\Implements_;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateImplements;

/**
* @internal This class is not part of the BC promise of this library.
*/
class ImplementsFactory implements PHPStanFactory
final class ImplementsFactory extends AbstractImplementsFactory
{
private DescriptionFactory $descriptionFactory;
private TypeResolver $typeResolver;

public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
{
$this->descriptionFactory = $descriptionFactory;
$this->typeResolver = $typeResolver;
parent::__construct($typeResolver, $descriptionFactory);
$this->tagName = '@implements';
}

public function create(PhpDocTagNode $node, Context $context): Tag
Expand All @@ -38,16 +34,9 @@ public function create(PhpDocTagNode $node, Context $context): Tag
$description = $tagValue->description;
}

$class = $node->name === '@implements' ? Implements_::class : TemplateImplements::class;

return new $class(
return new Implements_(
$this->typeResolver->createType($tagValue->type, $context),
$this->descriptionFactory->create($tagValue->description, $context)
$this->descriptionFactory->create($description, $context)
);
}

public function supports(PhpDocTagNode $node, Context $context): bool
{
return $node->value instanceof ImplementsTagValueNode && ($node->name === '@implements' || $node->name === '@template-implements');
}
}
42 changes: 42 additions & 0 deletions src/DocBlock/Tags/Factory/TemplateExtendsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;

use Webmozart\Assert\Assert;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateExtends;

/**
* @internal This class is not part of the BC promise of this library.
*/
final class TemplateExtendsFactory extends AbstractExtendsFactory
{
public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
{
parent::__construct($typeResolver, $descriptionFactory);
$this->tagName = '@template-extends';
}

public function create(PhpDocTagNode $node, Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, ExtendsTagValueNode::class);

$description = $tagValue->getAttribute('description');
if (is_string($description) === false) {
$description = $tagValue->description;
}

return new TemplateExtends(
$this->typeResolver->createType($tagValue->type, $context),
$this->descriptionFactory->create($description, $context)
);
}
}
2 changes: 1 addition & 1 deletion src/DocBlock/Tags/Factory/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function create(PhpDocTagNode $node, Context $context): Tag
$tagValue->name,
$this->typeResolver->createType($tagValue->bound, $context),
$this->typeResolver->createType($tagValue->default, $context),
$this->descriptionFactory->create($tagValue->description, $context)
$this->descriptionFactory->create($description, $context)
);
}

Expand Down
42 changes: 42 additions & 0 deletions src/DocBlock/Tags/Factory/TemplateImplementsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;

use Webmozart\Assert\Assert;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateImplements;

/**
* @internal This class is not part of the BC promise of this library.
*/
final class TemplateImplementsFactory extends AbstractImplementsFactory
{
public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
{
parent::__construct($typeResolver, $descriptionFactory);
$this->tagName = '@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)
);
}
}
6 changes: 5 additions & 1 deletion src/DocBlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateExtendsFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateImplementsFactory;

use function trim;
use function count;
Expand Down Expand Up @@ -81,8 +83,10 @@ public static function createInstance(array $additionalTags = []): DocBlockFacto
new PropertyWriteFactory($typeResolver, $descriptionFactory),
new MethodFactory($typeResolver, $descriptionFactory),
new ImplementsFactory($typeResolver, $descriptionFactory),
new TemplateFactory($typeResolver, $descriptionFactory),
new ExtendsFactory($typeResolver, $descriptionFactory),
new TemplateFactory($typeResolver, $descriptionFactory),
new TemplateImplementsFactory($typeResolver, $descriptionFactory),
new TemplateExtendsFactory($typeResolver, $descriptionFactory),
);

$tagFactory->addService($descriptionFactory);
Expand Down

0 comments on commit f17d984

Please sign in to comment.