Skip to content

Commit

Permalink
File::get[Method|Member][Properties|Parameters](): add support for na…
Browse files Browse the repository at this point in the history
…mespace relative type declarations

The `namespace` keyword as an operator is perfectly valid to be used as part of a type declaration.

This usage was so far not supported in the `File::getMethodParameters()`, `File::getMethodProperties()` and the `File::getMemberProperties()` methods.

Fixes now. Including adding a unit test for each.
  • Loading branch information
jrfnl committed Sep 2, 2020
1 parent db43214 commit f2a5abf
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ public function getMethodParameters($stackPtr)
$typeHintEndToken = $i;
}
break;
case T_NAMESPACE:
case T_NS_SEPARATOR:
// Part of a type hint or default value.
if ($defaultStart === null) {
Expand Down Expand Up @@ -1630,6 +1631,7 @@ public function getMethodProperties($stackPtr)
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_STATIC => T_STATIC,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
];

Expand Down Expand Up @@ -1813,6 +1815,7 @@ public function getMemberProperties($stackPtr)
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
];

Expand Down
5 changes: 5 additions & 0 deletions tests/Core/File/GetMemberPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,8 @@ class PHP8Mixed {
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
private ?mixed $nullableMixed;
}

class NSOperatorInType {
/* testNamespaceOperatorTypeHint */
public ?namespace\Name $prop;
}
10 changes: 10 additions & 0 deletions tests/Core/File/GetMemberPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ public function dataGetMemberProperties()
'nullable_type' => true,
],
],
[
'/* testNamespaceOperatorTypeHint */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => '?namespace\Name',
'nullable_type' => true,
],
],
];

}//end dataGetMemberProperties()
Expand Down
3 changes: 3 additions & 0 deletions tests/Core/File/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ function mixedTypeHint(mixed &...$var1) {}
/* testPHP8MixedTypeHintNullable */
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
function mixedTypeHintNullable(?Mixed $var1) {}

/* testNamespaceOperatorTypeHint */
function namespaceOperatorTypeHint(?namespace\Name $var1) {}
22 changes: 22 additions & 0 deletions tests/Core/File/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,28 @@ public function testPHP8MixedTypeHintNullable()
}//end testPHP8MixedTypeHintNullable()


/**
* Verify recognition of type declarations using the namespace operator.
*
* @return void
*/
public function testNamespaceOperatorTypeHint()
{
$expected = [];
$expected[0] = [
'name' => '$var1',
'content' => '?namespace\Name $var1',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '?namespace\Name',
'nullable_type' => true,
];

$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testNamespaceOperatorTypeHint()


/**
* Test helper.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/Core/File/GetMethodPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ function mixedTypeHint() :mixed {}
/* testPHP8MixedTypeHintNullable */
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
function mixedTypeHintNullable(): ?mixed {}

/* testNamespaceOperatorTypeHint */
function namespaceOperatorTypeHint() : ?namespace\Name {}
23 changes: 23 additions & 0 deletions tests/Core/File/GetMethodPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,29 @@ public function testPHP8MixedTypeHintNullable()
}//end testPHP8MixedTypeHintNullable()


/**
* Test a function with return type using the namespace operator.
*
* @return void
*/
public function testNamespaceOperatorTypeHint()
{
$expected = [
'scope' => 'public',
'scope_specified' => false,
'return_type' => '?namespace\Name',
'nullable_return_type' => true,
'is_abstract' => false,
'is_final' => false,
'is_static' => false,
'has_body' => true,
];

$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testNamespaceOperatorTypeHint()


/**
* Test helper.
*
Expand Down

0 comments on commit f2a5abf

Please sign in to comment.