Skip to content

Commit

Permalink
Remove Stmt\Throw
Browse files Browse the repository at this point in the history
This was a backwards-compatibility shim for Expr\Throw.
  • Loading branch information
nikic committed Sep 25, 2023
1 parent a1ccf57 commit b4183c2
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 69 deletions.
11 changes: 1 addition & 10 deletions grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,7 @@ non_empty_statement:
$$ = Stmt\InlineHTML[$1];
$$->setAttribute('hasLeadingNewline', $this->inlineHtmlHasLeadingNewline(#1));
}
| expr semi {
$e = $1;
if ($e instanceof Expr\Throw_) {
// For backwards-compatibility reasons, convert throw in statement position into
// Stmt\Throw_ rather than Stmt\Expression(Expr\Throw_).
$$ = Stmt\Throw_[$e->expr];
} else {
$$ = Stmt\Expression[$e];
}
}
| expr semi { $$ = Stmt\Expression[$1]; }
| T_UNSET '(' variables_list ')' semi { $$ = Stmt\Unset_[$3]; }
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
{ $$ = Stmt\Foreach_[$3, $5[0], ['keyVar' => null, 'byRef' => $5[1], 'stmts' => $7]]; }
Expand Down
29 changes: 0 additions & 29 deletions lib/PhpParser/Node/Stmt/Throw_.php

This file was deleted.

11 changes: 1 addition & 10 deletions lib/PhpParser/Parser/Php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -1473,16 +1473,7 @@ protected function initReduceCallbacks(): void {

},
172 => function ($stackPos) {

$e = $this->semStack[$stackPos-(2-1)];
if ($e instanceof Expr\Throw_) {
// For backwards-compatibility reasons, convert throw in statement position into
// Stmt\Throw_ rather than Stmt\Expression(Expr\Throw_).
$this->semValue = new Stmt\Throw_($e->expr, $this->getAttributes($this->tokenStartStack[$stackPos-(2-1)], $this->tokenEndStack[$stackPos]));
} else {
$this->semValue = new Stmt\Expression($e, $this->getAttributes($this->tokenStartStack[$stackPos-(2-1)], $this->tokenEndStack[$stackPos]));
}

$this->semValue = new Stmt\Expression($this->semStack[$stackPos-(2-1)], $this->getAttributes($this->tokenStartStack[$stackPos-(2-1)], $this->tokenEndStack[$stackPos]));
},
173 => function ($stackPos) {
$this->semValue = new Stmt\Unset_($this->semStack[$stackPos-(5-3)], $this->getAttributes($this->tokenStartStack[$stackPos-(5-1)], $this->tokenEndStack[$stackPos]));
Expand Down
11 changes: 1 addition & 10 deletions lib/PhpParser/Parser/Php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -1491,16 +1491,7 @@ protected function initReduceCallbacks(): void {

},
172 => function ($stackPos) {

$e = $this->semStack[$stackPos-(2-1)];
if ($e instanceof Expr\Throw_) {
// For backwards-compatibility reasons, convert throw in statement position into
// Stmt\Throw_ rather than Stmt\Expression(Expr\Throw_).
$this->semValue = new Stmt\Throw_($e->expr, $this->getAttributes($this->tokenStartStack[$stackPos-(2-1)], $this->tokenEndStack[$stackPos]));
} else {
$this->semValue = new Stmt\Expression($e, $this->getAttributes($this->tokenStartStack[$stackPos-(2-1)], $this->tokenEndStack[$stackPos]));
}

$this->semValue = new Stmt\Expression($this->semStack[$stackPos-(2-1)], $this->getAttributes($this->tokenStartStack[$stackPos-(2-1)], $this->tokenEndStack[$stackPos]));
},
173 => function ($stackPos) {
$this->semValue = new Stmt\Unset_($this->semStack[$stackPos-(5-3)], $this->getAttributes($this->tokenStartStack[$stackPos-(5-1)], $this->tokenEndStack[$stackPos]));
Expand Down
4 changes: 0 additions & 4 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,6 @@ protected function pStmt_Return(Stmt\Return_ $node): string {
return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
}

protected function pStmt_Throw(Stmt\Throw_ $node): string {
return 'throw ' . $this->p($node->expr) . ';';
}

protected function pStmt_Label(Stmt\Label $node): string {
return $node->name . ':';
}
Expand Down
8 changes: 5 additions & 3 deletions test/code/parser/errorHandling/recovery.test
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,11 @@ array(
)
)
)
12: Stmt_Throw(
expr: Expr_Variable(
name: x
12: Stmt_Expression(
expr: Expr_Throw(
expr: Expr_Variable(
name: x
)
)
)
13: Stmt_Goto(
Expand Down
8 changes: 5 additions & 3 deletions test/code/parser/stmt/controlFlow.test
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ array(
name: a
)
)
6: Stmt_Throw(
expr: Expr_Variable(
name: e
6: Stmt_Expression(
expr: Expr_Throw(
expr: Expr_Variable(
name: e
)
)
)
7: Stmt_Label(
Expand Down

0 comments on commit b4183c2

Please sign in to comment.