Skip to content

Commit eb061fd

Browse files
committed
Tokens\Collections::$returnTypeTokens: allow for "static" (PHP 8)
As of PHP 8.0, `static` can be used as a return type for function declarations. Ref: https://wiki.php.net/rfc/static_return_type Includes adding a unit test for the `BCFile::getMethodProperties()`/`FunctionDeclarations::getProperties()` methods to safeguard this. Sister-PR to the upstream squizlabs/PHP_CodeSniffer 2952
1 parent e4aa64c commit eb061fd

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

PHPCSUtils/Tokens/Collections.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ class Collections
423423
\T_CALLABLE => \T_CALLABLE,
424424
\T_SELF => \T_SELF,
425425
\T_PARENT => \T_PARENT,
426+
\T_STATIC => \T_STATIC,
426427
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
427428
];
428429

Tests/BackCompat/BCFile/GetMethodPropertiesTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ $result = array_map(
6767
$numbers
6868
);
6969

70+
class ReturnMe {
71+
/* testReturnTypeStatic */
72+
private function myFunction(): static {
73+
return $this;
74+
}
75+
}
76+
7077
/* testNotAFunction */
7178
return true;
7279

Tests/BackCompat/BCFile/GetMethodPropertiesTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,28 @@ public function testArrowFunction()
433433
$this->getMethodPropertiesTestHelper('/* ' . __FUNCTION__ . ' */', $expected, $arrowTokenTypes);
434434
}
435435

436+
/**
437+
* Test a function with return type "static".
438+
*
439+
* @return void
440+
*/
441+
public function testReturnTypeStatic()
442+
{
443+
$expected = [
444+
'scope' => 'private',
445+
'scope_specified' => true,
446+
'return_type' => 'static',
447+
'return_type_token' => 7, // Offset from the T_FUNCTION token.
448+
'nullable_return_type' => false,
449+
'is_abstract' => false,
450+
'is_final' => false,
451+
'is_static' => false,
452+
'has_body' => true,
453+
];
454+
455+
$this->getMethodPropertiesTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
456+
}
457+
436458
/**
437459
* Test for incorrect tokenization of array return type declarations in PHPCS < 2.8.0.
438460
*

0 commit comments

Comments
 (0)