Skip to content

Commit 81bf869

Browse files
Move reflection out of SorterBuilderKeyIterator
1 parent 92cb0d9 commit 81bf869

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

src/DependencyInjection/CompilerPass/SorterBuilderKeyIterator.php

-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use ArrayIterator;
88
use FilterIterator;
9-
use ReflectionClass;
109
use ReflectionMethod;
1110

1211
use function assert;
@@ -19,20 +18,10 @@
1918
* @template TKey
2019
* @template T
2120
* @template TIterator as ArrayIterator<int, ReflectionMethod>
22-
*
2321
* @extends FilterIterator<TKey, T, TIterator>
2422
*/
2523
class SorterBuilderKeyIterator extends FilterIterator
2624
{
27-
/** @phpstan-param class-string $className */
28-
public function __construct(string $className)
29-
{
30-
$reflectionClass = new ReflectionClass($className);
31-
$methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
32-
33-
parent::__construct(new ArrayIterator($methods));
34-
}
35-
3625
public function accept(): bool
3726
{
3827
return ! in_array($this->current(), [

src/DependencyInjection/Configuration.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
namespace Budgegeria\Bundle\IntlBundle\DependencyInjection;
66

7+
use ArrayIterator;
78
use Budgegeria\Bundle\IntlBundle\DependencyInjection\CompilerPass\SorterBuilderKeyIterator;
89
use Budgegeria\IntlSort\Builder;
10+
use ReflectionClass;
11+
use ReflectionMethod;
912
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1013
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1114
use Symfony\Component\Config\Definition\ConfigurationInterface;
@@ -39,7 +42,8 @@ private function buildSorter(NodeDefinition $node): void
3942
->arrayPrototype()
4043
->children();
4144

42-
foreach (new SorterBuilderKeyIterator(Builder::class) as $sorterConfigNames) {
45+
$methods = new ArrayIterator($this->getClassMethods(Builder::class));
46+
foreach (new SorterBuilderKeyIterator($methods) as $sorterConfigNames) {
4347
$sorterChildren->scalarNode($sorterConfigNames)->end();
4448
}
4549

@@ -50,4 +54,16 @@ private function buildSorter(NodeDefinition $node): void
5054
->end()
5155
->end();
5256
}
57+
58+
/**
59+
* @phpstan-param class-string $className
60+
*
61+
* @phpstan-return list<ReflectionMethod>
62+
*/
63+
private function getClassMethods(string $className): array
64+
{
65+
$reflectionClass = new ReflectionClass($className);
66+
67+
return $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
68+
}
5369
}

tests/DependencyInjection/CompilerPass/SorterBuilderKeyIteratorTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace Budgegeria\Bundle\IntlBundle\Tests\DependencyInjection\CompilerPass;
66

7+
use ArrayIterator;
78
use Budgegeria\Bundle\IntlBundle\DependencyInjection\CompilerPass\SorterBuilderKeyIterator;
89
use Budgegeria\IntlSort\Builder;
910
use PHPUnit\Framework\TestCase;
11+
use ReflectionClass;
12+
use ReflectionMethod;
1013

1114
use function array_values;
1215
use function iterator_to_array;
@@ -15,7 +18,9 @@ class SorterBuilderKeyIteratorTest extends TestCase
1518
{
1619
public function testIterationContent(): void
1720
{
18-
$sorterKeys = iterator_to_array(new SorterBuilderKeyIterator(Builder::class));
21+
$sorterKeys = iterator_to_array(new SorterBuilderKeyIterator(
22+
new ArrayIterator($this->getMethods(Builder::class)),
23+
));
1924
$expected = [
2025
'enable_french_collation',
2126
'disable_french_collation',
@@ -48,4 +53,16 @@ public function testIterationContent(): void
4853

4954
self::assertEquals($expected, array_values($sorterKeys));
5055
}
56+
57+
/**
58+
* @phpstan-param class-string $className
59+
*
60+
* @phpstan-return list<ReflectionMethod>
61+
*/
62+
private function getMethods(string $className): array
63+
{
64+
$reflectionClass = new ReflectionClass($className);
65+
66+
return $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
67+
}
5168
}

0 commit comments

Comments
 (0)