1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[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 Translation\Extractor\Visitor\Php\Symfony; |
13
|
|
|
|
14
|
|
|
use PhpParser\Node; |
15
|
|
|
use PhpParser\NodeVisitor; |
16
|
|
|
use Translation\Extractor\Model\SourceLocation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Rein Baarsma <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class FormTypeChoices extends AbstractFormType implements NodeVisitor |
22
|
|
|
{ |
23
|
|
|
use FormTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int defaults to major version 3 |
27
|
|
|
*/ |
28
|
|
|
protected $symfonyMajorVersion = 3; |
29
|
|
|
|
30
|
|
|
private $variables = []; |
31
|
|
|
|
32
|
|
|
private $state; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param int $sfMajorVersion |
36
|
|
|
*/ |
37
|
2 |
|
public function setSymfonyMajorVersion($sfMajorVersion) |
38
|
|
|
{ |
39
|
2 |
|
$this->symfonyMajorVersion = $sfMajorVersion; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
8 |
|
public function enterNode(Node $node) |
43
|
|
|
{ |
44
|
8 |
|
if (!$this->isFormType($node)) { |
45
|
8 |
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
parent::enterNode($node); |
49
|
|
|
|
50
|
8 |
|
if (null === $this->state && $node instanceof Node\Expr\Assign) { |
51
|
4 |
|
$this->state = 'variable'; |
52
|
8 |
|
} elseif ('variable' === $this->state && $node instanceof Node\Expr\Variable) { |
53
|
4 |
|
$this->variables['__variable-name'] = $node->name; |
54
|
4 |
|
$this->state = 'value'; |
55
|
8 |
|
} elseif ('value' === $this->state && $node instanceof Node\Expr\Array_) { |
56
|
2 |
|
$this->variables[$this->variables['__variable-name']] = $node; |
57
|
2 |
|
$this->state = null; |
58
|
|
|
} else { |
59
|
8 |
|
$this->state = null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// symfony 3 or 4 displays key by default, where symfony 2 displays value |
63
|
8 |
|
$useKey = 2 !== $this->symfonyMajorVersion; |
64
|
|
|
|
65
|
|
|
// remember choices in this node |
66
|
8 |
|
$choicesNodes = []; |
67
|
|
|
|
68
|
|
|
// loop through array |
69
|
8 |
|
if (!$node instanceof Node\Expr\Array_) { |
70
|
8 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
$domain = null; |
74
|
8 |
|
foreach ($node->items as $item) { |
75
|
8 |
|
if (!$item->key instanceof Node\Scalar\String_) { |
76
|
3 |
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
8 |
|
if ('choices_as_values' === $item->key->value) { |
80
|
2 |
|
$useKey = true; |
81
|
|
|
|
82
|
2 |
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
View Code Duplication |
if ('choice_translation_domain' === $item->key->value) { |
|
|
|
|
86
|
2 |
|
if ($item->value instanceof Node\Scalar\String_) { |
87
|
2 |
|
$domain = $item->value->value; |
88
|
2 |
|
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) { |
89
|
2 |
|
$domain = false; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
if ('choices' !== $item->key->value) { |
96
|
7 |
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
$choicesNodes[] = $item->value; |
100
|
|
|
} |
101
|
|
|
|
102
|
8 |
|
if (0 === count($choicesNodes) || false === $domain) { |
103
|
8 |
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// probably will be only 1, but who knows |
107
|
7 |
|
foreach ($choicesNodes as $choices) { |
108
|
7 |
|
if ($choices instanceof Node\Expr\Variable && isset($this->variables[$choices->name])) { |
109
|
2 |
|
$choices = $this->variables[$choices->name]; |
110
|
6 |
|
} elseif (!$choices instanceof Node\Expr\Array_) { |
111
|
|
|
$this->addError($choices, 'Form choice is not an array'); |
112
|
|
|
|
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
7 |
|
foreach ($choices->items as $citem) { |
117
|
7 |
|
$labelNode = $useKey ? $citem->key : $citem->value; |
118
|
7 |
|
if (!$labelNode instanceof Node\Scalar\String_) { |
119
|
3 |
|
$this->addError($citem, 'Choice label is not a scalar string'); |
120
|
|
|
|
121
|
3 |
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
7 |
|
$this->lateCollect(new SourceLocation($labelNode->value, $this->getAbsoluteFilePath(), $choices->getAttribute('startLine'), ['domain' => $domain])); |
125
|
|
|
} |
126
|
|
|
} |
127
|
7 |
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.