Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw is an expr instead of stmt #98

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/PHPCfg/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,6 @@ protected function parseStmt_Switch(Stmt\Switch_ $node)
$this->block = $endBlock;
}

protected function parseStmt_Throw(Stmt\Throw_ $node)
{
$this->block->children[] = new Op\Terminal\Throw_(
$this->readVariable($this->parseExprNode($node->expr)),
$this->mapAttributes($node),
);
$this->block = new Block(); // dead code
$this->block->dead = true;
}

protected function parseStmt_Trait(Stmt\Trait_ $node)
{
$name = $this->parseExprNode($node->namespacedName);
Expand Down Expand Up @@ -1145,6 +1135,20 @@ protected function parseExpr_Eval(Expr\Eval_ $expr)
return new Op\Expr\Eval_($this->readVariable($this->parseExprNode($expr->expr)), $this->mapAttributes($expr));
}

protected function parseExpr_Throw(Expr\Throw_ $expr)
{
$this->block->children[] = new Op\Terminal\Throw_(
$this->readVariable($this->parseExprNode($expr->expr)),
$this->mapAttributes($expr)
);

// Dump everything after the throw
$this->block = new Block();
$this->block->dead = true;

return new Literal(1);
}

protected function parseExpr_Exit(Expr\Exit_ $expr)
{
$e = null;
Expand Down
11 changes: 11 additions & 0 deletions test/code/throw.test
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also test the case where it's used as an expression? Something like $x or throw Exception()?

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

throw new \InvalidArgumentException("foo");
-----
Block#1
Expr_New
class: LITERAL('InvalidArgumentException')
args[0]: LITERAL('foo')
result: Var#1
Terminal_Throw
expr: Var#1
Loading