ConstraintVisitor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. foreach ($options->items as $item) {
  66. if (!$item->key instanceof Node\Scalar\String_) {
  67. continue;
  68. }
  69. if (false === stripos($item->key->value ?? '', 'message')) {
  70. continue;
  71. }
  72. if (!$item->value instanceof Node\Scalar\String_) {
  73. continue;
  74. }
  75. $messages[] = $item->value->value;
  76. break;
  77. }
  78. }
  79. foreach ($messages as $message) {
  80. $this->addMessageToCatalogue($message, 'validators', $node->getStartLine());
  81. }
  82. return null;
  83. }
  84. public function afterTraverse(array $nodes): ?Node
  85. {
  86. return null;
  87. }
  88. }