TransMethodVisitor.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Extractor\Visitor;
  11. use PhpParser\Node;
  12. use PhpParser\NodeVisitor;
  13. /**
  14. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  15. */
  16. final class TransMethodVisitor extends AbstractVisitor implements NodeVisitor
  17. {
  18. public function beforeTraverse(array $nodes): ?Node
  19. {
  20. return null;
  21. }
  22. public function enterNode(Node $node): ?Node
  23. {
  24. if (!$node instanceof Node\Expr\MethodCall && !$node instanceof Node\Expr\FuncCall) {
  25. return null;
  26. }
  27. if (!\is_string($node->name) && !$node->name instanceof Node\Identifier && !$node->name instanceof Node\Name) {
  28. return null;
  29. }
  30. $name = (string) $node->name;
  31. if ('trans' === $name || 't' === $name) {
  32. $firstNamedArgumentIndex = $this->nodeFirstNamedArgumentIndex($node);
  33. if (!$messages = $this->getStringArguments($node, 0 < $firstNamedArgumentIndex ? 0 : 'message')) {
  34. return null;
  35. }
  36. $domain = $this->getStringArguments($node, 2 < $firstNamedArgumentIndex ? 2 : 'domain')[0] ?? null;
  37. foreach ($messages as $message) {
  38. $this->addMessageToCatalogue($message, $domain, $node->getStartLine());
  39. }
  40. }
  41. return null;
  42. }
  43. public function leaveNode(Node $node): ?Node
  44. {
  45. return null;
  46. }
  47. public function afterTraverse(array $nodes): ?Node
  48. {
  49. return null;
  50. }
  51. }