Skip to content

Commit

Permalink
Manual fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Dec 2, 2020
1 parent ad32f24 commit 863bff4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
23 changes: 15 additions & 8 deletions lib/Doctrine/Common/Lexer/AbstractLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ abstract class AbstractLexer
* parameter, none)
* - 'position' : the position of the token in the input string
*
* @var array
* @var mixed[][]
* @psalm-var list<array{value: string, type: string|int|null, position: int}>
*/
private $tokens = [];

Expand All @@ -58,14 +59,16 @@ abstract class AbstractLexer
/**
* The next token in the input.
*
* @var array|null
* @var mixed[]|null
* @psalm-var array{value: string, type: string|int|null, position: int}|null
*/
public $lookahead;

/**
* The last matched/seen token.
*
* @var array|null
* @var mixed[]|null
* @psalm-var array{value: string, type: string|int|null, position: int}|null
*/
public $token;

Expand Down Expand Up @@ -157,7 +160,7 @@ public function isNextToken($token)
/**
* Checks whether any of the given tokens matches the current lookahead.
*
* @param array $tokens
* @param string[] $tokens
*
* @return bool
*/
Expand Down Expand Up @@ -211,7 +214,9 @@ public function isA($value, $token)
/**
* Moves the lookahead token forward.
*
* @return array|null The next token or NULL if there are no more tokens ahead.
* @return mixed[]|null The next token or NULL if there are no more tokens ahead.
*
* @psalm-return array{value: string, type: string|int|null, position: int}|null
*/
public function peek()
{
Expand All @@ -225,7 +230,9 @@ public function peek()
/**
* Peeks at the next token, returns it and immediately resets the peek.
*
* @return array|null The next token or NULL if there are no more tokens ahead.
* @return mixed[]|null The next token or NULL if there are no more tokens ahead.
*
* @psalm-return array{value: string, type: string|int|null, position: int}|null
*/
public function glimpse()
{
Expand Down Expand Up @@ -308,14 +315,14 @@ protected function getModifiers()
/**
* Lexical catchable patterns.
*
* @return array
* @return string[]
*/
abstract protected function getCatchablePatterns();

/**
* Lexical non-catchable patterns.
*
* @return array
* @return string[]
*/
abstract protected function getNonCatchablePatterns();

Expand Down
65 changes: 30 additions & 35 deletions tests/Doctrine/Common/Lexer/AbstractLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function tearDown(): void
setlocale(LC_ALL, null);
}

public function dataProvider()
/**
* @psalm-return list<array{string, list<array{value: string|int, type: string, position: int}>}>
*/
public function dataProvider(): array
{
return [
[
Expand All @@ -53,7 +56,7 @@ public function dataProvider()
];
}

public function testResetPeek()
public function testResetPeek(): void
{
$expectedTokens = [
[
Expand Down Expand Up @@ -81,7 +84,7 @@ public function testResetPeek()
$this->assertEquals($expectedTokens[0], $this->concreteLexer->peek());
}

public function testResetPosition()
public function testResetPosition(): void
{
$expectedTokens = [
[
Expand Down Expand Up @@ -117,12 +120,10 @@ public function testResetPosition()
}

/**
* @param string $input
* @param array $expectedTokens
*
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @dataProvider dataProvider
*/
public function testMoveNext($input, $expectedTokens)
public function testMoveNext(string $input, array $expectedTokens): void
{
$this->concreteLexer->setInput($input);
$this->assertNull($this->concreteLexer->lookahead);
Expand All @@ -136,7 +137,7 @@ public function testMoveNext($input, $expectedTokens)
$this->assertNull($this->concreteLexer->lookahead);
}

public function testSkipUntil()
public function testSkipUntil(): void
{
$this->concreteLexer->setInput('price=10');

Expand All @@ -153,7 +154,7 @@ public function testSkipUntil()
);
}

public function testUtf8Mismatch()
public function testUtf8Mismatch(): void
{
$this->concreteLexer->setInput("\xE9=10");

Expand All @@ -170,12 +171,10 @@ public function testUtf8Mismatch()
}

/**
* @param string $input
* @param array $expectedTokens
*
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @dataProvider dataProvider
*/
public function testPeek($input, $expectedTokens)
public function testPeek(string $input, array $expectedTokens): void
{
$this->concreteLexer->setInput($input);
foreach ($expectedTokens as $expectedToken) {
Expand All @@ -186,12 +185,10 @@ public function testPeek($input, $expectedTokens)
}

/**
* @param string $input
* @param array $expectedTokens
*
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @dataProvider dataProvider
*/
public function testGlimpse($input, $expectedTokens)
public function testGlimpse(string $input, array $expectedTokens): void
{
$this->concreteLexer->setInput($input);

Expand All @@ -203,34 +200,34 @@ public function testGlimpse($input, $expectedTokens)
$this->assertNull($this->concreteLexer->peek());
}

public function inputUntilPositionDataProvider()
/**
* @psalm-return list<array{string, int, string}>
*/
public function inputUntilPositionDataProvider(): array
{
return [
['price=10', 5, 'price'],
];
}

/**
* @param string $input
* @param int $position
* @param string $expectedInput
*
* @dataProvider inputUntilPositionDataProvider
*/
public function testGetInputUntilPosition($input, $position, $expectedInput)
{
public function testGetInputUntilPosition(
string $input,
int $position,
string $expectedInput
): void {
$this->concreteLexer->setInput($input);

$this->assertSame($expectedInput, $this->concreteLexer->getInputUntilPosition($position));
}

/**
* @param string $input
* @param array $expectedTokens
*
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @dataProvider dataProvider
*/
public function testIsNextToken($input, $expectedTokens)
public function testIsNextToken(string $input, array $expectedTokens): void
{
$this->concreteLexer->setInput($input);

Expand All @@ -242,12 +239,10 @@ public function testIsNextToken($input, $expectedTokens)
}

/**
* @param string $input
* @param array $expectedTokens
*
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @dataProvider dataProvider
*/
public function testIsNextTokenAny($input, $expectedTokens)
public function testIsNextTokenAny(string $input, array $expectedTokens): void
{
$allTokenTypes = array_map(static function ($token) {
return $token['type'];
Expand All @@ -263,13 +258,13 @@ public function testIsNextTokenAny($input, $expectedTokens)
}
}

public function testGetLiteral()
public function testGetLiteral(): void
{
$this->assertSame('Doctrine\Tests\Common\Lexer\ConcreteLexer::INT', $this->concreteLexer->getLiteral('int'));
$this->assertSame('fake_token', $this->concreteLexer->getLiteral('fake_token'));
}

public function testIsA()
public function testIsA(): void
{
$this->assertTrue($this->concreteLexer->isA(11, 'int'));
$this->assertTrue($this->concreteLexer->isA(1.1, 'int'));
Expand All @@ -279,7 +274,7 @@ public function testIsA()
$this->assertTrue($this->concreteLexer->isA('fake_text', 'string'));
}

public function testAddCatchablePatternsToMutableLexer()
public function testAddCatchablePatternsToMutableLexer(): void
{
$mutableLexer = new MutableLexer();
$mutableLexer->addCatchablePattern('[a-z]');
Expand Down
11 changes: 9 additions & 2 deletions tests/Doctrine/Common/Lexer/ConcreteLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class ConcreteLexer extends AbstractLexer
{
public const INT = 'int';

/**
* {@inheritDoc}
*/
protected function getCatchablePatterns()
{
return [
Expand All @@ -23,6 +26,9 @@ protected function getCatchablePatterns()
];
}

/**
* {@inheritDoc}
*/
protected function getNonCatchablePatterns()
{
return [
Expand All @@ -31,6 +37,9 @@ protected function getNonCatchablePatterns()
];
}

/**
* {@inheritDoc}
*/
protected function getType(&$value)
{
if (is_numeric($value)) {
Expand All @@ -46,7 +55,5 @@ protected function getType(&$value)
if (is_string($value)) {
return 'string';
}

return null;
}
}
14 changes: 14 additions & 0 deletions tests/Doctrine/Common/Lexer/MutableLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ class MutableLexer extends AbstractLexer
/** @var string[] */
private $catchablePatterns = [];

/**
* @param string $pattern
*
* @return void
*/
public function addCatchablePattern($pattern)
{
$this->catchablePatterns[] = $pattern;
}

/**
* {@inheritDoc}
*/
protected function getCatchablePatterns()
{
return $this->catchablePatterns;
}

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

/**
* {@inheritDoc}
*/
protected function getType(&$value)
{
return 1;
Expand Down

0 comments on commit 863bff4

Please sign in to comment.