ConstraintVisitor.php 3.0 KB

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