Skip to content

Commit 5f97b12

Browse files
committed
Introduce explicit Finally node
1 parent 1dea911 commit 5f97b12

File tree

10 files changed

+63
-27
lines changed

10 files changed

+63
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ Additionally the following changes were made:
3434
* The `type` subnode on `Class`, `ClassMethod` and `Property` has been renamed to `flags`. The
3535
`type` subnode has retained for backwards compatibility and is populated to the same value as
3636
`flags`. However, writes to `type` will not update `flags`.
37+
* The `TryCatch::$finallyStmts` subnode has been replaced with a `$finally` subnode that holds an
38+
explicit `Finally` node. This allows for more accurate attribute assignment.
3739
* The `Trait` constructor now has the same form as the `Class` and `Interface` constructors: It
3840
takes an array of subnodes. Unlike classes/interfaces, traits can only have a `stmts` subnode.
41+
* The `NodeDumper` now prints class/method/property/constant modifiers, as well as the include and
42+
use type in a textual representation, instead of only showing the number.
3943

4044
### Removed
4145

grammar/php5.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ catch:
206206

207207
optional_finally:
208208
/* empty */ { $$ = null; }
209-
| T_FINALLY '{' inner_statement_list '}' { $$ = $3; }
209+
| T_FINALLY '{' inner_statement_list '}' { $$ = Stmt\Finally_[$3]; }
210210
;
211211

212212
variables_list:

grammar/php7.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ catch:
207207

208208
optional_finally:
209209
/* empty */ { $$ = null; }
210-
| T_FINALLY '{' inner_statement_list '}' { $$ = $3; }
210+
| T_FINALLY '{' inner_statement_list '}' { $$ = Stmt\Finally_[$3]; }
211211
;
212212

213213
variables_list:

lib/PhpParser/Node/Stmt/Finally_.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace PhpParser\Node\Stmt;
4+
5+
use PhpParser\Node;
6+
7+
class Finally_ extends Node\Stmt
8+
{
9+
/** @var Node[] Statements */
10+
public $stmts;
11+
12+
/**
13+
* Constructs a finally node.
14+
*
15+
* @param Node[] $stmts Statements
16+
* @param array $attributes Additional attributes
17+
*/
18+
public function __construct(array $stmts = array(), array $attributes = array()) {
19+
parent::__construct($attributes);
20+
$this->stmts = $stmts;
21+
}
22+
23+
public function getSubNodeNames() {
24+
return array('stmts');
25+
}
26+
}

lib/PhpParser/Node/Stmt/TryCatch.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ class TryCatch extends Node\Stmt
1111
public $stmts;
1212
/** @var Catch_[] Catches */
1313
public $catches;
14-
/** @var null|Node[] Finally statements */
15-
public $finallyStmts;
14+
/** @var null|Finally_ Optional finally node */
15+
public $finally;
1616

1717
/**
1818
* Constructs a try catch node.
1919
*
20-
* @param Node[] $stmts Statements
21-
* @param Catch_[] $catches Catches
22-
* @param null|Node[] $finallyStmts Finally statements (null means no finally clause)
23-
* @param array|null $attributes Additional attributes
20+
* @param Node[] $stmts Statements
21+
* @param Catch_[] $catches Catches
22+
* @param null|Finally_ $finally Optionaly finally node
23+
* @param array|null $attributes Additional attributes
2424
*/
25-
public function __construct(array $stmts, array $catches, array $finallyStmts = null, array $attributes = array()) {
26-
if (empty($catches) && null === $finallyStmts) {
25+
public function __construct(array $stmts, array $catches, Finally_ $finally = null, array $attributes = array()) {
26+
if (empty($catches) && null === $finally) {
2727
throw new Error('Cannot use try without catch or finally');
2828
}
2929

3030
parent::__construct($attributes);
3131
$this->stmts = $stmts;
3232
$this->catches = $catches;
33-
$this->finallyStmts = $finallyStmts;
33+
$this->finally = $finally;
3434
}
3535

3636
public function getSubNodeNames() {
37-
return array('stmts', 'catches', 'finallyStmts');
37+
return array('stmts', 'catches', 'finally');
3838
}
3939
}

lib/PhpParser/Parser/Php5.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ protected function reduceRule161() {
15611561
}
15621562

15631563
protected function reduceRule162() {
1564-
$this->semValue = $this->semStack[$this->stackPos-(4-3)];
1564+
$this->semValue = new Stmt\Finally_($this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes);
15651565
}
15661566

15671567
protected function reduceRule163() {

lib/PhpParser/Parser/Php7.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ protected function reduceRule159() {
14381438
}
14391439

14401440
protected function reduceRule160() {
1441-
$this->semValue = $this->semStack[$this->stackPos-(4-3)];
1441+
$this->semValue = new Stmt\Finally_($this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes);
14421442
}
14431443

14441444
protected function reduceRule161() {

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,16 +729,18 @@ public function pStmt_Case(Stmt\Case_ $node) {
729729
public function pStmt_TryCatch(Stmt\TryCatch $node) {
730730
return 'try {' . $this->pStmts($node->stmts) . "\n" . '}'
731731
. $this->pImplode($node->catches)
732-
. ($node->finallyStmts !== null
733-
? ' finally {' . $this->pStmts($node->finallyStmts) . "\n" . '}'
734-
: '');
732+
. ($node->finally !== null ? $this->p($node->finally) : '');
735733
}
736734

737735
public function pStmt_Catch(Stmt\Catch_ $node) {
738736
return ' catch (' . $this->pImplode($node->types, '|') . ' $' . $node->var . ') {'
739737
. $this->pStmts($node->stmts) . "\n" . '}';
740738
}
741739

740+
public function pStmt_Finally(Stmt\Finally_ $node) {
741+
return ' finally {' . $this->pStmts($node->stmts) . "\n" . '}';
742+
}
743+
742744
public function pStmt_Break(Stmt\Break_ $node) {
743745
return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
744746
}

test/code/parser/stmt/multiCatch.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ array(
6060
)
6161
)
6262
)
63-
finallyStmts: null
63+
finally: null
6464
)
6565
)

test/code/parser/stmt/tryCatch.test

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ array(
7878
)
7979
)
8080
)
81-
finallyStmts: array(
82-
0: Expr_FuncCall(
83-
name: Name(
84-
parts: array(
85-
0: doFinally
81+
finally: Stmt_Finally(
82+
stmts: array(
83+
0: Expr_FuncCall(
84+
name: Name(
85+
parts: array(
86+
0: doFinally
87+
)
88+
)
89+
args: array(
8690
)
87-
)
88-
args: array(
8991
)
9092
)
9193
)
@@ -107,7 +109,7 @@ array(
107109
)
108110
)
109111
)
110-
finallyStmts: null
112+
finally: null
111113
comments: array(
112114
0: // no finally
113115
)
@@ -117,7 +119,9 @@ array(
117119
)
118120
catches: array(
119121
)
120-
finallyStmts: array(
122+
finally: Stmt_Finally(
123+
stmts: array(
124+
)
121125
)
122126
comments: array(
123127
0: // no catch

0 commit comments

Comments
 (0)