Skip to content

Commit 0512478

Browse files
Prevent void param type (#1142)
PHP 7.1 introduced the `void` type. Consequently, it is no longer allowed to specify `void` as the parameter type: ```php <?php function foo(void $bar) {} ``` * This pull request adds detection for this behavior and rejects functions that have the _identifier_ `void` as parameter type. * Pre-7.1, it was allowed to create a class `class void {}` and use that as a parameter type. In this case, the parameter's type is the _name_ `void`, so the error isn't thrown. * Two test cases are added to verify the above behavior. Fixes #1137.
1 parent 8c360e2 commit 0512478

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

lib/PhpParser/ParserAbstract.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,13 @@ protected function checkParam(Param $node): void {
10781078
$node->default->getAttributes()
10791079
));
10801080
}
1081+
1082+
if ($node->type instanceof Identifier && $node->type->name === 'void') {
1083+
$this->emitError(new Error(
1084+
'void cannot be used as a parameter type',
1085+
$node->type->getAttributes()
1086+
));
1087+
}
10811088
}
10821089

10831090
protected function checkTryCatch(TryCatch $node): void {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Invalid parameter with type void
2+
-----
3+
<?php
4+
function foo(void $foo) {}
5+
-----
6+
void cannot be used as a parameter type from 2:14 to 2:17
7+
array(
8+
0: Stmt_Function(
9+
attrGroups: array(
10+
)
11+
byRef: false
12+
name: Identifier(
13+
name: foo
14+
)
15+
params: array(
16+
0: Param(
17+
attrGroups: array(
18+
)
19+
flags: 0
20+
type: Identifier(
21+
name: void
22+
)
23+
byRef: false
24+
variadic: false
25+
var: Expr_Variable(
26+
name: foo
27+
)
28+
default: null
29+
hooks: array(
30+
)
31+
)
32+
)
33+
returnType: null
34+
stmts: array(
35+
)
36+
)
37+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Valid parameter with type void
2+
-----
3+
<?php
4+
function foo(void $foo) {}
5+
-----
6+
!!version=7.0
7+
array(
8+
0: Stmt_Function(
9+
attrGroups: array(
10+
)
11+
byRef: false
12+
name: Identifier(
13+
name: foo
14+
)
15+
params: array(
16+
0: Param(
17+
attrGroups: array(
18+
)
19+
flags: 0
20+
type: Name(
21+
name: void
22+
)
23+
byRef: false
24+
variadic: false
25+
var: Expr_Variable(
26+
name: foo
27+
)
28+
default: null
29+
hooks: array(
30+
)
31+
)
32+
)
33+
returnType: null
34+
stmts: array(
35+
)
36+
)
37+
)

0 commit comments

Comments
 (0)