Skip to content

Commit bbd6f63

Browse files
committed
The double arrow inside arrow functions is now tokenized as T_FN_ARROW (ref #2523)
The reduces a lot of confusion with double arrows used in arrays.
1 parent 200eae5 commit bbd6f63

File tree

8 files changed

+38
-2
lines changed

8 files changed

+38
-2
lines changed

package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
2828
<notes>
2929
- The PHP 7.4 T_FN token has been made available for older versions
3030
-- T_FN represents the fn string used for arrow functions
31-
-- The double arrow becomes the scope opener
31+
-- The double arrow becomes the scope opener, and uses a new T_FN_ARROW token type
3232
-- The token after the statement (normally a semicolon) becomes the scope closer
3333
-- The token is also associated with the opening and closing parenthesis of the statement
3434
-- Any functions named "fn" will cause have a T_FN token for the function name, but have no scope information

src/Files/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ public function getMethodProperties($stackPtr)
16541654
}
16551655

16561656
if ($this->tokens[$stackPtr]['code'] === T_FN) {
1657-
$bodyToken = T_DOUBLE_ARROW;
1657+
$bodyToken = T_FN_ARROW;
16581658
} else {
16591659
$bodyToken = T_OPEN_CURLY_BRACKET;
16601660
}

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,14 @@ $foo = array(<<<JSON
410410
JSON
411411
);
412412

413+
$array = array(
414+
'a' => fn() => return 1,
415+
'bb' => fn() => return 2,
416+
'ccc' => ( true ) ?
417+
fn() => return 1 :
418+
fn() => return 2,
419+
);
420+
413421
// Intentional syntax error.
414422
$a = array(
415423
'a' =>

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@ $foo = array(<<<JSON
438438
JSON
439439
);
440440

441+
$array = array(
442+
'a' => fn() => return 1,
443+
'bb' => fn() => return 2,
444+
'ccc' => ( true ) ?
445+
fn() => return 1 :
446+
fn() => return 2,
447+
);
448+
441449
// Intentional syntax error.
442450
$a = array(
443451
'a' =>

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ $foo = [<<<JSON
399399
JSON
400400
];
401401

402+
$array = [
403+
'a' => fn() => return 1,
404+
'bb' => fn() => return 2,
405+
'ccc' => ( true ) ?
406+
fn() => return 1 :
407+
fn() => return 2,
408+
];
409+
402410
// Intentional syntax error.
403411
$a = [
404412
'a' =>

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,14 @@ $foo = [<<<JSON
425425
JSON
426426
];
427427

428+
$array = [
429+
'a' => fn() => return 1,
430+
'bb' => fn() => return 2,
431+
'ccc' => ( true ) ?
432+
fn() => return 1 :
433+
fn() => return 2,
434+
];
435+
428436
// Intentional syntax error.
429437
$a = [
430438
'a' =>

src/Tokenizers/PHP.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,9 @@ protected function processAdditional()
17041704
$this->tokens[$i]['parenthesis_opener'] = $x;
17051705
$this->tokens[$i]['parenthesis_closer'] = $this->tokens[$x]['parenthesis_closer'];
17061706

1707+
$this->tokens[$arrow]['code'] = T_FN_ARROW;
1708+
$this->tokens[$arrow]['type'] = 'T_FN_ARROW';
1709+
17071710
$this->tokens[$arrow]['scope_condition'] = $i;
17081711
$this->tokens[$arrow]['scope_opener'] = $arrow;
17091712
$this->tokens[$arrow]['scope_closer'] = $scopeCloser;

src/Util/Tokens.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
define('T_CLOSE_USE_GROUP', 'PHPCS_T_CLOSE_USE_GROUP');
7575
define('T_ZSR', 'PHPCS_T_ZSR');
7676
define('T_ZSR_EQUAL', 'PHPCS_T_ZSR_EQUAL');
77+
define('T_FN_ARROW', 'T_FN_ARROW');
7778

7879
// Some PHP 5.5 tokens, replicated for lower versions.
7980
if (defined('T_FINALLY') === false) {

0 commit comments

Comments
 (0)