|
13 | 13 |
|
14 | 14 | use PhpParser\Node; |
15 | 15 | use PhpParser\NodeVisitor; |
16 | | -use Translation\Extractor\Visitor\Php\BasePHPVisitor; |
17 | 16 |
|
18 | 17 | /** |
19 | 18 | * @author Tobias Nyholm <[email protected]> |
20 | 19 | */ |
21 | | -final class FormTypePlaceholder extends BasePHPVisitor implements NodeVisitor |
| 20 | +final class FormTypePlaceholder extends AbstractFormType implements NodeVisitor |
22 | 21 | { |
23 | 22 | use FormTrait; |
24 | 23 |
|
| 24 | + private $arrayNodeVisited = []; |
| 25 | + |
25 | 26 | public function enterNode(Node $node) |
26 | 27 | { |
27 | 28 | if (!$this->isFormType($node)) { |
28 | 29 | return; |
29 | 30 | } |
| 31 | + parent::enterNode($node); |
30 | 32 |
|
31 | 33 | if (!$node instanceof Node\Expr\Array_) { |
32 | 34 | return; |
33 | 35 | } |
34 | 36 |
|
| 37 | + $placeholderNode = null; |
| 38 | + $domain = null; |
35 | 39 | foreach ($node->items as $item) { |
36 | 40 | if (!$item->key instanceof Node\Scalar\String_) { |
37 | 41 | continue; |
38 | 42 | } |
39 | | - |
40 | | - if ('placeholder' === $item->key->value) { |
| 43 | + if ('translation_domain' === $item->key->value) { |
| 44 | + // Try to find translation domain |
41 | 45 | if ($item->value instanceof Node\Scalar\String_) { |
42 | | - $line = $item->value->getAttribute('startLine'); |
43 | | - $this->addLocation($item->value->value, $line, $item); |
| 46 | + $domain = $item->value->value; |
44 | 47 | } elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) { |
45 | | - // 'placeholder' => false, |
46 | | - // Do noting |
47 | | - } else { |
48 | | - $this->addError($item, 'Form placeholder is not a scalar string'); |
| 48 | + $domain = false; |
| 49 | + } |
| 50 | + } elseif ('placeholder' === $item->key->value) { |
| 51 | + $placeholderNode = $item; |
| 52 | + } elseif ('attr' === $item->key->value) { |
| 53 | + foreach ($item->value->items as $attrValue) { |
| 54 | + if ('placeholder' === $attrValue->key->value) { |
| 55 | + $placeholderNode = $attrValue; |
| 56 | + |
| 57 | + break; |
| 58 | + } |
49 | 59 | } |
50 | 60 | } |
51 | 61 | } |
52 | | - } |
53 | | - |
54 | | - public function leaveNode(Node $node) |
55 | | - { |
56 | | - } |
57 | 62 |
|
58 | | - public function beforeTraverse(array $nodes) |
59 | | - { |
60 | | - } |
| 63 | + if (null !== $placeholderNode) { |
| 64 | + /** |
| 65 | + * Make sure we do not visit the same placeholder node twice. |
| 66 | + */ |
| 67 | + $hash = spl_object_hash($placeholderNode); |
| 68 | + if (isset($this->arrayNodeVisited[$hash])) { |
| 69 | + return; |
| 70 | + } |
| 71 | + $this->arrayNodeVisited[$hash] = true; |
61 | 72 |
|
62 | | - public function afterTraverse(array $nodes) |
63 | | - { |
| 73 | + if ($placeholderNode->value instanceof Node\Scalar\String_) { |
| 74 | + $line = $placeholderNode->value->getAttribute('startLine'); |
| 75 | + if (null !== $location = $this->getLocation($placeholderNode->value->value, $line, $placeholderNode, ['domain' => $domain])) { |
| 76 | + $this->lateCollect($location); |
| 77 | + } |
| 78 | + } elseif ($placeholderNode->value instanceof Node\Expr\ConstFetch && 'false' === $placeholderNode->value->name->toString()) { |
| 79 | + // 'placeholder' => false, |
| 80 | + // Do noting |
| 81 | + } else { |
| 82 | + $this->addError($placeholderNode, 'Form placeholder is not a scalar string'); |
| 83 | + } |
| 84 | + } |
64 | 85 | } |
65 | 86 | } |
0 commit comments