Skip to content

Commit

Permalink
Add identifier content applicators
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Apr 3, 2024
1 parent a10a69c commit 7b05772
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Tag/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use TypeLang\Parser\ParserInterface as TypesParserInterface;
use TypeLang\PHPDoc\Exception\InvalidTagException;
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
use TypeLang\PHPDoc\Tag\Content\IdentifierApplicator;
use TypeLang\PHPDoc\Tag\Content\OptionalIdentifierApplicator;
use TypeLang\PHPDoc\Tag\Content\OptionalTypeParserApplicator;
use TypeLang\PHPDoc\Tag\Content\ValueApplicator;
use TypeLang\PHPDoc\Tag\Content\OptionalValueApplicator;
Expand Down Expand Up @@ -78,6 +80,25 @@ public function nextOptionalType(TypesParserInterface $parser): ?TypeStatement
return $this->apply(new OptionalTypeParserApplicator($parser));
}

/**
* @api
* @param non-empty-string $tag
* @return non-empty-string
*/
public function nextIdentifier(string $tag): string
{
return $this->apply(new IdentifierApplicator($tag));
}

/**
* @api
* @return non-empty-string|null
*/
public function nextOptionalIdentifier(): ?string
{
return $this->apply(new OptionalIdentifierApplicator());
}

/**
* @api
* @param non-empty-string $tag
Expand Down
39 changes: 39 additions & 0 deletions src/Tag/Content/IdentifierApplicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace TypeLang\PHPDoc\Tag\Content;

use TypeLang\PHPDoc\Exception\InvalidTagException;
use TypeLang\PHPDoc\Tag\Content;

/**
* @template-extends Applicator<non-empty-string>
*/
final class IdentifierApplicator extends Applicator
{
private readonly OptionalIdentifierApplicator $id;

/**
* @param non-empty-string $tag
*/
public function __construct(
private readonly string $tag,
) {
$this->id = new OptionalIdentifierApplicator();
}

/**
* @return non-empty-string
*
* @throws InvalidTagException
*/
public function __invoke(Content $lexer): string
{
return ($this->id)($lexer)
?? throw $lexer->getTagException(\sprintf(
'Tag @%s contains an incorrect identifier value',
$this->tag,
));
}
}
33 changes: 33 additions & 0 deletions src/Tag/Content/OptionalIdentifierApplicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace TypeLang\PHPDoc\Tag\Content;

use TypeLang\PHPDoc\Tag\Content;

/**
* @template-extends Applicator<non-empty-string|null>
*/
final class OptionalIdentifierApplicator extends Applicator
{
/**
* @return non-empty-string|null
*/
public function __invoke(Content $lexer): ?string
{
if ($lexer->value === '') {
return null;
}

\preg_match('/([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\b/u', $lexer->value, $matches);

if (\count($matches) !== 2 || $matches[1] === '') {
return null;
}

$lexer->shift(\strlen($matches[0]));

return $matches[1];
}
}
2 changes: 1 addition & 1 deletion src/Tag/OptionalVariableNameProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ interface OptionalVariableNameProviderInterface
*
* @return non-empty-string|null
*/
public function getVariable(): ?string;
public function getVariableName(): ?string;
}
2 changes: 1 addition & 1 deletion src/Tag/VariableNameProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface VariableNameProviderInterface extends OptionalVariableNameProviderInte
*
* @return non-empty-string
*/
public function getVariable(): string;
public function getVariableName(): string;
}

0 comments on commit 7b05772

Please sign in to comment.