Skip to content

Commit

Permalink
Merge pull request doctrine#77 from greg0ire/allow-enums
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire authored Dec 9, 2022
2 parents c63c80d + 0186569 commit d617a49
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
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;
}

0 comments on commit d617a49

Please sign in to comment.