Skip to content

Commit a76c3f0

Browse files
committed
Optimize tags parsing logic
1 parent 1fe2653 commit a76c3f0

File tree

14 files changed

+46
-71
lines changed

14 files changed

+46
-71
lines changed

src/DocBlock/Tag/MethodTag/MethodTagFactory.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use TypeLang\Parser\Parser as TypesParser;
99
use TypeLang\Parser\ParserInterface as TypesParserInterface;
1010
use TypeLang\PHPDoc\DocBlock\Tag\Factory\TagFactoryInterface;
11-
use TypeLang\PHPDoc\Parser\Content\OptionalTypeParserReader;
11+
use TypeLang\PHPDoc\Parser\Content\OptionalTypeReader;
1212
use TypeLang\PHPDoc\Parser\Content\OptionalValueReader;
1313
use TypeLang\PHPDoc\Parser\Content\Stream;
1414
use TypeLang\PHPDoc\Parser\Content\TypeParserReader;
@@ -27,11 +27,11 @@ public function __construct(
2727

2828
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): MethodTag
2929
{
30-
$stream = new Stream($content);
30+
$stream = new Stream($tag, $content);
3131

3232
$isStatic = $stream->apply(new OptionalValueReader('static')) !== null;
33-
$returnType = $stream->apply(new TypeParserReader($tag, $this->parser));
34-
$callableType = $stream->apply(new OptionalTypeParserReader($this->parser));
33+
$returnType = $stream->apply(new TypeParserReader($this->parser));
34+
$callableType = $stream->apply(new OptionalTypeReader($this->parser));
3535

3636
// In case of return type has not been defined then we swap first
3737
// defined type as method signature definition.
@@ -63,9 +63,7 @@ public function create(string $tag, string $content, DescriptionParserInterface
6363
name: $tag,
6464
type: $callableType,
6565
static: $isStatic,
66-
description: \trim($stream->value) !== ''
67-
? $descriptions->parse(\rtrim($stream->value))
68-
: null,
66+
description: $stream->toOptionalDescription($descriptions),
6967
);
7068
}
7169
}

src/DocBlock/Tag/ParamTag/ParamTagFactory.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ private function isVariable(string $content): bool
3333

3434
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): ParamTag
3535
{
36-
$stream = new Stream($content);
36+
$stream = new Stream($tag, $content);
3737

3838
$type = null;
3939
$output = $variadic = false;
4040

4141
if (!$this->isVariable($stream->value)) {
42-
$type = $stream->apply(new TypeParserReader($tag, $this->parser));
42+
$type = $stream->apply(new TypeParserReader($this->parser));
4343
}
4444

4545
if (\str_starts_with($stream->value, '&')) {
@@ -52,17 +52,15 @@ public function create(string $tag, string $content, DescriptionParserInterface
5252
$variadic = true;
5353
}
5454

55-
$variable = $stream->apply(new VariableNameReader($tag));
55+
$variable = $stream->apply(new VariableNameReader());
5656

5757
return new ParamTag(
5858
name: $tag,
5959
type: $type,
6060
variable: $variable,
6161
isVariadic: $variadic,
6262
isOutput: $output,
63-
description: \trim($stream->value) !== ''
64-
? $descriptions->parse(\rtrim($stream->value))
65-
: null,
63+
description: $stream->toOptionalDescription($descriptions),
6664
);
6765
}
6866
}

src/DocBlock/Tag/PropertyTag/PropertyTagFactory.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,20 @@ public function __construct(
2525

2626
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): PropertyTag
2727
{
28-
$stream = new Stream($content);
28+
$stream = new Stream($tag, $content);
2929
$type = null;
3030

3131
if (!\str_starts_with($stream->value, '$')) {
32-
$type = $stream->apply(new TypeParserReader($tag, $this->parser));
32+
$type = $stream->apply(new TypeParserReader($this->parser));
3333
}
3434

35-
$variable = $stream->apply(new VariableNameReader($tag));
35+
$variable = $stream->apply(new VariableNameReader());
3636

3737
return new PropertyTag(
3838
name: $tag,
3939
type: $type,
4040
variable: $variable,
41-
description: \trim($stream->value) !== ''
42-
? $descriptions->parse(\rtrim($stream->value))
43-
: null,
41+
description: $stream->toOptionalDescription($descriptions),
4442
);
4543
}
4644
}

src/DocBlock/Tag/ReturnTag/ReturnTagFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ public function __construct(
2424

2525
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): ReturnTag
2626
{
27-
$stream = new Stream($content);
27+
$stream = new Stream($tag, $content);
2828

2929
return new ReturnTag(
3030
name: $tag,
31-
type: $stream->apply(new TypeParserReader($tag, $this->parser)),
32-
description: \trim($stream->value) !== ''
33-
? $descriptions->parse(\rtrim($stream->value))
34-
: null,
31+
type: $stream->apply(new TypeParserReader($this->parser)),
32+
description: $stream->toOptionalDescription($descriptions),
3533
);
3634
}
3735
}

