Skip to content

Commit 7873cc4

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 bb63db9 commit 7873cc4

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
@@ -1419,6 +1419,7 @@ public function getMethodParameters($stackPtr)
14191419
case T_TYPE_UNION:
14201420
case T_TYPE_INTERSECTION:
14211421
case T_FALSE:
1422+
case T_TRUE:
14221423
case T_NULL:
14231424
// Part of a type hint or default value.
14241425
if ($defaultStart === null) {

tests/Core/File/GetMethodParametersTest.inc

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

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

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

8282
/* testPHP8PseudoTypeFalseAndBool */
@@ -173,3 +173,10 @@ $closure = function (string&int $numeric_string) {};
173173
/* testPHP81NullableIntersectionTypes */
174174
// Intentional fatal error - nullability is not allowed with intersection types, but that's not the concern of the method.
175175
$closure = function (?Foo&Bar $object) {};
176+
177+
/* testPHP82PseudoTypeTrue */
178+
function pseudoTypeTrue(?true $var = true) {}
179+
180+
/* testPHP82PseudoTypeFalseAndTrue */
181+
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
182+
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
@@ -1231,6 +1231,54 @@ public function testPHP81NullableIntersectionTypes()
12311231
}//end testPHP81NullableIntersectionTypes()
12321232

12331233

1234+
/**
1235+
* Verify recognition of PHP 8.2 stand-alone `true` type.
1236+
*
1237+
* @return void
1238+
*/
1239+
public function testPHP82PseudoTypeTrue()
1240+
{
1241+
$expected = [];
1242+
$expected[0] = [
1243+
'name' => '$var',
1244+
'content' => '?true $var = true',
1245+
'default' => 'true',
1246+
'has_attributes' => false,
1247+
'pass_by_reference' => false,
1248+
'variable_length' => false,
1249+
'type_hint' => '?true',
1250+
'nullable_type' => true,
1251+
];
1252+
1253+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
1254+
1255+
}//end testPHP82PseudoTypeTrue()
1256+
1257+
1258+
/**
1259+
* Verify recognition of PHP 8.2 type declaration with (illegal) type false combined with type true.
1260+
*
1261+
* @return void
1262+
*/
1263+
public function testPHP82PseudoTypeFalseAndTrue()
1264+
{
1265+
$expected = [];
1266+
$expected[0] = [
1267+
'name' => '$var',
1268+
'content' => 'true|false $var = true',
1269+
'default' => 'true',
1270+
'has_attributes' => false,
1271+
'pass_by_reference' => false,
1272+
'variable_length' => false,
1273+
'type_hint' => 'true|false',
1274+
'nullable_type' => false,
1275+
];
1276+
1277+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
1278+
1279+
}//end testPHP82PseudoTypeFalseAndTrue()
1280+
1281+
12341282
/**
12351283
* Test helper.
12361284
*

0 commit comments

Comments
 (0)