Skip to content
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
2 changes: 2 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ tools:
enabled: true
excluded_dirs: [tests]
build:
environment:
php: 8.0.1
nodes:
analysis:
tests:
Expand Down
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
preset: psr2
risky: true
version: 8
finder:
name:
- "*.php"
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0

script:
- composer install --dev
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## PHP Rule Engine for PHP 7
## PHP Rule Engine


[![Latest Stable Version](https://travis-ci.org/nicoSWD/php-rule-parser.svg?branch=master)](https://travis-ci.org/nicoSWD/php-rule-parser)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nicoswd/php-rule-parser",
"type": "library",
"description": "PHP 7 Rule Engine - Rule Parser & Evaluator",
"description": "Rule Engine - Rule Parser & Evaluator",
"keywords": [
"rule",
"parser",
Expand Down Expand Up @@ -33,7 +33,7 @@
}
},
"require": {
"php": ">=7.1"
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^9.0",
Expand Down
23 changes: 11 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="src/autoload.php">
<testsuites>
<testsuite name="Unit tests">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit tests">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
37 changes: 18 additions & 19 deletions src/Compiler/StandardCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@

class StandardCompiler implements CompilerInterface
{
const BOOL_TRUE = '1';
const BOOL_FALSE = '0';
private const BOOL_TRUE = '1';
private const BOOL_FALSE = '0';

const LOGICAL_AND = '&';
const LOGICAL_OR = '|';
private const LOGICAL_AND = '&';
private const LOGICAL_OR = '|';

const OPENING_PARENTHESIS = '(';
const CLOSING_PARENTHESIS = ')';
private const OPENING_PARENTHESIS = '(';
private const CLOSING_PARENTHESIS = ')';

/** @var string */
private $output = '';
/** @var int */
private $openParenthesis = 0;
/** @var int */
private $closedParenthesis = 0;
private string $output = '';
private int $openParenthesis = 0;
private int $closedParenthesis = 0;

public function getCompiledRule(): string
{
Expand All @@ -48,6 +45,7 @@ private function openParenthesis(): void
$this->output .= self::OPENING_PARENTHESIS;
}

/** @throws ParserException */
private function closeParenthesis(): void
{
if ($this->openParenthesis < 1) {
Expand All @@ -58,6 +56,7 @@ private function closeParenthesis(): void
$this->output .= self::CLOSING_PARENTHESIS;
}

/** @throws ParserException */
public function addParentheses(BaseToken $token): void
{
if ($token instanceof TokenOpeningParenthesis) {
Expand All @@ -70,6 +69,7 @@ public function addParentheses(BaseToken $token): void
}
}

/** @throws ParserException */
public function addLogical(BaseToken $token): void
{
$lastChar = $this->getLastChar();
Expand All @@ -85,6 +85,7 @@ public function addLogical(BaseToken $token): void
}
}

/** @throws MissingOperatorException */
public function addBoolean(bool $bool): void
{
$lastChar = $this->getLastChar();
Expand All @@ -105,26 +106,24 @@ private function isIncompleteCondition(): bool
{
$lastChar = $this->getLastChar();

return (
return
$lastChar === self::LOGICAL_AND ||
$lastChar === self::LOGICAL_OR
);
$lastChar === self::LOGICAL_OR;
}

private function expectOpeningParenthesis(): bool
{
$lastChar = $this->getLastChar();

return (
return
$lastChar === '' ||
$lastChar === self::LOGICAL_AND ||
$lastChar === self::LOGICAL_OR ||
$lastChar === self::OPENING_PARENTHESIS
);
$lastChar === self::OPENING_PARENTHESIS;
}

private function getLastChar(): string
{
return substr($this->output, -1);
return substr($this->output, offset: -1);
}
}
12 changes: 6 additions & 6 deletions src/Evaluator/Evaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

final class Evaluator implements EvaluatorInterface
{
const LOGICAL_AND = '&';
const LOGICAL_OR = '|';
private const LOGICAL_AND = '&';
private const LOGICAL_OR = '|';

const BOOL_TRUE = '1';
const BOOL_FALSE = '0';
private const BOOL_TRUE = '1';
private const BOOL_FALSE = '0';

public function evaluate(string $group): bool
{
Expand All @@ -25,8 +25,8 @@ public function evaluate(string $group): bool
'~\(([^()]+)\)~',
$evalGroup,
$group,
-1,
$count
limit: -1,
count: $count
);
} while ($count > 0);

Expand Down
10 changes: 4 additions & 6 deletions src/Expression/BaseExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
*/
namespace nicoSWD\Rule\Expression;

use nicoSWD\Rule\Parser\Exception\ParserException;

abstract class BaseExpression
{
/**
* @param mixed $leftValue
* @param mixed $rightValue
* @return bool
*/
abstract public function evaluate($leftValue, $rightValue): bool;
/** @throws ParserException */
abstract public function evaluate(mixed $leftValue, mixed $rightValue): bool;
}
2 changes: 1 addition & 1 deletion src/Expression/EqualExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class EqualExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
return $leftValue == $rightValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Expression/EqualStrictExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

final class EqualStrictExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
if ($leftValue instanceof TokenCollection) {
$leftValue = $leftValue->toArray();
Expand Down
33 changes: 17 additions & 16 deletions src/Expression/ExpressionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@
*/
namespace nicoSWD\Rule\Expression;

use nicoSWD\Rule\Parser\Exception\ParserException;
use nicoSWD\Rule\TokenStream\Token;
use nicoSWD\Rule\TokenStream\Token\BaseToken;

class ExpressionFactory implements ExpressionFactoryInterface
{
/** @var array<string, string> */
protected $classLookup = [
Token\TokenEqual::class => EqualExpression::class,
Token\TokenEqualStrict::class => EqualStrictExpression::class,
Token\TokenNotEqual::class => NotEqualExpression::class,
Token\TokenNotEqualStrict::class => NotEqualStrictExpression::class,
Token\TokenGreater::class => GreaterThanExpression::class,
Token\TokenSmaller::class => LessThanExpression::class,
Token\TokenSmallerEqual::class => LessThanEqualExpression::class,
Token\TokenGreaterEqual::class => GreaterThanEqualExpression::class,
Token\TokenIn::class => InExpression::class,
Token\TokenNotIn::class => NotInExpression::class,
];

public function createFromOperator(Token\BaseToken $operator): BaseExpression
/** @throws ParserException */
public function createFromOperator(BaseToken $operator): BaseExpression
{
return new $this->classLookup[get_class($operator)]();
return match (get_class($operator)) {
Token\TokenEqual::class => new EqualExpression(),
Token\TokenEqualStrict::class => new EqualStrictExpression(),
Token\TokenNotEqual::class => new NotEqualExpression(),
Token\TokenNotEqualStrict::class => new NotEqualStrictExpression(),
Token\TokenGreater::class => new GreaterThanExpression(),
Token\TokenSmaller::class => new LessThanExpression(),
Token\TokenSmallerEqual::class => new LessThanEqualExpression(),
Token\TokenGreaterEqual::class => new GreaterThanEqualExpression(),
Token\TokenIn::class => new InExpression(),
Token\TokenNotIn::class => new NotInExpression(),
default => throw ParserException::unknownOperator($operator),
};
}
}
2 changes: 1 addition & 1 deletion src/Expression/GreaterThanEqualExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class GreaterThanEqualExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
return $leftValue >= $rightValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Expression/GreaterThanExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class GreaterThanExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
return $leftValue > $rightValue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/InExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

final class InExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
if ($rightValue instanceof TokenCollection) {
$rightValue = $rightValue->toArray();
Expand All @@ -25,6 +25,6 @@ public function evaluate($leftValue, $rightValue): bool
));
}

return in_array($leftValue, $rightValue, true);
return in_array($leftValue, $rightValue, strict: true);
}
}
2 changes: 1 addition & 1 deletion src/Expression/LessThanEqualExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class LessThanEqualExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
return $leftValue <= $rightValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Expression/LessThanExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class LessThanExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
return $leftValue < $rightValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Expression/NotEqualExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class NotEqualExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
return $leftValue != $rightValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Expression/NotEqualStrictExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class NotEqualStrictExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
return $leftValue !== $rightValue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/NotInExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

