Skip to content

Commit a76c3f0

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

14 files changed

+46
-71
lines changed

Diff for: src/DocBlock/Tag/MethodTag/MethodTagFactory.php

+5-7
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
}

Diff for: src/DocBlock/Tag/ParamTag/ParamTagFactory.php

+4-6
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
}

Diff for: src/DocBlock/Tag/PropertyTag/PropertyTagFactory.php

+4-6
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
}

Diff for: src/DocBlock/Tag/ReturnTag/ReturnTagFactory.php

+3-5
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
}

Diff for: src/DocBlock/Tag/TemplateExtendsTag/TemplateExtendsTagFactory.php

+3-5
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
}

Diff for: src/DocBlock/Tag/TemplateTag/TemplateTagFactory.php

+5-7
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
}

Diff for: src/DocBlock/Tag/ThrowsTag/ThrowsTagFactory.php

+3-5
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
}

Diff for: src/DocBlock/Tag/VarTag/VarTagFactory.php

+3-5
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
}

Diff for: src/Parser/Content/IdentifierReader.php

+3-7
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
}

Diff for: src/Parser/Content/OptionalTypeParserReader.php renamed to src/Parser/Content/OptionalTypeReader.php

+1-1
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,

Diff for: src/Parser/Content/Stream.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ final class Stream implements \Stringable
1717
*/
1818
public int $offset = 0;
1919

20-
public function __construct(public string $value)
21-
{
20+
public function __construct(
21+
/**
22+
* @var non-empty-string
23+
*/
24+
public readonly string $tag,
25+
public string $value,
26+
) {
2227
$this->source = $this->value;
2328
}
2429

Diff for: src/Parser/Content/TypeParserReader.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414
*/
1515
final class TypeParserReader extends Reader
1616
{
17-
/**
18-
* @param non-empty-string $tag
19-
*/
2017
public function __construct(
21-
private readonly string $tag,
2218
private readonly TypesParserInterface $parser,
2319
) {}
2420

@@ -32,7 +28,7 @@ public function __invoke(Stream $stream): TypeStatement
3228
$type = $this->parser->parse($stream->value);
3329
} catch (ParserExceptionInterface $e) {
3430
throw $stream->toException(
35-
message: \sprintf('Tag @%s contains an incorrect type', $this->tag),
31+
message: \sprintf('Tag @%s contains an incorrect type', $stream->tag),
3632
previous: $e,
3733
);
3834
}

Diff for: src/Parser/Content/ValueReader.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ final class ValueReader extends Reader
1818
private readonly OptionalValueReader $identifier;
1919

2020
/**
21-
* @param non-empty-string $tag
2221
* @param T $value
2322
*/
2423
public function __construct(
25-
private readonly string $tag,
2624
private readonly string $value,
2725
) {
2826
$this->identifier = new OptionalValueReader($value);
@@ -38,7 +36,7 @@ public function __invoke(Stream $stream): string
3836
return ($this->identifier)($stream)
3937
?? throw $stream->toException(\sprintf(
4038
'Tag @%s contains an incorrect identifier value "%s"',
41-
$this->tag,
39+
$stream->tag,
4240
$this->value,
4341
));
4442
}

Diff for: src/Parser/Content/VariableNameReader.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ final class VariableNameReader extends Reader
1313
{
1414
private readonly OptionalVariableNameReader $var;
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->var = new OptionalVariableNameReader();
2319
}
2420

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

0 commit comments

Comments
 (0)