Skip to content

Commit

Permalink
Use array_values for list of filters/functions/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 24, 2024
1 parent aa4f4cb commit f634240
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/Extension/AttributeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private function initFromAttributes()
foreach ($reflectionClass->getMethods() as $method) {
// Filters
foreach ($method->getAttributes(AsTwigFilter::class) as $attribute) {
/** @var AsTwigFilter $attribute */
$attribute = $attribute->newInstance();

$name = $attribute->name;
Expand All @@ -116,6 +117,7 @@ private function initFromAttributes()

// Functions
foreach ($method->getAttributes(AsTwigFunction::class) as $attribute) {
/** @var AsTwigFunction $attribute */
$attribute = $attribute->newInstance();

$name = $attribute->name;
Expand All @@ -139,6 +141,7 @@ private function initFromAttributes()

// Tests
foreach ($method->getAttributes(AsTwigTest::class) as $attribute) {
/** @var AsTwigTest $attribute */
$attribute = $attribute->newInstance();

$name = $attribute->name;
Expand All @@ -155,8 +158,8 @@ private function initFromAttributes()
}

// Assign all at the end to avoid inconsistent state in case of exception
$this->filters = $filters;
$this->functions = $functions;
$this->tests = $tests;
$this->filters = array_values($filters);
$this->functions = array_values($functions);
$this->tests = array_values($tests);
}
}
6 changes: 3 additions & 3 deletions tests/Extension/AttributeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public function testRuntimeExtension()
$class = ExtensionWithAttributes::class;
$extension = new AttributeExtension([$class]);

$this->assertSame([$class, 'fooFilter'], $extension->getFilters()['foo']->getCallable());
$this->assertSame([$class, 'fooFunction'], $extension->getFunctions()['foo']->getCallable());
$this->assertSame([$class, 'fooTest'], $extension->getTests()['foo']->getCallable());
$this->assertSame([$class, 'fooFilter'], $extension->getFilters()[0]->getCallable());
$this->assertSame([$class, 'fooFunction'], $extension->getFunctions()[0]->getCallable());
$this->assertSame([$class, 'fooTest'], $extension->getTests()[0]->getCallable());
}
}

0 comments on commit f634240

Please sign in to comment.