Skip to content

Commit

Permalink
PHP 8.0 | File::getMemberProperties(): add support for "union" types
Browse files Browse the repository at this point in the history
This adds support for union types to the `File::getMemberProperties()` method.

Includes extensive unit tests.
  • Loading branch information
jrfnl committed Sep 20, 2020
1 parent 38c01b3 commit b552768
Show file tree
Hide file tree
Showing 3 changed files with 171 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 @@ -1821,8 +1821,11 @@ public function getMemberProperties($stackPtr)
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_FALSE => T_FALSE,
T_NULL => T_NULL,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_TYPE_UNION => T_TYPE_UNION,
];

for ($i; $i < $stackPtr; $i++) {
Expand Down
47 changes: 47 additions & 0 deletions tests/Core/File/GetMemberPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,50 @@ class NSOperatorInType {
/* testNamespaceOperatorTypeHint */
public ?namespace\Name $prop;
}

$anon = class() {
/* testPHP8UnionTypesSimple */
public int|float $unionTypeSimple;

/* testPHP8UnionTypesTwoClasses */
private MyClassA|\Package\MyClassB $unionTypesTwoClasses;

/* testPHP8UnionTypesAllBaseTypes */
protected array|bool|int|float|NULL|object|string $unionTypesAllBaseTypes;

/* testPHP8UnionTypesAllPseudoTypes */
// Intentional fatal error - mixing types which cannot be combined, but that's not the concern of the method.
var false|mixed|self|parent|iterable|Resource $unionTypesAllPseudoTypes;

/* testPHP8UnionTypesIllegalTypes */
// Intentional fatal error - types which are not allowed for properties, but that's not the concern of the method.
public callable|static|void $unionTypesIllegalTypes;

/* testPHP8UnionTypesNullable */
// Intentional fatal error - nullability is not allowed with union types, but that's not the concern of the method.
public ?int|float $unionTypesNullable;

/* testPHP8PseudoTypeNull */
// Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
public null $pseudoTypeNull;

/* testPHP8PseudoTypeFalse */
// Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
public false $pseudoTypeFalse;

/* testPHP8PseudoTypeFalseAndBool */
// Intentional fatal error - false pseudotype is not allowed in combination with bool, but that's not the concern of the method.
public bool|FALSE $pseudoTypeFalseAndBool;

/* testPHP8ObjectAndClass */
// Intentional fatal error - object is not allowed in combination with class name, but that's not the concern of the method.
public object|ClassName $objectAndClass;

/* testPHP8PseudoTypeIterableAndArray */
// Intentional fatal error - iterable pseudotype is not allowed in combination with array or Traversable, but that's not the concern of the method.
public iterable|array|Traversable $pseudoTypeIterableAndArray;

/* testPHP8DuplicateTypeInUnionWhitespaceAndComment */
// Intentional fatal error - duplicate types are not allowed in union types, but that's not the concern of the method.
public int |string| /*comment*/ INT $duplicateTypeInUnion;
};
121 changes: 121 additions & 0 deletions tests/Core/File/GetMemberPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,127 @@ public function dataGetMemberProperties()
'nullable_type' => true,
],
],
[
'/* testPHP8UnionTypesSimple */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => 'int|float',
'nullable_type' => false,
],
],
[
'/* testPHP8UnionTypesTwoClasses */',
[
'scope' => 'private',
'scope_specified' => true,
'is_static' => false,
'type' => 'MyClassA|\Package\MyClassB',
'nullable_type' => false,
],
],
[
'/* testPHP8UnionTypesAllBaseTypes */',
[
'scope' => 'protected',
'scope_specified' => true,
'is_static' => false,
'type' => 'array|bool|int|float|NULL|object|string',
'nullable_type' => false,
],
],
[
'/* testPHP8UnionTypesAllPseudoTypes */',
[
'scope' => 'public',
'scope_specified' => false,
'is_static' => false,
'type' => 'false|mixed|self|parent|iterable|Resource',
'nullable_type' => false,
],
],
[
'/* testPHP8UnionTypesIllegalTypes */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
// Missing static, but that's OK as not an allowed syntax.
'type' => 'callable||void',
'nullable_type' => false,
],
],
[
'/* testPHP8UnionTypesNullable */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => '?int|float',
'nullable_type' => true,
],
],
[
'/* testPHP8PseudoTypeNull */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => 'null',
'nullable_type' => false,
],
],
[
'/* testPHP8PseudoTypeFalse */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => 'false',
'nullable_type' => false,
],
],
[
'/* testPHP8PseudoTypeFalseAndBool */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => 'bool|FALSE',
'nullable_type' => false,
],
],
[
'/* testPHP8ObjectAndClass */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => 'object|ClassName',
'nullable_type' => false,
],
],
[
'/* testPHP8PseudoTypeIterableAndArray */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => 'iterable|array|Traversable',
'nullable_type' => false,
],
],
[
'/* testPHP8DuplicateTypeInUnionWhitespaceAndComment */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => 'int|string|INT',
'nullable_type' => false,
],
],
];

}//end dataGetMemberProperties()
Expand Down

0 comments on commit b552768

Please sign in to comment.