php-rule-parser icon indicating copy to clipboard operation
php-rule-parser copied to clipboard

Multiple tests cause parser error

Open wbrian-onlineed opened this issue 2 years ago • 2 comments

If a function contains more than one instance of the test function, it complains of a parse error

// example bad rule: /a/.test('a') && /x/.test('x')

Error: "preg_match(): Unknown modifier '.'" File: nicoswd/php-rule-parser/src/Grammar/JavaScript/Methods/Test.php Line: 46 Function: preg_match PHP Version: 7.4.3 (ubuntu)

I have tried reordering them, changing contents of test, and removing "i" modifier. It simply doesn't like it when there are two.

Related side issue: documentation provides use as "foo".test(/oo/i) but actual usage requires the reverse:/oo/i.test("foo")

Thanks for providing this helpful utility.

wbrian-onlineed avatar Jun 07 '23 00:06 wbrian-onlineed

Hey @wbrian-onlineed

Thank you for bringing this up. As of now, all expressions require a comparison operator. Try something like this:

 /a/.test('a') === true && /x/.test('x') === true

I know it's not very intuitive, but it's an issue that resides from where the project originally started. I will eventually fix this at some point.

Let me know if this works for you.

EDIT: Here are some working examples: https://github.com/nicoSWD/php-rule-parser/blob/master/tests/integration/methods/TestTest.php

nicoSWD avatar Jun 07 '23 06:06 nicoSWD

This does not work ;-(

I understand that there needs to be an explicit equality operator present. However, even when present, the parser does not handle two or more tests in the same rule. It will fail with PHP error "preg_match(): Unknown modifier '.'".

This test should produce the error:

$rule = new Rule('/foo/.test(foo) === true && /bar/.test(foo) === false', ['foo' => 'foo']);
var_dump($rule->isTrue()); // PHP error

wbrian-onlineed avatar Jun 08 '23 17:06 wbrian-onlineed