From fbb68ad68973f2e21c76cd7df52cbc49d27ca366 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Apr 2026 19:32:57 +0800 Subject: [PATCH] feat(constants): add constant definition validation and test coverage - Add isValidDefineName method to validate constant names according to PHP rules - Implement validation check in define function call processing - Add fatal error handling for invalid constant names - Create comprehensive test case covering constant definition and usage scenarios --- src/Php/FuncCallOptimizer.php | 13 ++++++++++++- tests/zend/constants/constants_001.phpt | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/zend/constants/constants_001.phpt diff --git a/src/Php/FuncCallOptimizer.php b/src/Php/FuncCallOptimizer.php index 6e4d4710..cf66b2a8 100644 --- a/src/Php/FuncCallOptimizer.php +++ b/src/Php/FuncCallOptimizer.php @@ -50,6 +50,11 @@ trait FuncCallOptimizer } } + protected function isValidDefineName(string $name): bool + { + return preg_match('/^(?!\d)[\p{L}_][\p{L}\p{N}_]*$/u', $name) === 1; + } + protected function parseFuncCallWithOptimizer(string $name, Node\Expr\FuncCall $expr): string|false { $getArg = function ($i) use ($expr) { @@ -73,8 +78,14 @@ trait FuncCallOptimizer case 'objval': $arg1 = $expr->args[0]->value; $arg2 = $expr->args[1]->value; - return $this->convertObjectExpr($this->parseExpr($arg1), $this->parseExpr($arg2)); + + case 'define': + $arg1 = $expr->args[0]->value; + if ($this->isScalarString($arg1) and !$this->isValidDefineName($arg1->value)) { + $this->fatalError($expr, 'Invalid define name `' . $arg1->value . '`'); + } + break; default: break; } diff --git a/tests/zend/constants/constants_001.phpt b/tests/zend/constants/constants_001.phpt new file mode 100644 index 00000000..b9cec549 --- /dev/null +++ b/tests/zend/constants/constants_001.phpt @@ -0,0 +1,15 @@ +--TEST-- +Defining and using constants +--FILE-- + +--EXPECTF-- +int(2) +int(3)