Evaluator fixes/enhancements + tests

This commit is contained in:
odain
2025-09-05 15:21:15 +02:00
parent 11f142b782
commit 7e7b5874a6
7 changed files with 164 additions and 93 deletions

View File

@@ -0,0 +1,16 @@
<?php
namespace Combodo\iTop\PhpParser\Evaluation;
use PhpParser\Node\Expr\BinaryOp\Identical;
class IdenticalEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): ?string {
return Identical::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left === $right;
}
}

View File

@@ -15,8 +15,12 @@ class IssetEvaluator extends AbstractExprEvaluator {
/** @var Isset_ $oExpr */
foreach ($oExpr->vars as $oVar){
$var = PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oVar);
if (! isset($var)){
try{
$var = PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oVar);
if (is_null($var)){
return false;
}
} catch (\Throwable $t) {
return false;
}
}

View File

@@ -12,10 +12,19 @@ class PhpExpressionEvaluator {
/** @var iExprEvaluator[] $aPhpParserEvaluators */
private static array $aPhpParserEvaluators;
private int $iMode=self::ITOP_ALGO;
protected function __construct() {
}
const LIB_AND_FALLBACK=1;
const LIB_ONLY=2;
const ITOP_ALGO=3;
public function SetMode($iMode)
{
$this->iMode =$iMode;
}
final public static function GetInstance(): PhpExpressionEvaluator {
if (!isset(static::$oInstance)) {
static::$oInstance = new static();
@@ -58,13 +67,13 @@ class PhpExpressionEvaluator {
static::$oInstance = $oInstance;
}
public function EvaluateExpression(Expr $oExpression, int $iMode=self::LIB_AND_FALLBACK) : mixed
public function EvaluateExpression(Expr $oExpression) : mixed
{
if ($iMode==self::ITOP_ALGO){
if ($this->iMode===self::ITOP_ALGO){
return $this->EvaluateExpressionLocally($oExpression);
}
if ($iMode==self::LIB_ONLY){
if ($this->iMode==self::LIB_ONLY){
$oConstExprEvaluator = new ConstExprEvaluator();
} else {
$oConstExprEvaluator = new ConstExprEvaluator([$this, "EvaluateExpressionLocally"]);
@@ -97,10 +106,7 @@ class PhpExpressionEvaluator {
return $this->ParseAndEvaluateExpression($sBooleanExpr);
}
const LIB_AND_FALLBACK=1;
const LIB_ONLY=2;
const ITOP_ALGO=3;
public function ParseAndEvaluateExpression(string $sExpr, int $iMode=self::LIB_AND_FALLBACK) : mixed
public function ParseAndEvaluateExpression(string $sExpr) : mixed
{
$sPhpContent = <<<PHP
<?php
@@ -109,7 +115,7 @@ PHP;
try{
$aNodes = ModuleFileParser::GetInstance()->ParsePhpCode($sPhpContent);
$oExpr = $aNodes[0];
return $this->EvaluateExpression($oExpr->expr, $iMode);
return $this->EvaluateExpression($oExpr->expr);
} catch (\Throwable $t) {
throw new ModuleFileReaderException("Eval of '$sExpr' caused an error:".$t->getMessage());
}

View File

@@ -9,19 +9,20 @@ class VariableEvaluator extends AbstractExprEvaluator {
public function GetHandledExpressionType(): ?string {
return Variable::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var Variable $oExpr */
if (is_null($oExpr->name)){
return null;
$sName = $oExpr->name;
if (array_key_exists($sName, get_defined_vars())) {
return $$sName;
}
if (! isset($oExpr->name)) {
return null;
if (array_key_exists($sName, $GLOBALS)) {
global $$sName;
return $$sName;
}
$sVarname=$oExpr->name;
global $$sVarname;
return $$sVarname;
return null;
}
}