Skip to content

Commit

Permalink
The double arrow inside arrow functions is now tokenized as T_FN_ARRO…
Browse files Browse the repository at this point in the history
…W (ref #2523)

The reduces a lot of confusion with double arrows used in arrays.
  • Loading branch information
gsherwood committed Nov 8, 2019
1 parent 200eae5 commit bbd6f63
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<notes>
- The PHP 7.4 T_FN token has been made available for older versions
-- T_FN represents the fn string used for arrow functions
-- The double arrow becomes the scope opener
-- The double arrow becomes the scope opener, and uses a new T_FN_ARROW token type
-- The token after the statement (normally a semicolon) becomes the scope closer
-- The token is also associated with the opening and closing parenthesis of the statement
-- Any functions named "fn" will cause have a T_FN token for the function name, but have no scope information
Expand Down
2 changes: 1 addition & 1 deletion src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ public function getMethodProperties($stackPtr)
}

if ($this->tokens[$stackPtr]['code'] === T_FN) {
$bodyToken = T_DOUBLE_ARROW;
$bodyToken = T_FN_ARROW;
} else {
$bodyToken = T_OPEN_CURLY_BRACKET;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ $foo = array(<<<JSON
JSON
);

$array = array(
'a' => fn() => return 1,
'bb' => fn() => return 2,
'ccc' => ( true ) ?
fn() => return 1 :
fn() => return 2,
);

// Intentional syntax error.
$a = array(
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ $foo = array(<<<JSON
JSON
);

$array = array(
'a' => fn() => return 1,
'bb' => fn() => return 2,
'ccc' => ( true ) ?
fn() => return 1 :
fn() => return 2,
);

// Intentional syntax error.
$a = array(
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ $foo = [<<<JSON
JSON
];

$array = [
'a' => fn() => return 1,
'bb' => fn() => return 2,
'ccc' => ( true ) ?
fn() => return 1 :
fn() => return 2,
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ $foo = [<<<JSON
JSON
];

$array = [
'a' => fn() => return 1,
'bb' => fn() => return 2,
'ccc' => ( true ) ?
fn() => return 1 :
fn() => return 2,
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
3 changes: 3 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,9 @@ protected function processAdditional()
$this->tokens[$i]['parenthesis_opener'] = $x;
$this->tokens[$i]['parenthesis_closer'] = $this->tokens[$x]['parenthesis_closer'];

$this->tokens[$arrow]['code'] = T_FN_ARROW;
$this->tokens[$arrow]['type'] = 'T_FN_ARROW';

$this->tokens[$arrow]['scope_condition'] = $i;
$this->tokens[$arrow]['scope_opener'] = $arrow;
$this->tokens[$arrow]['scope_closer'] = $scopeCloser;
Expand Down
1 change: 1 addition & 0 deletions src/Util/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
define('T_CLOSE_USE_GROUP', 'PHPCS_T_CLOSE_USE_GROUP');
define('T_ZSR', 'PHPCS_T_ZSR');
define('T_ZSR_EQUAL', 'PHPCS_T_ZSR_EQUAL');
define('T_FN_ARROW', 'T_FN_ARROW');

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

0 comments on commit bbd6f63

Please sign in to comment.