Skip to content

Commit

Permalink
fix query: sequential member access was not resetting method arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rasteiner committed Dec 3, 2024
1 parent c7da7b7 commit a22d4ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Toolkit/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ private function memberAccess(): Node
throw new Exception('Expect property name after ".".');
}

if ($this->match(TokenType::T_OPEN_PAREN)) {
$arguments = $this->argumentList();
}
$arguments = match ($this->match(TokenType::T_OPEN_PAREN)) {
false => null,
default => $this->argumentList(),
};

$left = new MemberAccessNode(
$left,
$right,
$arguments ?? null,
$arguments,
$nullSafe
);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Toolkit/Query/ParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Kirby\Toolkit\Query;
use Kirby\TestCase;
use Kirby\Toolkit\Query\AST\ArgumentListNode;
use Kirby\Toolkit\Query\AST\LiteralNode;
use Kirby\Toolkit\Query\AST\MemberAccessNode;
use Kirby\Toolkit\Query\AST\VariableNode;
use Kirby\Toolkit\Query\Parser;
use Kirby\Toolkit\Query\Tokenizer;

class ParserTest extends TestCase {
function testMemberAccess() {
$tokenizer = new Tokenizer('user.name');
$ast = (new Parser($tokenizer))->parse();
$this->assertEquals($ast,
new MemberAccessNode(
new VariableNode('user'), 'name'
)
);
}

function testSquentialMemberAccess() {
$tokenizer = new Tokenizer('user.name("arg").age');
$ast = (new Parser($tokenizer))->parse();
$this->assertEquals($ast,
new MemberAccessNode(
new MemberAccessNode(
new VariableNode('user'),
'name',
new ArgumentListNode([new LiteralNode("arg")])
),
'age'
)
);
}
}

0 comments on commit a22d4ca

Please sign in to comment.