final class NotInExpression extends BaseExpression
{
public function evaluate($leftValue, $rightValue): bool
public function evaluate(mixed $leftValue, mixed $rightValue): bool
{
if ($rightValue instanceof TokenCollection) {
$rightValue = $rightValue->toArray();
Expand All @@ -25,6 +25,6 @@ public function evaluate($leftValue, $rightValue): bool
));
}

return !in_array($leftValue, $rightValue, true);
return !in_array($leftValue, $rightValue, strict: true);
}
}
6 changes: 1 addition & 5 deletions src/Grammar/CallableFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@

abstract class CallableFunction implements CallableUserFunctionInterface
{
/** @var ?BaseToken */
protected $token;

public function __construct(BaseToken $token = null)
public function __construct(protected ?BaseToken $token = null)
{
$this->token = $token;
}

protected function parseParameter(array $parameters, int $numParam): ?BaseToken
Expand Down
2 changes: 1 addition & 1 deletion src/Grammar/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

abstract class Grammar
{
/** @return array<array> */
/** @return array<int, array<int, mixed>> */
abstract public function getDefinition(): array;

/** @return array<string, string> */
Expand Down
2 changes: 1 addition & 1 deletion src/Grammar/JavaScript/Functions/ParseFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ParseFloat extends CallableFunction implements CallableUserFunctionI
{
public function call(?BaseToken ...$parameters): BaseToken
{
$value = $this->parseParameter($parameters, 0);
$value = $this->parseParameter($parameters, numParam: 0);

if (!isset($value)) {
return new TokenFloat(NAN);
Expand Down
Loading