Skip to content

Commit

Permalink
Dev: Factor out getMismatchInformation()
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt authored and LouisGac committed Aug 28, 2017
1 parent 127e99a commit cd7f13c
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)

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Jun 13, 2019

Author Collaborator

@Shnoulle By the way, this is a very easy function to write tests for, since it doesn't have any side-effects (doesn't read or write to session/database/request/etc).

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Jun 13, 2019

Collaborator

Need cheking of 'forced string' inside this one :)

I muts check exactly, in JS

  1. 5+'' < 20+'' : false (compare as string)
  2. 5 < 20+'' : true : compare as number
  3. '3A' < 5+'' : true : seems to compare as string (false currently in PHP : forcedString are not tested)
  4. '3A' < 5 : must check … (i test, but i don't remind)
  5. 0 < 5+'' : true, false in PHP (if i remind)

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Jun 13, 2019

Author Collaborator

Ah, sorry.

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Jun 13, 2019

Collaborator

I make some mantis related to old one :)

{
/* 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 cd7f13c

Please sign in to comment.