ConstraintVisitor.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * Code mostly comes from https://github.com/php-translation/extractor/blob/master/src/Visitor/Php/Symfony/Constraint.php
  17. */
  18. final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
  19. {
  20. public function __construct(
  21. private readonly array $constraintClassNames = [],
  22. ) {
  23. }
  24. public function beforeTraverse(array $nodes): ?Node
  25. {
  26. return null;
  27. }
  28. public function enterNode(Node $node): ?Node
  29. {
  30. return null;
  31. }
  32. public function leaveNode(Node $node): ?Node
  33. {
  34. if (!$node instanceof Node\Expr\New_ && !$node instanceof Node\Attribute) {
  35. return null;
  36. }
  37. $className = $node instanceof Node\Attribute ? $node->name : $node->class;
  38. if (!$className instanceof Node\Name) {
  39. return null;
  40. }
  41. $parts = $className->getParts();
  42. $isConstraintClass = false;
  43. foreach ($parts as $part) {
  44. if (\in_array($part, $this->constraintClassNames, true)) {
  45. $isConstraintClass = true;
  46. break;
  47. }
  48. }
  49. if (!$isConstraintClass) {
  50. return null;
  51. }
  52. $arg = $node->args[0] ?? null;
  53. if (!$arg instanceof Node\Arg) {
  54. return null;
  55. }
  56. if ($this->hasNodeNamedArguments($node)) {
  57. $messages = $this->getStringArguments($node, '/message/i', true);
  58. } else {
  59. if (!$arg->value instanceof Node\Expr\Array_) {
  60. // There is no way to guess which argument is a message to be translated.
  61. return null;
  62. }
  63. $messages = [];
  64. $options = $arg->value;
  65. /** @var Node\Expr\ArrayItem $item */
  66. foreach ($options->items as $item) {
  67. if (!$item->key instanceof Node\Scalar\String_) {
  68. continue;
  69. }
  70. if (false === stripos($item->key->value ?? '', 'message')) {
  71. continue;
  72. }
  73. if (!$item->value instanceof Node\Scalar\String_) {
  74. continue;
  75. }
  76. $messages[] = $item->value->value;
  77. break;
  78. }
  79. }
  80. foreach ($messages as $message) {
  81. $this->addMessageToCatalogue($message, 'validators', $node->getStartLine());
  82. }
  83. return null;
  84. }
  85. public function afterTraverse(array $nodes): ?Node
  86. {
  87. return null;
  88. }
  89. }