Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using enums as a token type #77

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/Doctrine/Common/Lexer/AbstractLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Doctrine\Common\Lexer;

use ReflectionClass;
use UnitEnum;

use function get_class;
use function implode;
use function preg_split;
use function sprintf;
Expand All @@ -18,7 +20,7 @@
/**
* Base class for writing simple lexers, i.e. for creating small DSLs.
*
* @template T of string|int
* @template T of UnitEnum|string|int
*/
abstract class AbstractLexer
{
Expand Down Expand Up @@ -275,13 +277,18 @@ protected function scan($input)
/**
* Gets the literal for a given token.
*
* @param int|string $token
* @param T $token
*
* @return int|string
*/
public function getLiteral($token)
{
if ($token instanceof UnitEnum) {
return get_class($token) . '::' . $token->name;
}

$className = static::class;

$reflClass = new ReflectionClass($className);
$constants = $reflClass->getConstants();

Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/Common/Lexer/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use ArrayAccess;
use Doctrine\Deprecations\Deprecation;
use ReturnTypeWillChange;
use UnitEnum;

use function in_array;

/**
* @template T of string|int
* @template T of UnitEnum|string|int
* @implements ArrayAccess<string,mixed>
*/
final class Token implements ArrayAccess
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/Common/Lexer/AbstractLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ public function testGetLiteral(): void
$this->assertSame('fake_token', $this->concreteLexer->getLiteral('fake_token'));
}

/**
* @requires PHP 8.1
*/
public function testGetLiteralWithEnumLexer(): void
{
$enumLexer = new EnumLexer();
$this->assertSame(
'Doctrine\Tests\Common\Lexer\TokenType::OPERATOR',
$enumLexer->getLiteral(TokenType::OPERATOR)
);
}

public function testIsA(): void
{
$this->assertTrue($this->concreteLexer->isA(11, 'int'));
Expand Down
58 changes: 58 additions & 0 deletions tests/Doctrine/Common/Lexer/EnumLexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Lexer;

use Doctrine\Common\Lexer\AbstractLexer;

use function in_array;
use function is_numeric;
use function is_string;

/** @extends AbstractLexer<TokenType> */
class EnumLexer extends AbstractLexer
{
/**
* {@inheritDoc}
*/
protected function getCatchablePatterns(): array
{
return [
'=|<|>',
'[a-z]+',
'\d+',
];
}

/**
* {@inheritDoc}
*/
protected function getNonCatchablePatterns(): array
{
return [
'\s+',
'(.)',
];
}

/**
* {@inheritDoc}
*/
protected function getType(&$value): TokenType
{
if (is_numeric($value)) {
$value = (int) $value;

return TokenType::INT;
}

if (in_array($value, ['=', '<', '>'])) {
return TokenType::OPERATOR;
}

if (is_string($value)) {
return TokenType::STRING;
}
}
}
12 changes: 12 additions & 0 deletions tests/Doctrine/Common/Lexer/TokenType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Lexer;

enum TokenType
{
case INT;
case OPERATOR;
case STRING;
}