8000 [Translation] Fix constant domain resolution in PhpAstExtractor by VincentLanglet · Pull Request #53623 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Fix constant domain resolution in PhpAstExtractor #53623

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

Closed
Closed
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,13 +46,20 @@ public function extract(iterable|string $resource, MessageCatalogue $catalogue):
{
foreach ($this->extractFiles($resource) as $file) {
$traverser = new NodeTraverser();

$nodes = $this->parser->parse(file_get_contents($file));

// First run is needed to resolve namespaces in class methods/constants.
$traverser->addVisitor(new NodeVisitor\NameResolver());
$traverser->traverse($nodes);

/** @var AbstractVisitor&NodeVisitor $visitor */
foreach ($this->visitors as $visitor) {
$visitor->initialize($catalogue, $file, $this->prefix);
$traverser->addVisitor($visitor);
}

$nodes = $this->parser->parse(file_get_contents($file));
// Second run is used for the custom visitors.
$traverser->traverse($nodes);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ 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 @@ -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);
?>
0