Skip to content

Commit 3cd1f67

Browse files
committed
PHP 8.0 | Tokenizer/PHP: support PHP8 dereferencing of text strings with 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
1 parent bb906ff commit 3cd1f67

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

src/Tokenizers/PHP.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,7 @@ protected function processAdditional()
21472147
T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR,
21482148
T_STRING => T_STRING,
21492149
T_CONSTANT_ENCAPSED_STRING => T_CONSTANT_ENCAPSED_STRING,
2150+
T_DOUBLE_QUOTED_STRING => T_DOUBLE_QUOTED_STRING,
21502151
];
21512152
$allowed += Util\Tokens::$magicConstants;
21522153

tests/Core/Tokenizer/ShortArrayTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ echo (clone $iterable)[20];
6565
/* testNullsafeMethodCallDereferencing */
6666
$var = $obj?->function_call()[$x];
6767

68+
/* testInterpolatedStringDereferencing */
69+
$var = "PHP{$rocks}"[1];
70+
6871
/*
6972
* Short array brackets.
7073
*/

tests/Core/Tokenizer/ShortArrayTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function dataSquareBrackets()
7373
['/* testClassMemberDereferencingOnInstantiation2 */'],
7474
['/* testClassMemberDereferencingOnClone */'],
7575
['/* testNullsafeMethodCallDereferencing */'],
76+
['/* testInterpolatedStringDereferencing */'],
7677
['/* testLiveCoding */'],
7778
];
7879

0 commit comments

Comments
 (0)