{"record":{"id":"517200b2f05f89c2","repo":"twigphp/Twig","slug":"block-chain-templates-must-be-strings-or-s-instances-s-given","errorCode":null,"errorMessage":"Block chain templates must be strings or \"%s\" instances, \"%s\" given.","messagePattern":"Block chain templates must be strings or \"(.+?)\" instances, \"(.+?)\" given\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/BlockChain.php","lineNumber":49,"sourceCode":"     * Whether the lineage can no longer move, making every further resolution pointless.\n     */\n    private bool $fixed = false;\n\n    /**\n     * @param iterable<string|TemplateWrapper> $templates Templates ordered from highest to lowest precedence\n     * @param array<string, mixed>             $context   Default variables used to resolve dynamic parent expressions\n     */\n    public function __construct(\n        private Environment $env,\n        iterable $templates,\n        private array $context = [],\n    ) {\n        foreach ($templates as $template) {\n            if (\\is_string($template)) {\n                $template = $env->load($template);\n            }\n            if (!$template instanceof TemplateWrapper) {\n                throw new \\TypeError(\\sprintf('Block chain templates must be strings or \"%s\" instances, \"%s\" given.', TemplateWrapper::class, get_debug_type($template)));\n            }\n\n            $template = $template->unwrap();\n            if (!$template->isOwnedBy($env)) {\n                throw new \\LogicException('A block chain cannot contain templates from different Twig environments.');\n            }\n\n            $this->templates[] = $template;\n        }\n\n        if (!$this->templates) {\n            throw new \\InvalidArgumentException('A block chain requires at least one template.');\n        }\n    }\n\n    /**\n     * @param array<string, mixed> $context\n     */","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/BlockChain.php#L31-L67","documentation":"BlockChain's constructor accepts an iterable of templates, each either a string template name or a TemplateWrapper. If an element is neither (e.g. a raw Template object, null, or an array), a TypeError is thrown reporting the actual debug type via get_debug_type(). The chain needs wrappers it can unwrap and verify against the Environment.","triggerScenarios":"Passing a \\Twig\\Template instance directly instead of a TemplateWrapper, passing null/false, or iterating a list that includes non-template values into Twig\\BlockChain::__construct.","commonSituations":"Calling ->unwrap() yourself before passing to BlockChain, or mixing results from $env->resolveTemplate() (returns Template) with $env->load() (returns TemplateWrapper).","solutions":["Pass template name strings (they are loaded via $env->load internally) or TemplateWrapper instances from Environment::load().","If you only have a Template, load its wrapper via the environment instead of passing the raw Template.","Add a per-item check with instanceof TemplateWrapper before constructing the chain."],"exampleFix":"// before\n$chain = new \\Twig\\BlockChain($env, [$env->resolveTemplate('a.html.twig')]); // raw Template\n// after\n$chain = new \\Twig\\BlockChain($env, ['a.html.twig']); // or [$env->load('a.html.twig')]","handlingStrategy":"type-guard","validationCode":"foreach ($templates as $t) {\n    if (!is_string($t) && !$t instanceof \\Twig\\TemplateWrapper) {\n        throw new \\InvalidArgumentException(sprintf('Expected string or TemplateWrapper, got %s.', get_debug_type($t)));\n    }\n}","typeGuard":"function isChainTemplate(mixed $t): bool {\n    return is_string($t) || $t instanceof \\Twig\\TemplateWrapper;\n}","tryCatchPattern":"try {\n    $chain = new \\Twig\\BlockChain($env, $templates);\n} catch (\\TypeError $e) {\n    // normalize: map non-string entries through $env->load() or drop them\n}","preventionTips":["Use Environment::load() / template name strings, never raw \\Twig\\Template objects.","Do not call ->unwrap() before constructing the chain.","Type-hint collections as list<string|TemplateWrapper> in your own APIs feeding BlockChain."],"tags":["php","twig","type-error","argument-validation"],"backgroundTag":"type-mismatch","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"}