Skip to content

Commit

Permalink
Make ExpressionParser::parseNameToStringMapping non-static.
Browse files Browse the repository at this point in the history
  • Loading branch information
drjayvee committed Aug 29, 2024
1 parent cde0426 commit 917cdd5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,14 @@ public function parseMappingExpression()
}

/*
* This method is static to make it testable.
* Calling `$this->parser->getStream()` as done in `parseMappingExpression()` only works if
* `parser->parse()` has been called, but that won't ever call this method.
* Unlike its sibling methods above, method requires a TokenStream parameter.
*
* The reason is that calling `$this->parser->getStream()` (as in `parseMappingExpression()`)
* only works _while_ `parser->parse()` is running, because that method sets and unsets the $stream property.
*
* The upside is that this method is fully pure, since it doesn't depend on the state of $this.
*/
public static function parseNameToStringMapping(TokenStream $stream, bool $allowOptionalKeys): ArrayExpression
public function parseNameToStringMapping(TokenStream $stream, bool $allowOptionalKeys): ArrayExpression
{
$stream->expect(Token::PUNCTUATION_TYPE, '{', 'A mapping element was expected');

Expand Down
2 changes: 1 addition & 1 deletion src/TokenParser/TypesTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function parse(Token $token): Node
{
$stream = $this->parser->getStream();

$expression = ExpressionParser::parseNameToStringMapping($stream, true);
$expression = $this->parser->getExpressionParser()->parseNameToStringMapping($stream, true);

$stream->expect(Token::BLOCK_END_TYPE);

Expand Down
17 changes: 12 additions & 5 deletions tests/ExpressionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,13 @@ public function getTestsForSequence()
public function testNameToStringMapping(string $template, ArrayExpression $expected, bool $allowOptionalKeys): void
{
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$parser = new Parser($env);
$stream = $env->tokenize(new Source($template, ''));
$stream->next();

$this->assertEquals($expected, ExpressionParser::parseNameToStringMapping($stream, $allowOptionalKeys));

$expressionParser = new ExpressionParser($parser, $env);

$this->assertEquals($expected, $expressionParser->parseNameToStringMapping($stream, $allowOptionalKeys));
}

public function getTestsForNameToStringMapping(): array
Expand Down Expand Up @@ -274,11 +277,15 @@ public function getTestsForNameToStringMapping(): array
public function testNameToStringMappingSyntaxError(string $template, bool $allowOptionalKeys): void
{
$this->expectException(SyntaxError::class);

$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$parser = new Parser($env);
$stream = $env->tokenize(new Source($template, ''));

ExpressionParser::parseNameToStringMapping($stream, $allowOptionalKeys);
$stream->next();

$expressionParser = new ExpressionParser($parser, $env);

$expressionParser->parseNameToStringMapping($stream, $allowOptionalKeys);
}

public function getFailingTestsForNameToStringMapping(): array
Expand Down

0 comments on commit 917cdd5

Please sign in to comment.