Skip to content

Commit 963c622

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 ee71851 commit 963c622

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
@@ -1705,6 +1705,7 @@ public function getMethodProperties($stackPtr)
17051705
T_PARENT => T_PARENT,
17061706
T_STATIC => T_STATIC,
17071707
T_FALSE => T_FALSE,
1708+
T_TRUE => T_TRUE,
17081709
T_NULL => T_NULL,
17091710
T_NAMESPACE => T_NAMESPACE,
17101711
T_NS_SEPARATOR => T_NS_SEPARATOR,

tests/Core/File/GetMethodPropertiesTest.inc

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

104104
/* testPHP8PseudoTypeNull */
105-
// Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
105+
// 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.
106106
function pseudoTypeNull(): null {}
107107

108108
/* testPHP8PseudoTypeFalse */
109-
// Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
109+
// 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.
110110
function pseudoTypeFalse(): false {}
111111

112112
/* testPHP8PseudoTypeFalseAndBool */
@@ -150,3 +150,10 @@ $closure = function (): string&int {};
150150
/* testPHP81NullableIntersectionTypes */
151151
// Intentional fatal error - nullability is not allowed with intersection types, but that's not the concern of the method.
152152
$closure = function (): ?Foo&Bar {};
153+
154+
/* testPHP82PseudoTypeTrue */
155+
function pseudoTypeTrue(): ?true {}
156+
157+
/* testPHP82PseudoTypeFalseAndTrue */
158+
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
159+
function pseudoTypeFalseAndTrue(): true|false {}

tests/Core/File/GetMethodPropertiesTest.php

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

891891

892+
/**
893+
* Verify recognition of PHP 8.2 stand-alone `true` type.
894+
*
895+
* @return void
896+
*/
897+
public function testPHP82PseudoTypeTrue()
898+
{
899+
$expected = [
900+
'scope' => 'public',
901+
'scope_specified' => false,
902+
'return_type' => '?true',
903+
'nullable_return_type' => true,
904+
'is_abstract' => false,
905+
'is_final' => false,
906+
'is_static' => false,
907+
'has_body' => true,
908+
];
909+
910+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
911+
912+
}//end testPHP82PseudoTypeTrue()
913+
914+
915+
/**
916+
* Verify recognition of PHP 8.2 type declaration with (illegal) type false combined with type true.
917+
*
918+
* @return void
919+
*/
920+
public function testPHP82PseudoTypeFalseAndTrue()
921+
{
922+
$expected = [
923+
'scope' => 'public',
924+
'scope_specified' => false,
925+
'return_type' => 'true|false',
926+
'nullable_return_type' => false,
927+
'is_abstract' => false,
928+
'is_final' => false,
929+
'is_static' => false,
930+
'has_body' => true,
931+
];
932+
933+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
934+
935+
}//end testPHP82PseudoTypeFalseAndTrue()
936+
937+
892938
/**
893939
* Test helper.
894940
*

0 commit comments

Comments
 (0)