Skip to content

Commit 3aa8682

Browse files
committed
PHP 8.2 | File::getMethodParameters(): 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::getMethodParameters()` 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 c709801 commit 3aa8682

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/Files/File.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,7 @@ public function getMethodParameters($stackPtr)
14791479
case T_TYPE_UNION:
14801480
case T_TYPE_INTERSECTION:
14811481
case T_FALSE:
1482+
case T_TRUE:
14821483
case T_NULL:
14831484
// Part of a type hint or default value.
14841485
if ($defaultStart === null) {

tests/Core/File/GetMethodParametersTest.inc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ function unionTypesAllPseudoTypes(false|mixed|self|parent|iterable|Resource $var
6666
$closure = function (?int|float $number) {};
6767

6868
/* testPHP8PseudoTypeNull */
69-
// Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
69+
// 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.
7070
function pseudoTypeNull(null $var = null) {}
7171

7272
/* testPHP8PseudoTypeFalse */
73-
// Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
73+
// 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.
7474
function pseudoTypeFalse(false $var = false) {}
7575

7676
/* testPHP8PseudoTypeFalseAndBool */
@@ -167,3 +167,10 @@ $closure = function (string&int $numeric_string) {};
167167
/* testPHP81NullableIntersectionTypes */
168168
// Intentional fatal error - nullability is not allowed with intersection types, but that's not the concern of the method.
169169
$closure = function (?Foo&Bar $object) {};
170+
171+
/* testPHP82PseudoTypeTrue */
172+
function pseudoTypeTrue(?true $var = true) {}
173+
174+
/* testPHP82PseudoTypeFalseAndTrue */
175+
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
176+
function pseudoTypeFalseAndTrue(true|false $var = true) {}

tests/Core/File/GetMethodParametersTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,54 @@ public function testPHP81NullableIntersectionTypes()
11661166
}//end testPHP81NullableIntersectionTypes()
11671167

11681168

1169+
/**
1170+
* Verify recognition of PHP 8.2 stand-alone `true` type.
1171+
*
1172+
* @return void
1173+
*/
1174+
public function testPHP82PseudoTypeTrue()
1175+
{
1176+
$expected = [];
1177+
$expected[0] = [
1178+
'name' => '$var',
1179+
'content' => '?true $var = true',
1180+
'default' => 'true',
1181+
'has_attributes' => false,
1182+
'pass_by_reference' => false,
1183+
'variable_length' => false,
1184+
'type_hint' => '?true',
1185+
'nullable_type' => true,
1186+
];
1187+
1188+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
1189+
1190+
}//end testPHP82PseudoTypeTrue()
1191+
1192+
1193+
/**
1194+
* Verify recognition of PHP 8.2 type declaration with (illegal) type false combined with type true.
1195+
*
1196+
* @return void
1197+
*/
1198+
public function testPHP82PseudoTypeFalseAndTrue()
1199+
{
1200+
$expected = [];
1201+
$expected[0] = [
1202+
'name' => '$var',
1203+
'content' => 'true|false $var = true',
1204+
'default' => 'true',
1205+
'has_attributes' => false,
1206+
'pass_by_reference' => false,
1207+
'variable_length' => false,
1208+
'type_hint' => 'true|false',
1209+
'nullable_type' => false,
1210+
];
1211+
1212+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
1213+
1214+
}//end testPHP82PseudoTypeFalseAndTrue()
1215+
1216+
11691217
/**
11701218
* Test helper.
11711219
*

0 commit comments

Comments
 (0)