Skip to content

Commit daff272

Browse files
committed
PHP 8.1 | File::findImplementedInterfaceNames(): add support for enums implementing interfaces
An `enum` declaration can implement one or more interfaces. This commit updates the `File::findImplementedInterfaceNames()` method to allow it to find the interfaces implemented by an `enum` as well. Includes unit tests.
1 parent 2596a15 commit daff272

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/Files/File.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,11 +2765,11 @@ public function findExtendedClassName($stackPtr)
27652765

27662766

27672767
/**
2768-
* Returns the names of the interfaces that the specified class implements.
2768+
* Returns the names of the interfaces that the specified class or enum implements.
27692769
*
27702770
* Returns FALSE on error or if there are no implemented interface names.
27712771
*
2772-
* @param int $stackPtr The stack position of the class.
2772+
* @param int $stackPtr The stack position of the class or enum token.
27732773
*
27742774
* @return array|false
27752775
*/
@@ -2782,6 +2782,7 @@ public function findImplementedInterfaceNames($stackPtr)
27822782

27832783
if ($this->tokens[$stackPtr]['code'] !== T_CLASS
27842784
&& $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS
2785+
&& $this->tokens[$stackPtr]['code'] !== T_ENUM
27852786
) {
27862787
return false;
27872788
}

tests/Core/File/FindImplementedInterfaceNamesTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ class testFECNClassThatExtendsAndImplements extends testFECNClass implements Int
2424

2525
/* testClassThatImplementsAndExtends */
2626
class testFECNClassThatImplementsAndExtends implements \InterfaceA, InterfaceB extends testFECNClass {}
27+
28+
/* testBackedEnumWithoutImplements */
29+
enum Suit:string {}
30+
31+
/* testEnumImplements */
32+
enum Suit implements Colorful {}
33+
34+
/* testBackedEnumImplements */
35+
enum Suit: string implements Colorful, \Deck {}

tests/Core/File/FindImplementedInterfaceNamesTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FindImplementedInterfaceNamesTest extends AbstractMethodUnitTest
2727
*/
2828
public function testFindImplementedInterfaceNames($identifier, $expected)
2929
{
30-
$OOToken = $this->getTargetToken($identifier, [T_CLASS, T_ANON_CLASS, T_INTERFACE]);
30+
$OOToken = $this->getTargetToken($identifier, [T_CLASS, T_ANON_CLASS, T_INTERFACE, T_ENUM]);
3131
$result = self::$phpcsFile->findImplementedInterfaceNames($OOToken);
3232
$this->assertSame($expected, $result);
3333

@@ -81,6 +81,21 @@ public function dataImplementedInterface()
8181
'InterfaceB',
8282
],
8383
],
84+
[
85+
'/* testBackedEnumWithoutImplements */',
86+
false,
87+
],
88+
[
89+
'/* testEnumImplements */',
90+
['Colorful'],
91+
],
92+
[
93+
'/* testBackedEnumImplements */',
94+
[
95+
'Colorful',
96+
'\Deck',
97+
],
98+
],
8499
];
85100

86101
}//end dataImplementedInterface()

0 commit comments

Comments
 (0)