TwigStan analyzes Twig templates using PHPStan. Pushword core provides Pushword\Core\TwigStan\ResolveViewNodeVisitor to make literal view() calls resolvable during analysis. It uses the selected site’s normal template resolver, including host overrides, themes and fallback namespaces.
The visitor is activated explicitly in the analysis loader. It is not registered in the application’s Twig environment and does not add a TwigStan dependency to core.
Install the development package alongside Pushword core 1.0.108 or later:
composer require --dev pushword/twigstan:^1.0.109
Follow the package setup: a small analysis kernel outside src/, a loader, and twigstan.php selecting PHP sources and templates. The package preserves the project’s kernel configuration, enables its analysis service container and keeps its cache under .twigstan/. Add that directory to .gitignore.
The analyzer dependencies are locked inside the package. Their installation does not change the site’s PHPStan or its extension installer. Only extensions explicitly included in the template-analysis PHPStan configuration are loaded. Shared application libraries retain autoload priority.
Run from the project root:
vendor/bin/pushword-twigstan
PUSHWORD_TWIGSTAN_HOST=example.org vendor/bin/pushword-twigstan
Literal Twig view() and PHP getView() calls use the selected site’s real resolver, including host overrides, themes and fallback namespaces. The loader switches the request context to that host; unknown hosts fail explicitly. Run each host separately rather than treating one site’s results as coverage of all sites.
Dynamic template names, including Pushword’s generic page renderer, and Twig embedded in page content still require additional context discovery. Scanning a file does not establish coverage when no render context is known.
Start with explicit template files having known PHP render points. A separate twigstan-ci.php can select this narrower list. Run:
vendor/bin/pushword-twigstan --configuration=twigstan-ci.php --check-coverage
The command fails on analyzer errors and on any entry without a collected PHP render context. Its final report counts each covered template and lists missing ones. The coverage mode expects template files rather than directories.
The monorepo uses the same package through thin wrappers under .scripts/twigstan/:
composer twigstan
composer twigstan -- packages/core/src/templates/user/forgot_password.html.twig
composer twigstan-ci
composer test-twigstan
composer twigstan-ci checks the 22 entries in .scripts/twigstan/ci.php, plus their dependencies, at level 8. These cover login and password forms, links, cards, videos, conversation notifications, static-server configuration and progress fragments. There is no baseline or ignored error in this scope.
The package’s isolated tool pins TwigStan 0.2.8, PHPStan 2.3.6 and Twig 3.30.0 without local patches. TwigStan includes the inherited parent block context fix (#328).
Attribute access on a value known to always be null is intentionally reported, including behind ??, |default and is defined (#329). If the value can be an object, declare its actual nullable type at the PHP render point. If it is always null, remove the unreachable attribute access. Fifteen regression cases check inherited contexts, nullable objects, runtime fallbacks and real errors.
For an upstream upgrade, update the pinned constraint in packages/twigstan/tools/composer.json, then run:
composer --working-dir=packages/twigstan/tools update twigstan/twigstan
composer test-twigstan
Commit the dependency lock with the constraint change. PAO is disabled in analysis subprocesses because its PHPStan formatter replaces TwigStan’s result files.
The full analysis remains outside required CI. EasyAdmin templates, form themes, embedded templates and generated optional-value guards can report diagnostics caused by incomplete context propagation or library type declarations. Check the PHP render context and runtime behavior before changing a template. The gallery remains outside TwigStan CI because of generated optional-attribute guards; its rendering behavior is covered by runtime tests.