{"record":{"id":"ef6d4ae73aab886f","repo":"twigphp/Twig","slug":"test-s-is-already-registered","errorCode":null,"errorMessage":"Test \"%s\" is already registered.","messagePattern":"Test \"(.+?)\" is already registered\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Extension/StagingExtension.php","lineNumber":90,"sourceCode":"\n    public function addTokenParser(TokenParserInterface $parser): void\n    {\n        if (isset($this->tokenParsers[$parser->getTag()])) {\n            throw new \\LogicException(\\sprintf('Tag \"%s\" is already registered.', $parser->getTag()));\n        }\n\n        $this->tokenParsers[$parser->getTag()] = $parser;\n    }\n\n    public function getTokenParsers(): array\n    {\n        return $this->tokenParsers;\n    }\n\n    public function addTest(TwigTest $test): void\n    {\n        if (isset($this->tests[$test->getName()])) {\n            throw new \\LogicException(\\sprintf('Test \"%s\" is already registered.', $test->getName()));\n        }\n\n        $this->tests[$test->getName()] = $test;\n    }\n\n    public function getTests(): array\n    {\n        return $this->tests;\n    }\n}\n","sourceCodeStart":72,"sourceCodeEnd":101,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/StagingExtension.php#L72-L101","documentation":"StagingExtension stores user-registered TwigTests (the `is ...` tests) by name. Twig throws this LogicException when addTest is called with a test whose name is already taken, since test names must be unique for expression resolution to be deterministic.","triggerScenarios":"Calling $twig->addTest(new TwigTest('foo', ...)) twice, or adding two tests with the same name — e.g. two extensions both defining a test named 'same', or a custom test colliding with one from a core extension.","commonSituations":"Installing two bundles/extensions exporting the same test name; registering the same test in a shared bootstrap executed twice; adding a test whose name collides with a built-in like 'empty' or 'null'.","solutions":["Rename your custom test to a unique name, or drop the duplicate registration.","Check which extension already defines the test with that name and pick a single source of truth.","Guard registration code with a container/service singleton or a static flag so it runs once."],"exampleFix":"// before\n$twig->addTest(new TwigTest('bundle', fn() => true));\n$twig->addTest(new TwigTest('bundle', fn() => false)); // 'bundle' already registered\n\n// after\n$twig->addTest(new TwigTest('my_bundle', fn() => true));","handlingStrategy":"try-catch","validationCode":"$knownTests = array_column($twig->getTests() ?? [], null);\nif (array_key_exists($test->getName(), (array) $knownTests)) { /* skip or rename */ }","typeGuard":null,"tryCatchPattern":"try { $twig->addTest($test); } catch (\\LogicException $e) {\n    if (str_contains($e->getMessage(), 'is already registered')) { /* duplicate, skip */ }\n    else { throw $e; }\n}","preventionTips":["Prefix custom test names with a project namespace to avoid collisions.","Register each test once from a single Extension class.","Check composer-installed Twig extensions for overlapping test names.","Avoid shadowing built-in test names (empty, null, iterable, etc.)."],"tags":["twig","extension","test","duplicate-registration"],"backgroundTag":"conflicting-config-options","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}