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

Combine string token followed by reference #783

Merged
merged 3 commits into from
Sep 14, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@

<service id="nelmio_alice.fixture_builder.expression_language.lexer.function_lexer"
class="Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\FunctionLexer">
<argument type="service" id="nelmio_alice.fixture_builder.expression_language.lexer.string_then_reference_lexer" />
</service>

<service id="nelmio_alice.fixture_builder.expression_language.lexer.string_then_reference_lexer"
class="Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\StringThenReferenceLexer">
<argument type="service" id="nelmio_alice.fixture_builder.expression_language.lexer.sub_patterns_lexer" />
</service>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer;

use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\LexerInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\TokenType;
use Nelmio\Alice\IsAServiceTrait;

/**
* @internal
*/
final class StringThenReferenceLexer implements LexerInterface
{
use IsAServiceTrait;

/**
* @var LexerInterface
*/
private $decoratedLexer;

public function __construct(LexerInterface $decoratedLexer)
{
$this->decoratedLexer = $decoratedLexer;
}

/**
* {@inheritdoc}
*/
public function lex(string $value): array
{
$tokens = $this->decoratedLexer->lex($value);

$idx = 0;
do {
$token = $tokens[$idx] ?? false;
$nextToken = $tokens[$idx + 1] ?? false;

if (!($token instanceof Token) || !($nextToken instanceof Token)) {
continue;
}

if ($token->getType() === TokenType::STRING_TYPE &&
$nextToken->getType() === TokenType::SIMPLE_REFERENCE_TYPE &&
trim($token->getValue()) !== '' &&
!in_array(substr($token->getValue(), -1), [' ', '\''], true)
) {
array_splice($tokens, $idx, 2, [
new Token(
$token->getValue().$nextToken->getValue(),
new TokenType(TokenType::STRING_TYPE)
)
]);
}

++$idx;
} while ($token !== false && $nextToken !== false);

return $tokens;
}
}
7 changes: 5 additions & 2 deletions src/Loader/NativeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\GlobalPatternsLexer;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\ReferenceEscaperLexer;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\ReferenceLexer;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\StringThenReferenceLexer;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\SubPatternsLexer;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\LexerInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\FunctionFixtureReferenceParser;
Expand Down Expand Up @@ -414,8 +415,10 @@ protected function createLexer(): LexerInterface
new ReferenceEscaperLexer(
new GlobalPatternsLexer(
new FunctionLexer(
new SubPatternsLexer(
new ReferenceLexer()
new StringThenReferenceLexer(
new SubPatternsLexer(
new ReferenceLexer()
)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ public function provideValues()
],
];

yield 'string ending with letter followed by reference character' => [
'[email protected]',
[
new Token('foo', new TokenType(TokenType::STRING_TYPE)),
new Token('\@', new TokenType(TokenType::ESCAPED_VALUE_TYPE)),
new Token('example.com', new TokenType(TokenType::STRING_TYPE)),
],
];

yield 'string ending with number followed by reference character' => [
'[email protected]',
[
new Token('foo', new TokenType(TokenType::STRING_TYPE)),
new Token('[email protected]', new TokenType(TokenType::STRING_TYPE)),
],
];

// Escaped character
yield '[Escape character] nominal (1)' => [
'\\',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer;

use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\LexerInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\TokenType;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;

/**
* @covers \Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\StringThenReferenceLexer
*/
class StringThenReferenceLexerTest extends TestCase
{
public function testIsALexer()
{
$this->assertTrue(is_a(StringThenReferenceLexer::class, LexerInterface::class, true));
}

/**
* @expectedException \Nelmio\Alice\Throwable\Exception\UnclonableException
*/
public function testIsNotClonable()
{
clone new StringThenReferenceLexer(new FakeLexer());
}

public function testMergesNonEmptyStringFollowedByAReference()
{
$value = '[email protected]';
$expected = [
new Token('foo', new TokenType(TokenType::STRING_TYPE)),
new Token('[email protected]', new TokenType(TokenType::STRING_TYPE)),
new Token(' bar', new TokenType(TokenType::STRING_TYPE)),
];

$decoratedLexerProphecy = $this->prophesize(LexerInterface::class);
$decoratedLexerProphecy
->lex($value)
->willReturn(
[
new Token('foo', new TokenType(TokenType::STRING_TYPE)),
new Token('55', new TokenType(TokenType::STRING_TYPE)),
new Token('@example.com', new TokenType(TokenType::SIMPLE_REFERENCE_TYPE)),
new Token(' bar', new TokenType(TokenType::STRING_TYPE)),
]
)
;
/** @var LexerInterface $decoratedLexer */
$decoratedLexer = $decoratedLexerProphecy->reveal();

$lexer = new StringThenReferenceLexer($decoratedLexer);
$actual = $lexer->lex($value);

$this->assertEquals(count($expected), count($actual));
$this->assertEquals($expected, $actual);

$decoratedLexerProphecy->lex(Argument::any())->shouldHaveBeenCalledTimes(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public function provideValues()
'"dummy"',
];

yield 'string ending with letter followed by reference character' => [
'[email protected]',
'[email protected]',
];

yield 'string ending with number followed by reference character' => [
'[email protected]',
'[email protected]',
];

// Escaped character
yield '[Escape character] nominal (1)' => [
'\\',
Expand Down