Skip to content

Commit 7b05772

Browse files
committed
Add identifier content applicators
1 parent a10a69c commit 7b05772

5 files changed

+95
-2
lines changed

src/Tag/Content.php

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use TypeLang\Parser\ParserInterface as TypesParserInterface;
99
use TypeLang\PHPDoc\Exception\InvalidTagException;
1010
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
11+
use TypeLang\PHPDoc\Tag\Content\IdentifierApplicator;
12+
use TypeLang\PHPDoc\Tag\Content\OptionalIdentifierApplicator;
1113
use TypeLang\PHPDoc\Tag\Content\OptionalTypeParserApplicator;
1214
use TypeLang\PHPDoc\Tag\Content\ValueApplicator;
1315
use TypeLang\PHPDoc\Tag\Content\OptionalValueApplicator;
@@ -78,6 +80,25 @@ public function nextOptionalType(TypesParserInterface $parser): ?TypeStatement
7880
return $this->apply(new OptionalTypeParserApplicator($parser));
7981
}
8082

83+
/**
84+
* @api
85+
* @param non-empty-string $tag
86+
* @return non-empty-string
87+
*/
88+
public function nextIdentifier(string $tag): string
89+
{
90+
return $this->apply(new IdentifierApplicator($tag));
91+
}
92+
93+
/**
94+
* @api
95+
* @return non-empty-string|null
96+
*/
97+
public function nextOptionalIdentifier(): ?string
98+
{
99+
return $this->apply(new OptionalIdentifierApplicator());
100+
}
101+
81102
/**
82103
* @api
83104
* @param non-empty-string $tag
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PHPDoc\Tag\Content;
6+
7+
use TypeLang\PHPDoc\Exception\InvalidTagException;
8+
use TypeLang\PHPDoc\Tag\Content;
9+
10+
/**
11+
* @template-extends Applicator<non-empty-string>
12+
*/
13+
final class IdentifierApplicator extends Applicator
14+
{
15+
private readonly OptionalIdentifierApplicator $id;
16+
17+
/**
18+
* @param non-empty-string $tag
19+
*/
20+
public function __construct(
21+
private readonly string $tag,
22+
) {
23+
$this->id = new OptionalIdentifierApplicator();
24+
}
25+
26+
/**
27+
* @return non-empty-string
28+
*
29+
* @throws InvalidTagException
30+
*/
31+
public function __invoke(Content $lexer): string
32+
{
33+
return ($this->id)($lexer)
34+
?? throw $lexer->getTagException(\sprintf(
35+
'Tag @%s contains an incorrect identifier value',
36+
$this->tag,
37+
));
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PHPDoc\Tag\Content;
6+
7+
use TypeLang\PHPDoc\Tag\Content;
8+
9+
/**
10+
* @template-extends Applicator<non-empty-string|null>
11+
*/
12+
final class OptionalIdentifierApplicator extends Applicator
13+
{
14+
/**
15+
* @return non-empty-string|null
16+
*/
17+
public function __invoke(Content $lexer): ?string
18+
{
19+
if ($lexer->value === '') {
20+
return null;
21+
}
22+
23+
\preg_match('/([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\b/u', $lexer->value, $matches);
24+
25+
if (\count($matches) !== 2 || $matches[1] === '') {
26+
return null;
27+
}
28+
29+
$lexer->shift(\strlen($matches[0]));
30+
31+
return $matches[1];
32+
}
33+
}

src/Tag/OptionalVariableNameProviderInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ interface OptionalVariableNameProviderInterface
1515
*
1616
* @return non-empty-string|null
1717
*/
18-
public function getVariable(): ?string;
18+
public function getVariableName(): ?string;
1919
}

src/Tag/VariableNameProviderInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ interface VariableNameProviderInterface extends OptionalVariableNameProviderInte
1414
*
1515
* @return non-empty-string
1616
*/
17-
public function getVariable(): string;
17+
public function getVariableName(): string;
1818
}

0 commit comments

Comments
 (0)