Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Extractor/Visitor/AbstractVisitor.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Translation\Extractor\Visitor;
13
14 use PhpParser\Node;
15 use Symfony\Component\Translation\MessageCatalogue;
16
17 /**
18 * @author Mathieu Santostefano <[email protected]>
19 */
20 abstract class AbstractVisitor
21 {
22 private MessageCatalogue $catalogue;
23 private \SplFileInfo $file;
24 private string $messagePrefix;
25
26 public function initialize(MessageCatalogue $catalogue, \SplFileInfo $file, string $messagePrefix): void
27 {
28 $this->catalogue = $catalogue;
29 $this->file = $file;
30 $this->messagePrefix = $messagePrefix;
31 }
32
33 protected function addMessageToCatalogue(string $message, ?string $domain, int $line): void
34 {
35 $domain ??= 'messages';
36 $this->catalogue->set($message, $this->messagePrefix.$message, $domain);
37 $metadata = $this->catalogue->getMetadata($message, $domain) ?? [];
38 $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $this->file);
39 $metadata['sources'][] = $normalizedFilename.':'.$line;
40 $this->catalogue->setMetadata($message, $metadata, $domain);
41 }
42
43 protected function getStringArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node, int|string $index, bool $indexIsRegex = false): array
44 {
45 if (\is_string($index)) {
46 return $this->getStringNamedArguments($node, $index, $indexIsRegex);
47 }
48
49 $args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
50
51 if (!($arg = $args[$index] ?? null) instanceof Node\Arg) {
52 return [];
53 }
54
55 return (array) $this->getStringValue($arg->value);
56 }
57
58 protected function hasNodeNamedArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node): bool
59 {
60 $args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
61
62 foreach ($args as $arg) {
63 if ($arg instanceof Node\Arg && null !== $arg->name) {
64 return true;
65 }
66 }
67
68 return false;
69 }
70
71 protected function nodeFirstNamedArgumentIndex(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node): int
72 {
73 $args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
74
75 foreach ($args as $i => $arg) {
76 if ($arg instanceof Node\Arg && null !== $arg->name) {
77 return $i;
78 }
79 }
80
81 return \PHP_INT_MAX;
82 }
83
84 private function getStringNamedArguments(Node\Expr\CallLike|Node\Attribute $node, ?string $argumentName = null, bool $isArgumentNamePattern = false): array
85 {
86 $args = $node instanceof Node\Expr\CallLike ? $node->getArgs() : $node->args;
87 $argumentValues = [];
88
89 foreach ($args as $arg) {
90 if (!$isArgumentNamePattern && $arg->name?->toString() === $argumentName) {
91 $argumentValues[] = $this->getStringValue($arg->value);
92 } elseif ($isArgumentNamePattern && preg_match($argumentName, $arg->name?->toString() ?? '') > 0) {
93 $argumentValues[] = $this->getStringValue($arg->value);
94 }
95 }
96
97 return array_filter($argumentValues);
98 }
99
100 private function getStringValue(Node $node): ?string
101 {
102 if ($node instanceof Node\Scalar\String_) {
103 return $node->value;
104 }
105
106 if ($node instanceof Node\Expr\BinaryOp\Concat) {
107 if (null === $left = $this->getStringValue($node->left)) {
108 return null;
109 }
110
111 if (null === $right = $this->getStringValue($node->right)) {
112 return null;
113 }
114
115 return $left.$right;
116 }
117
118 if ($node instanceof Node\Expr\Assign && $node->expr instanceof Node\Scalar\String_) {
119 return $node->expr->value;
120 }
121
122 if ($node instanceof Node\Expr\ClassConstFetch) {
123 try {
124 $reflection = new \ReflectionClass($node->class->toString());
125 $constant = $reflection->getReflectionConstant($node->name->toString());
126 if (false !== $constant && \is_string($constant->getValue())) {
127 return $constant->getValue();
128 }
129 } catch (\ReflectionException) {
130 }
131 }
132
133 return null;
134 }
135 }