I recently upgraded my Symfony project from 5.4 to 6.4.13. Now, I am encountering an error when running the Symfony translation debug php bin/console debug:translation en
command in my project. The error message is as follows:
Attempted to call an undefined method named "toString" of class "PhpParser\Node\Expr\Variable".
Project Details:
framework:
default_locale: '%env(APP_LANGUAGE)%'
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks: '%application_locales%'
Question: how to resolve this error?
The debug:translation
command parses PHP code to extract strings. In particular, it extracts strings of the form:
class::name
Here is the relevant part of the AbstractVisitor::getStringValue()
method:
if ($node instanceof Node\Expr\ClassConstFetch) {
try {
$reflection = new \ReflectionClass($node->class->toString());
$constant = $reflection->getReflectionConstant($node->name->toString());
if (false !== $constant && \is_string($constant->getValue())) {
return $constant->getValue();
}
} catch (\ReflectionException) {
}
}
The code assumes that $node->class
is a literal node and calls toString()
on it. However, one of your files contains:
$this::NAME
In that case, $node->class
is a Variable
node, which has no toString()
method, and the call throws an exception.
As a workaround, you can access the constant using the explicit class name:
YourClass::NAME