Releases: nicoSWD/php-rule-parser
Releases · nicoSWD/php-rule-parser
Major refactor — recursive descent parser, arithmetic, unary operators, and token system overhaul
Major refactor — recursive descent parser, arithmetic, unary operators, and token system overhaul
Latest
New Features
- Arithmetic operations — Full support for math expressions with proper operator precedence:
+,-,*,/,%. Parentheses can be used to override precedence. (2 + 3 * 4 == 14) - String concatenation — The
+operator now performs string concatenation when either operand is a string, and numeric addition otherwise. ("hello " + "world") - Unary minus and logical NOT operators — Support for negation (
-5), double negation (--5), and logical NOT (!false,!(1 == 2),!foo). - Expression result API —
Rule::result()returns the actual computed value of an expression (e.g.,int(15)for5 * 3), not just true/false. - Syntax highlighter — A built-in custom syntax highlighter with customizable styles for token types.
- Duplicate regex modifier validation — Invalid or duplicate regex modifiers now throw an error.
Major Refactors & Improvements
- Recursive descent parser — Replaced the old parser with a proper recursive descent parser, improving correctness and maintainability.
- Token system simplification — Consolidated 27 token classes into a single
GenericToken+TokenKindenum, drastically reducing complexity. - Lexer streaming —
Lexer::tokenize()converted from array-based to generator-based streaming for better memory efficiency. - Eliminated TokenStream God object — Cleaned up the dependency tree by removing the monolithic
TokenStreamclass. - RuleEngineBuilder — Extracted dependency wiring from the God constructor in
RuleEngineinto a dedicated builder. - Interpreter pattern in AstEvaluator — Replaced fragile
matchexpressions with the Interpreter pattern for cleaner node evaluation. - Consolidated InternalFunction/InternalMethod — Merged into a single
InternalCallableDTO. - Consolidated Token/TokenKind enums — Reduced duplication across token enums.
- Removed dead code — Cleaned up the
Expressiondirectory,EvaluableExpression, unusedTokenIteratormethods, and more. - PSR-12 coding standard — Migrated from PSR-2 to PSR-12.
Bug Fixes
- Division by zero — Now properly handled in the evaluator instead of causing errors.
- String escape sequences — Fixed unescaping of escape sequences in
TokenEncapsedString. - AST cache not used — Fixed double parsing in
Rule.phpwhere the AST cache was never utilized. - Infinite recursion — Fixed infinite recursion in
Rule.phpproperty hook. - Lexer reentrancy — Fixed mutable state / reentrancy issue in the Lexer.
- FunctionRegistry performance — Eliminated unnecessary new instance creation on every function call.
- TokenCollection data structure — Replaced
SplObjectStoragewithArrayObjectto fix wrong data structure usage. - Fragile method prefix fallback — Refactored fragile method prefix fallback loop in
ObjectMethodCaller. - Double evaluation — Eliminated double evaluation in
isValid()/getError(). - Duplicated node evaluation — Fixed duplicated node evaluation logic in
AstEvaluator. - Stack-based token inspection — Replaced fragile
$afterValueboolean with stack-based token inspection.
Dependency Updates
- Updated PHP requirement to
>=8.5 - Updated
phpunit/phpunitto^13.1 - Updated
mockery/mockeryto^1.6 - Updated
squizlabs/php_codesnifferto^4.0
Update for PHP 8.4
No new features, just prettier code
Release for PHP 8
Minor improvements
Release for PHP 8
This release doesn't add any new features, it just uses some of the new PHP 8 syntax.
Use version 0.6 if you're using PHP 7.
Add "not in" operator
Support for custom objects
This release adds the ability to call methods on objects, and use the result as part of the rule.
class User
{
// ...
public function points(): int
{
return 1337;
}
}
$variables = [
'user' => new User(),
];
$rule = new Rule('user.points() > 300', $variables);
var_dump($rule->isTrue()); // bool(true)Add support for custom objects
0.6.0-alpha Add support for custom object calls
Require PHPUnit ^5.4|^6.0
0.5.7 Require phpunit 5.4|6.0
Evaluator is no longer final
0.5.6 Merge branch 'master' of github.com:nicoSWD/php-rule-parser
Evaluator is no longer final
0.5.5 Update .travis.yml