Skip to content

Commit f2a5abf

Browse files
committed
File::get[Method|Member][Properties|Parameters](): add support for namespace 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.
1 parent db43214 commit f2a5abf

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

src/Files/File.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ public function getMethodParameters($stackPtr)
14411441
$typeHintEndToken = $i;
14421442
}
14431443
break;
1444+
case T_NAMESPACE:
14441445
case T_NS_SEPARATOR:
14451446
// Part of a type hint or default value.
14461447
if ($defaultStart === null) {
@@ -1630,6 +1631,7 @@ public function getMethodProperties($stackPtr)
16301631
T_SELF => T_SELF,
16311632
T_PARENT => T_PARENT,
16321633
T_STATIC => T_STATIC,
1634+
T_NAMESPACE => T_NAMESPACE,
16331635
T_NS_SEPARATOR => T_NS_SEPARATOR,
16341636
];
16351637

@@ -1813,6 +1815,7 @@ public function getMemberProperties($stackPtr)
18131815
T_CALLABLE => T_CALLABLE,
18141816
T_SELF => T_SELF,
18151817
T_PARENT => T_PARENT,
1818+
T_NAMESPACE => T_NAMESPACE,
18161819
T_NS_SEPARATOR => T_NS_SEPARATOR,
18171820
];
18181821

tests/Core/File/GetMemberPropertiesTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,8 @@ class PHP8Mixed {
188188
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
189189
private ?mixed $nullableMixed;
190190
}
191+
192+
class NSOperatorInType {
193+
/* testNamespaceOperatorTypeHint */
194+
public ?namespace\Name $prop;
195+
}

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,16 @@ public function dataGetMemberProperties()
479479
'nullable_type' => true,
480480
],
481481
],
482+
[
483+
'/* testNamespaceOperatorTypeHint */',
484+
[
485+
'scope' => 'public',
486+
'scope_specified' => true,
487+
'is_static' => false,
488+
'type' => '?namespace\Name',
489+
'nullable_type' => true,
490+
],
491+
],
482492
];
483493

484494
}//end dataGetMemberProperties()

tests/Core/File/GetMethodParametersTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ function mixedTypeHint(mixed &...$var1) {}
3838
/* testPHP8MixedTypeHintNullable */
3939
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
4040
function mixedTypeHintNullable(?Mixed $var1) {}
41+
42+
/* testNamespaceOperatorTypeHint */
43+
function namespaceOperatorTypeHint(?namespace\Name $var1) {}

tests/Core/File/GetMethodParametersTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ public function testPHP8MixedTypeHintNullable()
318318
}//end testPHP8MixedTypeHintNullable()
319319

320320

321+
/**
322+
* Verify recognition of type declarations using the namespace operator.
323+
*
324+
* @return void
325+
*/
326+
public function testNamespaceOperatorTypeHint()
327+
{
328+
$expected = [];
329+
$expected[0] = [
330+
'name' => '$var1',
331+
'content' => '?namespace\Name $var1',
332+
'pass_by_reference' => false,
333+
'variable_length' => false,
334+
'type_hint' => '?namespace\Name',
335+
'nullable_type' => true,
336+
];
337+
338+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
339+
340+
}//end testNamespaceOperatorTypeHint()
341+
342+
321343
/**
322344
* Test helper.
323345
*

tests/Core/File/GetMethodPropertiesTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,6 @@ function mixedTypeHint() :mixed {}
8080
/* testPHP8MixedTypeHintNullable */
8181
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
8282
function mixedTypeHintNullable(): ?mixed {}
83+
84+
/* testNamespaceOperatorTypeHint */
85+
function namespaceOperatorTypeHint() : ?namespace\Name {}

tests/Core/File/GetMethodPropertiesTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,29 @@ public function testPHP8MixedTypeHintNullable()
452452
}//end testPHP8MixedTypeHintNullable()
453453

454454

455+
/**
456+
* Test a function with return type using the namespace operator.
457+
*
458+
* @return void
459+
*/
460+
public function testNamespaceOperatorTypeHint()
461+
{
462+
$expected = [
463+
'scope' => 'public',
464+
'scope_specified' => false,
465+
'return_type' => '?namespace\Name',
466+
'nullable_return_type' => true,
467+
'is_abstract' => false,
468+
'is_final' => false,
469+
'is_static' => false,
470+
'has_body' => true,
471+
];
472+
473+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
474+
475+
}//end testNamespaceOperatorTypeHint()
476+
477+
455478
/**
456479
* Test helper.
457480
*

0 commit comments

Comments
 (0)