Skip to content

Commit

Permalink
Dev: Factor out getMismatchInformation()
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Aug 22, 2017
1 parent 9b8ce14 commit 01b9c15
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions application/helpers/expressions/em_core_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,39 @@ public function RDP_GetErrors()
return $this->RDP_errs;
}

/**
* Get informatin about type mismatch between arguments.
* @param array $arg1
* @param array $arg2
* @return array Like (boolean $bMismatchType, boolean $bBothNumeric, boolean $bBothString)
*/
private function getMismatchInformation(array $arg1, array $arg2)
{
/* When value come from DB : it's set to 1.000000 (DECIMAL) : must be fixed see #11163. Response::model() must fix this . or not ? */
/* Don't return true always : user can entre non numeric value in a numeric value : we must compare as string then */
$arg1[0]=($arg1[2]=="NUMBER" && strpos($arg1[0], ".")) ? rtrim(rtrim($arg1[0], "0"), ".") : $arg1[0];
$arg2[0]=($arg2[2]=="NUMBER" && strpos($arg2[0], ".")) ? rtrim(rtrim($arg2[0], "0"), ".") : $arg2[0];
$bNumericArg1 = !$arg1[0] || strval(floatval($arg1[0]))==strval($arg1[0]);
$bNumericArg2 = !$arg2[0] || strval(floatval($arg2[0]))==strval($arg2[0]);

$bStringArg1 = !$arg1[0] || !$bNumericArg1;
$bStringArg2 = !$arg2[0] || !$bNumericArg2;

$bBothNumeric = ($bNumericArg1 && $bNumericArg2);
$bBothString = ($bStringArg1 && $bStringArg2);
$bMismatchType = (!$bBothNumeric && !$bBothString);

return array($bMismatchType, $bBothNumeric, $bBothString);
}

/**
* RDP_EvaluateBinary() computes binary expressions, such as (a or b), (c * d), popping the top two entries off the
* stack and pushing the result back onto the stack.
*
* @param array $token
* @return boolean - false if there is any error, else true
*/
public function RDP_EvaluateBinary(array $token)
public function RDP_EvaluateBinary(array $token)
{
if (count($this->RDP_stack) < 2)
{
Expand All @@ -292,19 +317,8 @@ public function RDP_EvaluateBinary(array $token)
$this->RDP_AddError(self::gT("Invalid value(s) on the stack"), $token);
return false;
}
/* When value come from DB : it's set to 1.000000 (DECIMAL) : must be fixed see #11163. Response::model() must fix this . or not ? */
/* Don't return true always : user can entre non numeric value in a numeric value : we must compare as string then */
$arg1[0]=($arg1[2]=="NUMBER" && strpos($arg1[0],".")) ? rtrim(rtrim($arg1[0],"0"),".") : $arg1[0];
$arg2[0]=($arg2[2]=="NUMBER" && strpos($arg2[0],".")) ? rtrim(rtrim($arg2[0],"0"),".") : $arg2[0];
$bNumericArg1 = !$arg1[0] || strval(floatval($arg1[0]))==strval($arg1[0]);
$bNumericArg2 = !$arg2[0] || strval(floatval($arg2[0]))==strval($arg2[0]);

$bStringArg1 = !$arg1[0] || !$bNumericArg1;
$bStringArg2 = !$arg2[0] || !$bNumericArg2;

$bBothNumeric = ($bNumericArg1 && $bNumericArg2);
$bBothString = ($bStringArg1 && $bStringArg2);
$bMismatchType=(!$bBothNumeric && !$bBothString);
list($bMismatchType, $bBothNumeric, $bBothString) = $this->getMismatchInformation($arg1, $arg2);

// Set bBothString if one is forced to be string, only if both can be numeric. Mimic JS and PHP
// Not sure if needed to test if [2] is set. : TODO review
Expand Down

0 comments on commit 01b9c15

Please sign in to comment.