phpcallbackdeprecatedphp-7.2

Need help to update a small class using deprecated create_function() within a callback function


I need help to update a class using deprecated create_function(). Can someone understand the construction in the content of my class to replace it with 7.2 version?

Class explained: This class is working with nestled data characters in string like "[(((2 * 4) + 6))]" to rip out the proper math calculations. The example is of course more complex than that -in that matter- I do not understand the class by myself... It just working as expected!

The main problem (private function) is here:

private function compute($input){
    $compute = create_function('', 'return '.$input.';');
    return 0 + $compute();
}

And it is called 2 times as:

    //  Calculate the result
    if(preg_match(self::PATTERN, $input, $match)){
        return $this->compute($match[0]);
    }

Here is the complete class:

class Field_calculate {

    const PATTERN = '/(?:\-?\d+(?:\.?\d+)?[\+\-\*\/])+\-?\d+(?:\.?\d+)?/';
    const PARENTHESIS_DEPTH = 10;

    public function calculate($input){
        if(strpos($input, '+') != null || strpos($input, '-') != null || strpos($input, '/') != null || strpos($input, '*') != null){
            //  Remove white spaces and invalid math chars
            $original = $input ;
            //$GLOBALS['A_INFO'] .= "Clean input : ".$original." = ".$input."<br/>";
            
            // Validate parenthesis
            $nestled = $this->analyse($input);
            if(!$nestled){
                $GLOBALS['A_INFO'] .= "_x_Parentheses in ".$original." NOT NESTLED CORRECTLY<br/>";
            }
            $input = str_replace(',', '.', $input);
            $input = preg_replace('/[^0-9\.\+\-\*\/\(\)]/', '', $input);
            if($input[0] == '(' && $input[strlen($input) - 1] == ')') {
                $input = substr($input, 1, -1);
            }

            //  Calculate each of the parenthesis from the top
            $i = 0;
            while(strpos($input, '(') || strpos($input, ')')){
                $input = preg_replace_callback('/\(([^\(\)]+)\)/', 'self::callback', $input);

                $i++;
                if($i > self::PARENTHESIS_DEPTH){
                    break;
                }
            }

            //  Calculate the result
            if(preg_match(self::PATTERN, $input, $match)){
                return $this->compute($match[0]);
            }

            return 0;
        }

        return $input;
    }
    
    private function analyse($input){
    
        $depth = 0;
        for ($i = 0; $i < strlen($input); $i++) {
            $depth += $input[$i] == '(';
            $depth -= $input[$i] == ')';
            if ($depth < 0) break;
        }
        if ($depth != 0) return false;
            else return true;
    }

    private function compute($input){
        $compute = create_function('', 'return '.$input.';');
        return 0 + $compute();
    }

    private function callback($input){
        if(is_numeric($input[1])){
            return $input[1];
        }
        elseif(preg_match(self::PATTERN, $input[1], $match)){
            return $this->compute($match[0]);
        }
        return 0;
    }
}

Thanks for any help.


Solution

  • This class is returning math results from a string. You can find your class and same problems here: calculate math expression from a string using eval

    It first strips out non-math materials, "[(((2 * 4) + 6))]" becomes "((2 * 4) + 6)" and in the end the pure results = 14. Instead of stripping the numeric values out, its using the function, to fake "eval" the string as php-code. Since mixing variables is removed by php v.7.2, this workaround is not valid.

    This is done by adding numeric variable to a funtion calculation results by:

    return 0 + $compute();

    This could be done as:

    $value = eval("return ($input);");
    return $value;
    

    or complete:

    private function compute($input){
        $returnvalue = eval("return ($input);");
        return $returnvalue;
    }
    

    This fixes yout php 7.2 error. But usin eval is not recommended. There are some other classes out there doing this for you. Try to replace your class with a small math library instead.