Skip to content

[Translation] Fix constant domain resolution in PhpAstExtractor #53624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function extract(iterable|string $resource, MessageCatalogue $catalogue):
{
foreach ($this->extractFiles($resource) as $file) {
$traverser = new NodeTraverser();

// This is needed to resolve namespaces in class methods/constants.
$nameResolver = new NodeVisitor\NameResolver();
$traverser->addVisitor($nameResolver);

/** @var AbstractVisitor&NodeVisitor $visitor */
foreach ($this->visitors as $visitor) {
$visitor->initialize($catalogue, $file, $this->prefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ private function getStringValue(Node $node): ?string
return $node->expr->value;
}

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) {
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function beforeTraverse(array $nodes): ?Node
}

public function enterNode(Node $node): ?Node
{
return null;
}

public function leaveNode(Node $node): ?Node
{
if (!$node instanceof Node\Expr\New_ && !$node instanceof Node\Attribute) {
return null;
Expand Down Expand Up @@ -100,11 +105,6 @@ public function enterNode(Node $node): ?Node
return null;
}

public function leaveNode(Node $node): ?Node
{
return null;
}

public function afterTraverse(array $nodes): ?Node
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function beforeTraverse(array $nodes): ?Node
}

public function enterNode(Node $node): ?Node
{
return null;
}

public function leaveNode(Node $node): ?Node
{
if (!$node instanceof Node\Expr\MethodCall && !$node instanceof Node\Expr\FuncCall) {
return null;
Expand Down Expand Up @@ -53,11 +58,6 @@ public function enterNode(Node $node): ?Node
return null;
}

public function leaveNode(Node $node): ?Node
{
return null;
}

public function afterTraverse(array $nodes): ?Node
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function beforeTraverse(array $nodes): ?Node
}

public function enterNode(Node $node): ?Node
{
return null;
}

public function leaveNode(Node $node): ?Node
{
if (!$node instanceof Node\Expr\New_) {
return null;
Expand Down Expand Up @@ -53,11 +58,6 @@ public function enterNode(Node $node): ?Node
return null;
}

public function leaveNode(Node $node): ?Node
{
return null;
}

public function afterTraverse(array $nodes): ?Node
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

final class PhpAstExtractorTest extends TestCase
{
public const OTHER_DOMAIN = 'not_messages';

/**
* @dataProvider resourcesProvider
*/
Expand Down Expand Up @@ -124,6 +126,7 @@ public function testExtraction(iterable|string $resource)
'variable-assignation-inlined-with-named-arguments-in-trans-method' => 'prefixvariable-assignation-inlined-with-named-arguments-in-trans-method',
'mix-named-arguments-without-parameters' => 'prefixmix-named-arguments-without-parameters',
'mix-named-arguments-disordered' => 'prefixmix-named-arguments-disordered',
'const-domain' => 'prefixconst-domain',
],
'validators' => [
'message-in-constraint-attribute' => 'prefixmessage-in-constraint-attribute',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@
<?php echo $view['translator']->trans('mix-named-arguments-disordered', domain: 'not_messages', parameters: []); ?>

<?php echo $view['translator']->trans(...); // should not fail ?>

<?php
use Symfony\Component\Translation\Tests\Extractor\PhpAstExtractorTest;
echo $view['translator']->trans('const-domain', [], PhpAstExtractorTest::OTHER_DOMAIN);
?>