AbstractVisitor.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Symfony\Component\Translation\MessageCatalogue;
  13. /**
  14. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  15. */
  16. abstract class AbstractVisitor
  17. {
  18. private MessageCatalogue $catalogue;
  19. private \SplFileInfo $file;
  20. private string $messagePrefix;
  21. public function initialize(MessageCatalogue $catalogue, \SplFileInfo $file, string $messagePrefix): void
  22. {
  23. $this->catalogue = $catalogue;
  24. $this->file = $file;
  25. $this->messagePrefix = $messagePrefix;
  26. }
  27. protected function addMessageToCatalogue(string $message, ?string $domain, int $line): void
  28. {
  29. $domain ??= 'messages';
  30. $this->catalogue->set($message, $this->messagePrefix.$message, $domain);
  31. $metadata = $this->catalogue->getMetadata($message, $domain) ?? [];
  32. $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $this->file);
  33. $metadata['sources'][] = $normalizedFilename.':'.$line;
  34. $this->catalogue->setMetadata($message, $metadata, $domain);
  35. }
  36. protected function getStringArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node, int|string $index, bool $indexIsRegex = false): array
  37. {
  38. if (\is_string($index)) {
  39. return $this->getStringNamedArguments($node, $index, $indexIsRegex);
  40. }
  41. $args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
  42. if (!($arg = $args[$index] ?? null) instanceof Node\Arg) {
  43. return [];
  44. }
  45. return (array) $this->getStringValue($arg->value);
  46. }
  47. protected function hasNodeNamedArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node): bool
  48. {
  49. $args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
  50. foreach ($args as $arg) {
  51. if ($arg instanceof Node\Arg && null !== $arg->name) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. protected function nodeFirstNamedArgumentIndex(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node): int
  58. {
  59. $args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
  60. foreach ($args as $i => $arg) {
  61. if ($arg instanceof Node\Arg && null !== $arg->name) {
  62. return $i;
  63. }
  64. }
  65. return \PHP_INT_MAX;
  66. }
  67. private function getStringNamedArguments(Node\Expr\CallLike|Node\Attribute $node, string $argumentName = null, bool $isArgumentNamePattern = false): array
  68. {
  69. $args = $node instanceof Node\Expr\CallLike ? $node->getArgs() : $node->args;
  70. $argumentValues = [];
  71. foreach ($args as $arg) {
  72. if (!$isArgumentNamePattern && $arg->name?->toString() === $argumentName) {
  73. $argumentValues[] = $this->getStringValue($arg->value);
  74. } elseif ($isArgumentNamePattern && preg_match($argumentName, $arg->name?->toString() ?? '') > 0) {
  75. $argumentValues[] = $this->getStringValue($arg->value);
  76. }
  77. }
  78. return array_filter($argumentValues);
  79. }
  80. private function getStringValue(Node $node): ?string
  81. {
  82. if ($node instanceof Node\Scalar\String_) {
  83. return $node->value;
  84. }
  85. if ($node instanceof Node\Expr\BinaryOp\Concat) {
  86. if (null === $left = $this->getStringValue($node->left)) {
  87. return null;
  88. }
  89. if (null === $right = $this->getStringValue($node->right)) {
  90. return null;
  91. }
  92. return $left.$right;
  93. }
  94. if ($node instanceof Node\Expr\Assign && $node->expr instanceof Node\Scalar\String_) {
  95. return $node->expr->value;
  96. }
  97. return null;
  98. }
  99. }