src/DocBlock/Tag/TemplateExtendsTag/TemplateExtendsTagFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ public function __construct(
2525

2626
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): TemplateExtendsTag
2727
{
28-
$stream = new Stream($content);
28+
$stream = new Stream($tag, $content);
2929

30-
$type = $stream->apply(new TypeParserReader($tag, $this->parser));
30+
$type = $stream->apply(new TypeParserReader($this->parser));
3131

3232
return new TemplateExtendsTag(
3333
name: $tag,
3434
type: $type,
35-
description: \trim($stream->value) !== ''
36-
? $descriptions->parse(\rtrim($stream->value))
37-
: null,
35+
description: $stream->toOptionalDescription($descriptions),
3836
);
3937
}
4038
}

src/DocBlock/Tag/TemplateTag/TemplateTagFactory.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use TypeLang\Parser\ParserInterface as TypesParserInterface;
99
use TypeLang\PHPDoc\DocBlock\Tag\Factory\TagFactoryInterface;
1010
use TypeLang\PHPDoc\Parser\Content\IdentifierReader;
11-
use TypeLang\PHPDoc\Parser\Content\OptionalTypeParserReader;
11+
use TypeLang\PHPDoc\Parser\Content\OptionalTypeReader;
1212
use TypeLang\PHPDoc\Parser\Content\OptionalValueReader;
1313
use TypeLang\PHPDoc\Parser\Content\Stream;
1414
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
@@ -26,15 +26,15 @@ public function __construct(
2626

2727
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): TemplateTag
2828
{
29-
$stream = new Stream($content);
29+
$stream = new Stream($tag, $content);
3030

31-
$template = $stream->apply(new IdentifierReader($tag));
31+
$template = $stream->apply(new IdentifierReader());
3232

3333
$type = null;
3434

3535
$stream->lookahead(function (Stream $stream) use (&$type): bool {
3636
if ($stream->apply(new OptionalValueReader('of')) !== null) {
37-
$type = $stream->apply(new OptionalTypeParserReader($this->parser));
37+
$type = $stream->apply(new OptionalTypeReader($this->parser));
3838
}
3939

4040
return $type !== null;
@@ -44,9 +44,7 @@ public function create(string $tag, string $content, DescriptionParserInterface
4444
name: $tag,
4545
template: $template,
4646
type: $type,
47-
description: \trim($stream->value) !== ''
48-
? $descriptions->parse(\rtrim($stream->value))
49-
: null,
47+
description: $stream->toOptionalDescription($descriptions),
5048
);
5149
}
5250
}

src/DocBlock/Tag/ThrowsTag/ThrowsTagFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ public function __construct(
2424

2525
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): ThrowsTag
2626
{
27-
$stream = new Stream($content);
27+
$stream = new Stream($tag, $content);
2828

2929
return new ThrowsTag(
3030
name: $tag,
31-
type: $stream->apply(new TypeParserReader($tag, $this->parser)),
32-
description: \trim($stream->value) !== ''
33-
? $descriptions->parse(\rtrim($stream->value))
34-
: null,
31+
type: $stream->apply(new TypeParserReader($this->parser)),
32+
description: $stream->toOptionalDescription($descriptions),
3533
);
3634
}
3735
}

src/DocBlock/Tag/VarTag/VarTagFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ public function __construct(
2525

2626
public function create(string $tag, string $content, DescriptionParserInterface $descriptions): VarTag
2727
{
28-
$stream = new Stream($content);
28+
$stream = new Stream($tag, $content);
2929

30-
$type = $stream->apply(new TypeParserReader($tag, $this->parser));
30+
$type = $stream->apply(new TypeParserReader($this->parser));
3131
$variable = $stream->apply(new OptionalVariableNameReader());
3232

3333
return new VarTag(
3434
name: $tag,
3535
type: $type,
3636
variable: $variable,
37-
description: \trim($stream->value) !== ''
38-
? $descriptions->parse(\rtrim($stream->value))
39-
: null,
37+
description: $stream->toOptionalDescription($descriptions),
4038
);
4139
}
4240
}

src/Parser/Content/IdentifierReader.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ final class IdentifierReader extends Reader
1313
{
1414
private readonly OptionalIdentifierReader $id;
1515

16-
/**
17-
* @param non-empty-string $tag
18-
*/
19-
public function __construct(
20-
private readonly string $tag,
21-
) {
16+
public function __construct()
17+
{
2218
$this->id = new OptionalIdentifierReader();
2319
}
2420

@@ -31,7 +27,7 @@ public function __invoke(Stream $stream): string
3127
return ($this->id)($stream)
3228
?? throw $stream->toException(\sprintf(
3329
'Tag @%s contains an incorrect identifier value',
34-
$this->tag,
30+
$stream->tag,
3531
));
3632
}
3733
}

src/Parser/Content/OptionalTypeParserReader.php renamed to src/Parser/Content/OptionalTypeReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* @template-extends Reader<TypeStatement|null>
1414
*/
15-
final class OptionalTypeParserReader extends Reader
15+
final class OptionalTypeReader extends Reader
1616
{
1717
public function __construct(
1818
private readonly TypesParserInterface $parser,

0 commit comments

Comments
 (0)