Skip to content

Commit e91f880

Browse files
committed
PHP Tokenizer: bugfix for PHP 7.4 typed properties
Typed properties are explicitly also supported for PHP 4-style properties using just the `var` or `static` keyword. However, in that case, the nullable indicator `?` was not converted from `T_INLINE_THEN` to `T_NULLABLE`. Fixed now. Includes unit tests via the `File::getMemberProperties()` method. Ref: https://wiki.php.net/rfc/typed_properties_v2
1 parent c2494e3 commit e91f880

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/Tokenizers/PHP.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ protected function tokenize($string)
11521152
if ($tokenType === T_FUNCTION
11531153
|| $tokenType === T_FN
11541154
|| isset(Util\Tokens::$methodPrefixes[$tokenType]) === true
1155+
|| $tokenType === T_VAR
11551156
) {
11561157
if (PHP_CODESNIFFER_VERBOSITY > 1) {
11571158
echo "\t\t* token $stackPtr changed from ? to T_NULLABLE".PHP_EOL;

tests/Core/File/GetMemberPropertiesTest.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TestMemberProperties
66
var $varA = true;
77

88
/* testVarType */
9-
var int $varA = true;
9+
var ?int $varA = true;
1010

1111
/* testPublic */
1212
public $varB = true;
@@ -30,7 +30,7 @@ class TestMemberProperties
3030
static $varE = true;
3131

3232
/* testStaticType */
33-
static string $varE = true;
33+
static ?string $varE = true;
3434

3535
/* testStaticVar */
3636
static var $varF = true;

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public function dataGetMemberProperties()
6161
'scope' => 'public',
6262
'scope_specified' => false,
6363
'is_static' => false,
64-
'type' => 'int',
65-
'nullable_type' => false,
64+
'type' => '?int',
65+
'nullable_type' => true,
6666
],
6767
],
6868
[
@@ -141,8 +141,8 @@ public function dataGetMemberProperties()
141141
'scope' => 'public',
142142
'scope_specified' => false,
143143
'is_static' => true,
144-
'type' => 'string',
145-
'nullable_type' => false,
144+
'type' => '?string',
145+
'nullable_type' => true,
146146
],
147147
],
148148
[

0 commit comments

Comments
 (0)