Skip to content

Commit bb63db9

Browse files
committed
PHP 8.2 | File::getMethodProperties(): allow for true in types
As of PHP 8.2, `true`, `false` and `null` will be allowed as stand-alone types. `true` can now also be used in union types (was already allowed for `false` and `null`). The `true` and the `false` types are allowed to be nullable, the `null` type is not (but that's not the concern of this method). Also see: https://3v4l.org/ZpfID This commit adjusts the `File::getMethodProperties()` method to take `true` into account. `false` and `null` were already handled due to these previously already being allowed in union types. Includes unit tests. Refs: * https://wiki.php.net/rfc/null-false-standalone-types * https://wiki.php.net/rfc/true-type
1 parent cb1458f commit bb63db9

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/Files/File.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,7 @@ public function getMethodProperties($stackPtr)
16481648
T_PARENT => T_PARENT,
16491649
T_STATIC => T_STATIC,
16501650
T_FALSE => T_FALSE,
1651+
T_TRUE => T_TRUE,
16511652
T_NULL => T_NULL,
16521653
T_TYPE_INTERSECTION => T_TYPE_INTERSECTION,
16531654
T_TYPE_UNION => T_TYPE_UNION,

tests/Core/File/GetMethodPropertiesTest.inc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ function unionTypesAllPseudoTypes($var) : false|MIXED|self|parent|static|iterabl
108108
$closure = function () use($a) :?int|float {};
109109

110110
/* testPHP8PseudoTypeNull */
111-
// Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
111+
// PHP 8.0 - 8.1: Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
112112
function pseudoTypeNull(): null {}
113113

114114
/* testPHP8PseudoTypeFalse */
115-
// Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
115+
// PHP 8.0 - 8.1: Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
116116
function pseudoTypeFalse(): false {}
117117

118118
/* testPHP8PseudoTypeFalseAndBool */
@@ -156,3 +156,10 @@ $closure = function (): string&int {};
156156
/* testPHP81NullableIntersectionTypes */
157157
// Intentional fatal error - nullability is not allowed with intersection types, but that's not the concern of the method.
158158
$closure = function (): ?Foo&Bar {};
159+
160+
/* testPHP82PseudoTypeTrue */
161+
function pseudoTypeTrue(): ?true {}
162+
163+
/* testPHP82PseudoTypeFalseAndTrue */
164+
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
165+
function pseudoTypeFalseAndTrue(): true|false {}

tests/Core/File/GetMethodPropertiesTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,52 @@ public function testPHP81NullableIntersectionTypes()
935935
}//end testPHP81NullableIntersectionTypes()
936936

937937

938+
/**
939+
* Verify recognition of PHP 8.2 stand-alone `true` type.
940+
*
941+
* @return void
942+
*/
943+
public function testPHP82PseudoTypeTrue()
944+
{
945+
$expected = [
946+
'scope' => 'public',
947+
'scope_specified' => false,
948+
'return_type' => '?true',
949+
'nullable_return_type' => true,
950+
'is_abstract' => false,
951+
'is_final' => false,
952+
'is_static' => false,
953+
'has_body' => true,
954+
];
955+
956+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
957+
958+
}//end testPHP82PseudoTypeTrue()
959+
960+
961+
/**
962+
* Verify recognition of PHP 8.2 type declaration with (illegal) type false combined with type true.
963+
*
964+
* @return void
965+
*/
966+
public function testPHP82PseudoTypeFalseAndTrue()
967+
{
968+
$expected = [
969+
'scope' => 'public',
970+
'scope_specified' => false,
971+
'return_type' => 'true|false',
972+
'nullable_return_type' => false,
973+
'is_abstract' => false,
974+
'is_final' => false,
975+
'is_static' => false,
976+
'has_body' => true,
977+
];
978+
979+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
980+
981+
}//end testPHP82PseudoTypeFalseAndTrue()
982+
983+
938984
/**
939985
* Test helper.
940986
*

0 commit comments

Comments
 (0)