Skip to content

Commit 583fd65

Browse files
authored
Merge pull request #472 from PHPCSStandards/php-8.2/file-getmethodproperties-support-dnf
PHP 8.2 | File::getMethodProperties(): add support for DNF types
2 parents f036e26 + 1eb64b8 commit 583fd65

File tree

3 files changed

+163
-12
lines changed

3 files changed

+163
-12
lines changed

src/Files/File.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,18 +1739,20 @@ public function getMethodProperties($stackPtr)
17391739
}
17401740

17411741
$valid = [
1742-
T_STRING => T_STRING,
1743-
T_CALLABLE => T_CALLABLE,
1744-
T_SELF => T_SELF,
1745-
T_PARENT => T_PARENT,
1746-
T_STATIC => T_STATIC,
1747-
T_FALSE => T_FALSE,
1748-
T_TRUE => T_TRUE,
1749-
T_NULL => T_NULL,
1750-
T_NAMESPACE => T_NAMESPACE,
1751-
T_NS_SEPARATOR => T_NS_SEPARATOR,
1752-
T_TYPE_UNION => T_TYPE_UNION,
1753-
T_TYPE_INTERSECTION => T_TYPE_INTERSECTION,
1742+
T_STRING => T_STRING,
1743+
T_CALLABLE => T_CALLABLE,
1744+
T_SELF => T_SELF,
1745+
T_PARENT => T_PARENT,
1746+
T_STATIC => T_STATIC,
1747+
T_FALSE => T_FALSE,
1748+
T_TRUE => T_TRUE,
1749+
T_NULL => T_NULL,
1750+
T_NAMESPACE => T_NAMESPACE,
1751+
T_NS_SEPARATOR => T_NS_SEPARATOR,
1752+
T_TYPE_UNION => T_TYPE_UNION,
1753+
T_TYPE_INTERSECTION => T_TYPE_INTERSECTION,
1754+
T_TYPE_OPEN_PARENTHESIS => T_TYPE_OPEN_PARENTHESIS,
1755+
T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
17541756
];
17551757

17561758
for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) {

tests/Core/File/GetMethodPropertiesTest.inc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ function pseudoTypeTrue(): ?true {}
170170
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
171171
function pseudoTypeFalseAndTrue(): true|false {}
172172

173+
/* testPHP82DNFType */
174+
function hasDNFType() : bool|(Foo&Bar)|string {}
175+
176+
abstract class AbstractClass {
177+
/* testPHP82DNFTypeAbstractMethod */
178+
abstract protected function abstractMethodDNFType() : float|(Foo&Bar);
179+
}
180+
181+
/* testPHP82DNFTypeIllegalNullable */
182+
// Intentional fatal error - nullable operator cannot be combined with DNF.
183+
function illegalNullableDNF(): ?(A&\Pck\B)|bool {}
184+
185+
/* testPHP82DNFTypeClosure */
186+
$closure = function() : object|(namespace\Foo&Countable) {};
187+
188+
/* testPHP82DNFTypeFn */
189+
// Intentional fatal error - void type cannot be combined with DNF.
190+
$arrow = fn() : null|(Partially\Qualified&Traversable)|void => do_something();
191+
173192
/* testNotAFunction */
174193
return true;
175194

tests/Core/File/GetMethodPropertiesTest.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,136 @@ public function testPHP82PseudoTypeFalseAndTrue()
11871187
}//end testPHP82PseudoTypeFalseAndTrue()
11881188

11891189

