Skip to content

Commit

Permalink
PHP 8.0 | Tokenizer/PHP: support PHP8 dereferencing of text strings w…
Browse files Browse the repository at this point in the history
…ith interpolated variables

As of PHP 8, interpolated text strings can be dereferenced, however, the square brackets are incorrectly tokenized as _short array_ brackets instead of as "normal" square brackets.

Fixed by adding the `T_DOUBLE_QUOTED_STRING` token to the allowed tokens for leaving the brackets alone in the PHP Tokenizer class.

Includes unit test.

Ref: https://wiki.php.net/rfc/variable_syntax_tweaks#interpolated_and_non-interpolated_strings
  • Loading branch information
jrfnl committed Nov 23, 2020
1 parent bb906ff commit 3cd1f67
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,7 @@ protected function processAdditional()
T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR,
T_STRING => T_STRING,
T_CONSTANT_ENCAPSED_STRING => T_CONSTANT_ENCAPSED_STRING,
T_DOUBLE_QUOTED_STRING => T_DOUBLE_QUOTED_STRING,
];
$allowed += Util\Tokens::$magicConstants;

Expand Down
3 changes: 3 additions & 0 deletions tests/Core/Tokenizer/ShortArrayTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ echo (clone $iterable)[20];
/* testNullsafeMethodCallDereferencing */
$var = $obj?->function_call()[$x];

/* testInterpolatedStringDereferencing */
$var = "PHP{$rocks}"[1];

/*
* Short array brackets.
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Tokenizer/ShortArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function dataSquareBrackets()
['/* testClassMemberDereferencingOnInstantiation2 */'],
['/* testClassMemberDereferencingOnClone */'],
['/* testNullsafeMethodCallDereferencing */'],
['/* testInterpolatedStringDereferencing */'],
['/* testLiveCoding */'],
];

Expand Down

0 comments on commit 3cd1f67

Please sign in to comment.