Skip to content

Commit 4022659

Browse files
ondrejmirtesclaude
andcommitted
Word the native NodeTraverser's LogicExceptions exactly like php-parser
The invalid-return messages named the returned type through zend_zval_value_name() ("int", "float", "true", the class name) where php-parser uses gettype() ("integer", "double", "boolean", "object"), and the unreasonable-replacement messages left out the node types php-parser interpolates ("Trying to replace statement (Stmt_Echo) with expression (Expr_Variable)..."). Use zend_zval_get_legacy_type() and call getType() on both nodes. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 7d93ab4 commit 4022659

2 files changed

Lines changed: 103 additions & 18 deletions

File tree

‎turbo-ext/src/NodeTraverser.cpp‎

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,24 @@ static void pt_do_nodes_free(pt_do_nodes *dn)
9393
}
9494
}
9595

96-
static void pt_trav_throw_logic(const char *format, const char *arg)
96+
static void pt_trav_throw_logic(const char *message)
9797
{
9898
zend_string *name = zend_string_init("LogicException", sizeof("LogicException") - 1, 0);
9999
zend_class_entry *ce = zend_lookup_class(name);
100100
zend_string_release(name);
101101
if (ce == NULL) {
102-
zend_throw_error(NULL, format, arg);
102+
zend_throw_error(NULL, "%s", message);
103103
return;
104104
}
105-
zend_throw_exception_ex(ce, 0, format, arg);
105+
zend_throw_exception(ce, message, 0);
106+
}
107+
108+
/* "... returned invalid value of type " . gettype($return) */
109+
static void pt_trav_throw_invalid_return(const char *hook, zval *value)
110+
{
111+
zend_string *message = zend_strpprintf(0, "%s() returned invalid value of type %s", hook, ZSTR_VAL(zend_zval_get_legacy_type(value)));
112+
pt_trav_throw_logic(ZSTR_VAL(message));
113+
zend_string_release(message);
106114
}
107115

108116
/* Node subnode info incl. names, resolved lazily per class. */
@@ -436,7 +444,7 @@ class NodeTraverser
436444
break;
437445
}
438446
}
439-
pt_trav_throw_logic("enterNode() returned invalid value of type %s", zend_zval_value_name(retRef.raw()));
447+
pt_trav_throw_invalid_return("enterNode", retRef.raw());
440448
failed = true;
441449
return;
442450
}
@@ -490,11 +498,11 @@ class NodeTraverser
490498
}
491499
}
492500
if (retRef.isArray()) {
493-
pt_trav_throw_logic("leaveNode() may only return an array if the parent structure is an array%s", "");
501+
pt_trav_throw_logic("leaveNode() may only return an array if the parent structure is an array");
494502
failed = true;
495503
return;
496504
}
497-
pt_trav_throw_logic("leaveNode() returned invalid value of type %s", zend_zval_value_name(retRef.raw()));
505+
pt_trav_throw_invalid_return("leaveNode", retRef.raw());
498506
failed = true;
499507
return;
500508
}
@@ -552,7 +560,7 @@ class NodeTraverser
552560

553561
if (!value.instanceOf(nodeIface)) {
554562
if (UNEXPECTED(value.isArray())) {
555-
pt_trav_throw_logic("Invalid node structure: Contains nested arrays%s", "");
563+
pt_trav_throw_logic("Invalid node structure: Contains nested arrays");
556564
failed = true;
557565
break;
558566
}
@@ -620,12 +628,12 @@ class NodeTraverser
620628
break;
621629
}
622630
if (code == REPLACE_WITH_NULL) {
623-
pt_trav_throw_logic("REPLACE_WITH_NULL can not be used if the parent structure is an array%s", "");
631+
pt_trav_throw_logic("REPLACE_WITH_NULL can not be used if the parent structure is an array");
624632
failed = true;
625633
break;
626634
}
627635
}
628-
pt_trav_throw_logic("enterNode() returned invalid value of type %s", zend_zval_value_name(retRef.raw()));
636+
pt_trav_throw_invalid_return("enterNode", retRef.raw());
629637
failed = true;
630638
break;
631639
}
@@ -682,12 +690,12 @@ class NodeTraverser
682690
break;
683691
}
684692
if (code == REPLACE_WITH_NULL) {
685-
pt_trav_throw_logic("REPLACE_WITH_NULL can not be used if the parent structure is an array%s", "");
693+
pt_trav_throw_logic("REPLACE_WITH_NULL can not be used if the parent structure is an array");
686694
failed = true;
687695
break;
688696
}
689697
}
690-
pt_trav_throw_logic("leaveNode() returned invalid value of type %s", zend_zval_value_name(retRef.raw()));
698+
pt_trav_throw_invalid_return("leaveNode", retRef.raw());
691699
failed = true;
692700
break;
693701
}
@@ -797,15 +805,42 @@ class NodeTraverser
797805