1190+
/**
1191+
* Verify recognition of PHP 8.2 DNF return type declaration.
1192+
*
1193+
* @return void
1194+
*/
1195+
public function testPHP82DNFType()
1196+
{
1197+
// Offsets are relative to the T_FUNCTION token.
1198+
$expected = [
1199+
'scope' => 'public',
1200+
'scope_specified' => false,
1201+
'return_type' => 'bool|(Foo&Bar)|string',
1202+
'return_type_token' => 8,
1203+
'return_type_end_token' => 16,
1204+
'nullable_return_type' => false,
1205+
'is_abstract' => false,
1206+
'is_final' => false,
1207+
'is_static' => false,
1208+
'has_body' => true,
1209+
];
1210+
1211+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
1212+
1213+
}//end testPHP82DNFType()
1214+
1215+
1216+
/**
1217+
* Verify recognition of PHP 8.2 DNF return type declaration on an abstract method.
1218+
*
1219+
* @return void
1220+
*/
1221+
public function testPHP82DNFTypeAbstractMethod()
1222+
{
1223+
// Offsets are relative to the T_FUNCTION token.
1224+
$expected = [
1225+
'scope' => 'protected',
1226+
'scope_specified' => true,
1227+
'return_type' => 'float|(Foo&Bar)',
1228+
'return_type_token' => 8,
1229+
'return_type_end_token' => 14,
1230+
'nullable_return_type' => false,
1231+
'is_abstract' => true,
1232+
'is_final' => false,
1233+
'is_static' => false,
1234+
'has_body' => false,
1235+
];
1236+
1237+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
1238+
1239+
}//end testPHP82DNFTypeAbstractMethod()
1240+
1241+
1242+
/**
1243+
* Verify recognition of PHP 8.2 DNF return type declaration with illegal nullability.
1244+
*
1245+
* @return void
1246+
*/
1247+
public function testPHP82DNFTypeIllegalNullable()
1248+
{
1249+
// Offsets are relative to the T_FUNCTION token.
1250+
$expected = [
1251+
'scope' => 'public',
1252+
'scope_specified' => false,
1253+
'return_type' => '?(A&\Pck\B)|bool',
1254+
'return_type_token' => 8,
1255+
'return_type_end_token' => 17,
1256+
'nullable_return_type' => true,
1257+
'is_abstract' => false,
1258+
'is_final' => false,
1259+
'is_static' => false,
1260+
'has_body' => true,
1261+
];
1262+
1263+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
1264+
1265+
}//end testPHP82DNFTypeIllegalNullable()
1266+
1267+
1268+
/**
1269+
* Verify recognition of PHP 8.2 DNF return type declaration on a closure.
1270+
*
1271+
* @return void
1272+
*/
1273+
public function testPHP82DNFTypeClosure()
1274+
{
1275+
// Offsets are relative to the T_CLOSURE token.
1276+
$expected = [
1277+
'scope' => 'public',
1278+
'scope_specified' => false,
1279+
'return_type' => 'object|(namespace\Foo&Countable)',
1280+
'return_type_token' => 6,
1281+
'return_type_end_token' => 14,
1282+
'nullable_return_type' => false,
1283+
'is_abstract' => false,
1284+
'is_final' => false,
1285+
'is_static' => false,
1286+
'has_body' => true,
1287+
];
1288+
1289+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
1290+
1291+
}//end testPHP82DNFTypeClosure()
1292+
1293+
1294+
/**
1295+
* Verify recognition of PHP 8.2 DNF return type declaration on an arrow function.
1296+
*
1297+
* @return void
1298+
*/
1299+
public function testPHP82DNFTypeFn()
1300+
{
1301+
// Offsets are relative to the T_FN token.
1302+
$expected = [
1303+
'scope' => 'public',
1304+
'scope_specified' => false,
1305+
'return_type' => 'null|(Partially\Qualified&Traversable)|void',
1306+
'return_type_token' => 6,
1307+
'return_type_end_token' => 16,
1308+
'nullable_return_type' => false,
1309+
'is_abstract' => false,
1310+
'is_final' => false,
1311+
'is_static' => false,
1312+
'has_body' => true,
1313+
];
1314+
1315+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
1316+
1317+
}//end testPHP82DNFTypeFn()
1318+
1319+
11901320
/**
11911321
* Test for incorrect tokenization of array return type declarations in PHPCS < 2.8.0.
11921322
*

0 commit comments

Comments
 (0)