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
pull/1/head
韩天峰 5 months ago
parent 0cee9a4154
commit fbb68ad689
  1. 13
      src/Php/FuncCallOptimizer.php
  2. 15
      tests/zend/constants/constants_001.phpt

@ -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;
}

@ -0,0 +1,15 @@
--TEST--
Defining and using constants
--FILE--
<?php
define('foo', 2);
define('foo1', 3);
var_dump(constant('foo'));
var_dump(constant('foo1'));
?>
--EXPECTF--
int(2)
int(3)
Loading…
Cancel
Save