798806
zv::ObjRef oldRef(oldNode);
799807
zv::ObjRef newRef(newNode);
800-
if (oldRef.instanceOf(stmtCe) && newRef.instanceOf(exprCe)) {
801-
pt_trav_throw_logic("Trying to replace statement with expression. Are you missing a Stmt_Expression wrapper?%s", "");
802-
return false;
808+
bool statementWithExpression = oldRef.instanceOf(stmtCe) && newRef.instanceOf(exprCe);
809+
if (!statementWithExpression && !(oldRef.instanceOf(exprCe) && newRef.instanceOf(stmtCe))) return true;
810+
811+
/* "... ({$old->getType()}) ... ({$new->getType()})" */
812+
zv::Val oldType = nodeType(oldNode);
813+
if (UNEXPECTED(oldType.isUndef())) return false;
814+
zv::Val newType = nodeType(newNode);
815+
if (UNEXPECTED(newType.isUndef())) return false;
816+
zend_string *message = statementWithExpression
817+
? zend_strpprintf(0, "Trying to replace statement (%s) with expression (%s). Are you missing a Stmt_Expression wrapper?", Z_STRVAL_P(oldType.raw()), Z_STRVAL_P(newType.raw()))
818+
: zend_strpprintf(0, "Trying to replace expression (%s) with statement (%s)", Z_STRVAL_P(oldType.raw()), Z_STRVAL_P(newType.raw()));
819+
pt_trav_throw_logic(ZSTR_VAL(message));
820+
zend_string_release(message);
821+
return false;
822+
}
823+
824+
/* $node->getType() as a string; UNDEF means a pending exception */
825+
static zv::Val nodeType(zend_object *node)
826+
{
827+
zval type;
828+
ZVAL_UNDEF(&type);
829+
zend_call_method_with_0_params(node, node->ce, NULL, "gettype", &type);
830+
if (UNEXPECTED(EG(exception))) {
831+
zval_ptr_dtor(&type);
832+
return zv::Val();
803833
}
804-
if (oldRef.instanceOf(exprCe) && newRef.instanceOf(stmtCe)) {
805-
pt_trav_throw_logic("Trying to replace expression with statement%s", "");
806-
return false;
834+
zv::Val owned = zv::Val::adopt(type);
835+
if (Z_TYPE_P(owned.raw()) != IS_STRING) {
836+
zend_string *string = zval_get_string(owned.raw());
837+
if (UNEXPECTED(EG(exception))) {
838+
zend_string_release(string);
839+
return zv::Val();
840+
}
841+
return zv::Val::adoptString(string);
807842
}
808-
return true;
843+
return owned;
809844
}
810845

811846
/* Builds the per-visitor hook plan from $this->visitors; false on error. */

‎turbo-ext/tests/parser-visitors.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,53 @@ public function leaveNode(\PhpParser\Node $node)
378378
return $ntShape($function->stmts);
379379
});
380380
}
381+
382+
// the LogicException messages: gettype() names, and the node types of an
383+
// unreasonable replacement
384+
$ntReturning = static fn (string $hook, callable $decide) => new class ($hook, $decide) extends \PhpParser\NodeVisitorAbstract {
385+
386+
public function __construct(private string $hook, private $decide)
387+
{
388+
}
389+
390+
public function enterNode(\PhpParser\Node $node)
391+
{
392+
return $this->hook === 'enter' ? ($this->decide)($node) : null;
393+
}
394+
395+
public function leaveNode(\PhpParser\Node $node)
396+
{
397+
return $this->hook === 'leave' ? ($this->decide)($node) : null;
398+
}
399+
400+
};
401+
$ntInvalidReturns = [
402+
'int' => 42,
403+
'float' => 1.5,
404+
'string' => 'x',
405+
'true' => true,
406+
'false' => false,
407+
'object' => new \stdClass(),
408+
];
409+
foreach (['enter', 'leave'] as $ntHook) {
410+
foreach ($ntInvalidReturns as $ntLabel => $ntValue) {
411+
foreach (['Stmt_Echo' => 'in an array', 'Scalar_Int' => 'in a subnode'] as $ntTarget => $ntWhere) {
412+
$ntCompare("$ntHook returning $ntLabel $ntWhere", static function (string $side, string $traverserClass) use ($smokeParser, $ntReturning, $ntHook, $ntValue, $ntTarget): array {
413+
$visitor = $ntReturning($ntHook, static fn ($node) => $node->getType() === $ntTarget ? $ntValue : null);
414+
return (new $traverserClass($visitor))->traverse($smokeParser->parse('<?php echo 1;'));
415+
});
416+
}
417+
}
418+
$ntCompare("$ntHook replacing a statement with an expression", static function (string $side, string $traverserClass) use ($smokeParser, $ntReturning, $ntHook): array {
419+
$visitor = $ntReturning($ntHook, static fn ($node) => $node instanceof \PhpParser\Node\Stmt\Echo_ ? new \PhpParser\Node\Expr\Variable('x') : null);
420+
return (new $traverserClass($visitor))->traverse($smokeParser->parse('<?php echo 1;'));
421+
});
422+
$ntCompare("$ntHook replacing an expression with a statement", static function (string $side, string $traverserClass) use ($smokeParser, $ntReturning, $ntHook): array {
423+
$visitor = $ntReturning($ntHook, static fn ($node) => $node instanceof \PhpParser\Node\Expr\Variable ? new \PhpParser\Node\Stmt\Nop() : null);
424+
return (new $traverserClass($visitor))->traverse($smokeParser->parse('<?php $a;'));
425+
});
426+
$ntCompare("$ntHook replacing an expression with a statement in an array", static function (string $side, string $traverserClass) use ($ntReturning, $ntHook): array {
427+
$visitor = $ntReturning($ntHook, static fn ($node) => $node instanceof \PhpParser\Node\Expr\Variable ? new \PhpParser\Node\Stmt\Nop() : null);
428+
return (new $traverserClass($visitor))->traverse([new \PhpParser\Node\Expr\Variable('a')]);
429+
});
430+
}

0 commit comments

Comments
 (0)