Skip to content

Commit

Permalink
PHP Tokenizer: bugfix for PHP 7.4 typed properties
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Jan 9, 2020
1 parent c2494e3 commit e91f880
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ protected function tokenize($string)
if ($tokenType === T_FUNCTION
|| $tokenType === T_FN
|| isset(Util\Tokens::$methodPrefixes[$tokenType]) === true
|| $tokenType === T_VAR
) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t* token $stackPtr changed from ? to T_NULLABLE".PHP_EOL;
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/File/GetMemberPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TestMemberProperties
var $varA = true;

/* testVarType */
var int $varA = true;
var ?int $varA = true;

/* testPublic */
public $varB = true;
Expand All @@ -30,7 +30,7 @@ class TestMemberProperties
static $varE = true;

/* testStaticType */
static string $varE = true;
static ?string $varE = true;

/* testStaticVar */
static var $varF = true;
Expand Down
8 changes: 4 additions & 4 deletions tests/Core/File/GetMemberPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function dataGetMemberProperties()
'scope' => 'public',
'scope_specified' => false,
'is_static' => false,
'type' => 'int',
'nullable_type' => false,
'type' => '?int',
'nullable_type' => true,
],
],
[
Expand Down Expand Up @@ -141,8 +141,8 @@ public function dataGetMemberProperties()
'scope' => 'public',
'scope_specified' => false,
'is_static' => true,
'type' => 'string',
'nullable_type' => false,
'type' => '?string',
'nullable_type' => true,
],
],
[
Expand Down

0 comments on commit e91f880

Please sign in to comment.