Skip to content

Commit 50f0d9c

Browse files
Restore FnTokenEmulator (#1141)
As of PHP 7.4, functions can no longer be named fn, due to the new arrow function syntax. Consequently, pre-7.4, functions may be called fn. This pull request restores a previously present token emulator that made this behavior possible. Fixes #1139.
1 parent 0512478 commit 50f0d9c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lib/PhpParser/Lexer/Emulative.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Lexer\TokenEmulator\AttributeEmulator;
1010
use PhpParser\Lexer\TokenEmulator\EnumTokenEmulator;
1111
use PhpParser\Lexer\TokenEmulator\ExplicitOctalEmulator;
12+
use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
1213
use PhpParser\Lexer\TokenEmulator\MatchTokenEmulator;
1314
use PhpParser\Lexer\TokenEmulator\NullsafeTokenEmulator;
1415
use PhpParser\Lexer\TokenEmulator\PipeOperatorEmulator;
@@ -40,6 +41,7 @@ public function __construct(?PhpVersion $phpVersion = null) {
4041
$this->hostPhpVersion = PhpVersion::getHostVersion();
4142

4243
$emulators = [
44+
new FnTokenEmulator(),
4345
new MatchTokenEmulator(),
4446
new NullsafeTokenEmulator(),
4547
new AttributeEmulator(),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PhpParser\Lexer\TokenEmulator;
4+
5+
use PhpParser\PhpVersion;
6+
7+
// Retained for reverse emulation support only.
8+
final class FnTokenEmulator extends KeywordEmulator {
9+
public function getPhpVersion(): PhpVersion {
10+
return PhpVersion::fromString('7.4');
11+
}
12+
13+
public function getKeywordString(): string {
14+
return 'fn';
15+
}
16+
17+
public function getKeywordToken(): int {
18+
return \T_FN;
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Function with the name fn
2+
-----
3+
<?php
4+
function fn() {}
5+
-----
6+
!!version=7.3
7+
array(
8+
0: Stmt_Function(
9+
attrGroups: array(
10+
)
11+
byRef: false
12+
name: Identifier(
13+
name: fn
14+
)
15+
params: array(
16+
)
17+
returnType: null
18+
stmts: array(
19+
)
20+
)
21+
)

0 commit comments

Comments
 (0)