Skip to content

Commit 8835dfa

Browse files
committed
ClassHelper: Improved getAllNames()
1 parent 2cfddd1 commit 8835dfa

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

SlevomatCodingStandard/Helpers/ClassHelper.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Generator;
66
use PHP_CodeSniffer\Files\File;
7-
use function array_map;
87
use function iterator_to_array;
98
use function sprintf;
109
use const T_ANON_CLASS;
@@ -47,18 +46,19 @@ public static function getName(File $phpcsFile, int $classPointer): string
4746

4847
/**
4948
* @param \PHP_CodeSniffer\Files\File $phpcsFile
50-
* @return string[]
49+
* @return array<int, string>
5150
*/
5251
public static function getAllNames(File $phpcsFile): array
5352
{
5453
$previousClassPointer = 0;
5554

56-
return array_map(
57-
static function (int $classPointer) use ($phpcsFile): string {
58-
return self::getName($phpcsFile, $classPointer);
59-
},
60-
iterator_to_array(self::getAllClassPointers($phpcsFile, $previousClassPointer))
61-
);
55+
$names = [];
56+
/** @var int $classPointer */
57+
foreach (iterator_to_array(self::getAllClassPointers($phpcsFile, $previousClassPointer)) as $classPointer) {
58+
$names[$classPointer] = self::getName($phpcsFile, $classPointer);
59+
}
60+
61+
return $names;
6262
}
6363

6464
/**

tests/Helpers/ClassHelperTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SlevomatCodingStandard\Helpers;
44

5+
use function array_values;
56
use const T_ANON_CLASS;
67

78
class ClassHelperTest extends TestCase
@@ -36,13 +37,13 @@ public function testNameWithoutNamespace(): void
3637
public function testGetAllNamesWithNamespace(): void
3738
{
3839
$phpcsFile = $this->getCodeSnifferFile(__DIR__ . '/data/classWithNamespace.php');
39-
self::assertSame(['FooClass', 'FooInterface', 'FooTrait'], ClassHelper::getAllNames($phpcsFile));
40+
self::assertSame(['FooClass', 'FooInterface', 'FooTrait'], array_values(ClassHelper::getAllNames($phpcsFile)));
4041
}
4142

4243
public function testGetAllNamesWithoutNamespace(): void
4344
{
4445
$phpcsFile = $this->getCodeSnifferFile(__DIR__ . '/data/classWithoutNamespace.php');
45-
self::assertSame(['FooClass', 'FooInterface', 'FooTrait'], ClassHelper::getAllNames($phpcsFile));
46+
self::assertSame(['FooClass', 'FooInterface', 'FooTrait'], array_values(ClassHelper::getAllNames($phpcsFile)));
4647
}
4748

4849
public function testGetAllNamesWithNoClass(): void

0 commit comments

Comments
 (0)