langleyfoxall/math_eval
Safely evaluate math expressions in PHP. langleyfoxall/math_eval parses and computes strings with operators, brackets, and common functions, ideal for user-defined formulas and configuration values without using eval(). Lightweight and easy to integrate in Laravel or any PHP app.
sin(), log()), and variable substitution aligns with Laravel’s need for flexible yet controlled logic in business applications.eval()-style vulnerabilities.Math::evaluate()).Validator can restrict input to whitelisted operators/functions.mossadal/math-parser internally, but no composer conflicts).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Stale Codebase | Last release in 2019; no active maintenance. | Fork the repository to backport security fixes and add Laravel-specific tests. |
| Security | Claims sandboxed evaluation, but edge cases (e.g., nested functions, custom operators) may pose risks. | Whitelist allowed functions (e.g., sin, log, sqrt) and validate input syntax via regex. |
| Performance | Pure PHP implementation may lag for high-volume evaluations (e.g., 10,000+ expressions/sec). | Benchmark against alternatives (e.g., symfony/math) and cache results (Redis). |
| Laravel Ecosystem | No native Laravel integrations (e.g., no Eloquent query builder support). | Wrap in a service class with Laravel-specific methods (e.g., Math::evaluateForUserInput()). |
| Testing | Limited test coverage; regression risk for edge cases (e.g., floating-point precision). | Add unit tests for critical expressions and integration tests with Laravel’s request pipeline. |
| PHP 8.x Compatibility | No explicit PHP 8.x support, but no breaking changes expected. | Test with PHP 8.0+ and type-declare inputs (e.g., math_eval(string $expr, array $vars): float). |
Why not alternatives?
symfony/math: More features (e.g., matrices) but heavier.league/math: Better for financial calculations.php-ai/php-math: Supports symbolic math but complex.math_eval is simpler and lighter, but lacks advanced features.Input Validation Strategy
/^[0-9+\-*\/().\s]+$/).['sin', 'log', 'sqrt']).Performance Requirements
math.js).php-math-parser if maintained).Long-Term Maintenance
symfony/math or league/math.Error Handling
null or throw a custom exception (e.g., InvalidMathExpression).Laravel-Specific Edge Cases
Proof of Concept (PoC)
use MathEval\Evaluator;
$evaluator = new Evaluator();
$result = $evaluator->evaluate('2 + 2 * (3 - 1)'); // Should return 8
"__destruct()").Package Setup
composer.json:
"require": {
"langleyfoxall/math_eval": "^2.0"
}
composer update.Service Provider
config/app.php or a custom provider:
// app/Providers/MathServiceProvider.php
public function register()
{
$this->app->singleton('math', function () {
return new \MathEval\Evaluator();
});
}
Facade (Optional)
// app/Facades/Math.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Math extends Facade { protected static function getFacadeAccessor() { return 'math'; } }
$result = Math::evaluate('2 + 2'); // Returns 4
Request Integration
public function calculate(Request $request)
{
$expression = $request->input('expression');
$result = Math::evaluate($expression);
return response()->json(['result' => $result]);
}
public function rules()
{
return ['expression' => 'required|safe_math_expression'];
}
bcmath/gmp improve performance for large numbers.price_formula column in a product table).Math::evaluate($expr) logs $expr and $result).2
How can I help you explore Laravel packages today?