You've already forked php-flasher
mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-04-05 12:32:55 +01:00
fix: validate alias attribute in PresenterCompilerPass
Add validation to ensure services tagged with 'flasher.presenter' have the required 'alias' attribute. Previously, accessing $attributes['alias'] without checking would throw an "Undefined array key" error. Now throws a clear InvalidArgumentException with a helpful message.
This commit is contained in:
@@ -20,6 +20,13 @@ final class PresenterCompilerPass implements CompilerPassInterface
|
||||
|
||||
foreach ($container->findTaggedServiceIds('flasher.presenter') as $id => $tags) {
|
||||
foreach ($tags as $attributes) {
|
||||
if (!isset($attributes['alias'])) {
|
||||
throw new \InvalidArgumentException(\sprintf(
|
||||
'Service "%s" tagged with "flasher.presenter" must have an "alias" attribute.',
|
||||
$id
|
||||
));
|
||||
}
|
||||
|
||||
$definition->addMethodCall('addPresenter', [
|
||||
$attributes['alias'],
|
||||
new ServiceClosureArgument(new Reference($id)),
|
||||
|
||||
@@ -54,4 +54,18 @@ final class PresenterCompilerPassTest extends TestCase
|
||||
|
||||
$this->assertCount(0, $definition->getMethodCalls(), 'No method calls should be made when no tagged services exist.');
|
||||
}
|
||||
|
||||
public function testProcessThrowsExceptionWhenAliasIsMissing(): void
|
||||
{
|
||||
$definition = new Definition();
|
||||
$this->container->setDefinition('flasher.response_manager', $definition);
|
||||
|
||||
// Register a service without the required 'alias' attribute
|
||||
$this->container->register('presenter_without_alias')->addTag('flasher.presenter');
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Service "presenter_without_alias" tagged with "flasher.presenter" must have an "alias" attribute.');
|
||||
|
||||
$this->compilerPass->process($this->container);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user