Skip to content

Commit 29d6579

Browse files
author
Kirill Nesmeyanov
committed
Make description factory optional for tag factories
1 parent a429d00 commit 29d6579

13 files changed

Lines changed: 109 additions & 61 deletions

src/DocBlock.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
final class DocBlock implements TagProviderInterface
1313
{
14-
use TagProvider;
14+
use TagProvider {
15+
getIterator as private getTagProviderIterator;
16+
}
1517

1618
/**
1719
* @param iterable<array-key, TagInterface> $tags
@@ -27,4 +29,10 @@ public function getDescription(): Description
2729
{
2830
return $this->description;
2931
}
32+
33+
public function getIterator(): \Traversable
34+
{
35+
yield from $this->description;
36+
yield from $this->getTagProviderIterator();
37+
}
3038
}

src/DocBlock/Description.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(
2020
$this->initializeTags($tags);
2121
}
2222

23-
public static function fromNonTagged(string $body): self
23+
public static function create(string $body): self
2424
{
2525
return new self($body);
2626
}

src/DocBlock/StandardTagFactory.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use TypeLang\PhpDocParser\DocBlock\Tag\GenericTag;
99
use TypeLang\PhpDocParser\DocBlock\Tag\InvalidTag;
1010
use TypeLang\PhpDocParser\DocBlock\Tag\InvalidTypedTag;
11+
use TypeLang\PhpDocParser\DocBlock\Tag\TagFactory;
1112
use TypeLang\PhpDocParser\DocBlock\Tag\TagInterface;
1213
use TypeLang\PhpDocParser\Exception\InvalidTagVariableNameException;
1314

1415
/**
15-
* @template-implements TagFactoryInterface<TagInterface>
16+
* @template-extends TagFactory<TagInterface>
1617
*/
17-
final class StandardTagFactory implements TagFactoryInterface
18+
final class StandardTagFactory extends TagFactory
1819
{
1920
private readonly TagNameExtractor $parts;
2021

@@ -48,6 +49,8 @@ public function __construct(
4849
foreach ($factories as $name => $factory) {
4950
$this->add($factory, $name);
5051
}
52+
53+
parent::__construct();
5154
}
5255

5356
/**
@@ -84,17 +87,15 @@ public function create(string $tag): TagInterface
8487
return $factory->create($body);
8588
} catch (InvalidTagVariableNameException $e) {
8689
$type = $e->getType();
87-
$body = Description::fromNonTagged(
88-
body: \substr($body, $e->getTypeOffset()),
89-
);
90+
$body = $this->createDescription(\substr($body, $e->getTypeOffset()));
9091

9192
if ($type === null) {
9293
return new InvalidTag($name, $body);
9394
}
9495

9596
return new InvalidTypedTag($name, $type, $body);
9697
} catch (\InvalidArgumentException) {
97-
return new InvalidTag($name, Description::fromNonTagged($body));
98+
return new InvalidTag($name, $this->createDescription($body));
9899
}
99100
}
100101

@@ -104,7 +105,7 @@ public function create(string $tag): TagInterface
104105
return new GenericTag($name);
105106
}
106107

107-
return new GenericTag($name, Description::fromNonTagged($body));
108+
return new GenericTag($name, $this->createDescription($body));
108109
}
109110

110111
private function getFactory(string $tag): ?TagFactoryInterface

src/DocBlock/Tag/CommonTagFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ final class CommonTagFactory extends TagFactory
1515
{
1616
/**
1717
* @param class-string<TTag> $class
18-
* @param DescriptionFactoryInterface $descriptions
1918
*/
2019
public function __construct(
2120
private readonly string $class,
22-
DescriptionFactoryInterface $descriptions,
21+
?DescriptionFactoryInterface $descriptions = null,
2322
) {
2423
parent::__construct($descriptions);
2524
}
@@ -29,7 +28,7 @@ public function create(string $tag): Tag
2928
try {
3029
/** @psalm-suppress UnsafeInstantiation */
3130
return new ($this->class)(
32-
description: $this->extractDescription($tag),
31+
description: $this->createDescription($tag),
3332
);
3433
} catch (\Throwable $e) {
3534
throw InvalidTagException::fromException($e);

src/DocBlock/Tag/CommonTypedTagFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class CommonTypedTagFactory extends TypedTagFactory
2020
public function __construct(
2121
private readonly string $class,
2222
ParserInterface $parser,
23-
DescriptionFactoryInterface $descriptions,
23+
?DescriptionFactoryInterface $descriptions = null,
2424
) {
2525
parent::__construct($parser, $descriptions);
2626
}
@@ -33,7 +33,7 @@ public function create(string $tag): TypedTag
3333
/** @psalm-suppress UnsafeInstantiation */
3434
return new ($this->class)(
3535
type: $type,
36-
description: $this->extractDescription($description),
36+
description: $this->createDescription($description),
3737
);
3838
} catch (\Throwable $e) {
3939
throw InvalidTagException::fromException($e);

src/DocBlock/Tag/CommonTypedTagWithNameFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class CommonTypedTagWithNameFactory extends TypedTagFactory
2424
public function __construct(
2525
private readonly string $class,
2626
ParserInterface $parser,
27-
DescriptionFactoryInterface $descriptions,
27+
?DescriptionFactoryInterface $descriptions = null,
2828
) {
2929
$this->variables = new TagVariableExtractor();
3030

@@ -48,7 +48,7 @@ public function create(string $tag): TypedTag
4848
return new ($this->class)(
4949
variable: $variable,
5050
type: $type,
51-
description: $this->extractDescription($description),
51+
description: $this->createDescription($description),
5252
);
5353
} catch (\Throwable $e) {
5454
throw InvalidTagException::fromException($e);

src/DocBlock/Tag/Tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(
1818
Description|string|null $description = null,
1919
) {
2020
if (\is_string($description)) {
21-
$description = Description::fromNonTagged($description);
21+
$description = Description::create($description);
2222
}
2323

2424
$this->description = $description;

src/DocBlock/Tag/TagFactory.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TypeLang\PhpDocParser\DocBlock\Description;
88
use TypeLang\PhpDocParser\DocBlock\DescriptionFactoryInterface;
9+
use TypeLang\PhpDocParser\DocBlock\Tag\TagInterface as TReturn;
910
use TypeLang\PhpDocParser\DocBlock\TagFactoryInterface;
1011

1112
/**
@@ -16,15 +17,30 @@
1617
abstract class TagFactory implements TagFactoryInterface
1718
{
1819
public function __construct(
19-
protected readonly DescriptionFactoryInterface $descriptions,
20+
protected ?DescriptionFactoryInterface $descriptions = null,
2021
) {}
2122

22-
protected function extractDescription(?string $description): ?Description
23+
protected function createDescription(?string $description): ?Description
2324
{
2425
if ($description !== null) {
25-
return $this->descriptions->create($description);
26+
return $this->descriptions?->create($description)
27+
?? Description::create($description)
28+
;
2629
}
2730

2831
return null;
2932
}
33+
34+
public function withDescriptionFactory(?DescriptionFactoryInterface $factory): self
35+
{
36+
$self = clone $this;
37+
$self->descriptions = $factory;
38+
39+
return $self;
40+
}
41+
42+
public function getDescriptionFactory(): ?DescriptionFactoryInterface
43+
{
44+
return $this->descriptions;
45+
}
3046
}

src/DocBlock/Tag/TypedTagFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract class TypedTagFactory extends TagFactory
1919

2020
public function __construct(
2121
ParserInterface $parser,
22-
DescriptionFactoryInterface $descriptions,
22+
?DescriptionFactoryInterface $descriptions = null,
2323
) {
2424
$this->types = new TagTypeExtractor($parser);
2525

src/DocBlock/TagFactoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@ interface TagFactoryInterface
2020
* @return TReturn|InvalidTagInterface A new tag object.
2121
*/
2222
public function create(string $tag): TagInterface;
23+
24+
/**
25+
* @psalm-immutable
26+
*/
27+
public function withDescriptionFactory(?DescriptionFactoryInterface $factory): self;
28+
29+
/**
30+
* @psalm-immutable
31+
*/
32+
public function getDescriptionFactory(): ?DescriptionFactoryInterface;
2333
}

0 commit comments

Comments
 (0)