File tree Expand file tree Collapse file tree 14 files changed +103
-43
lines changed
Functional/Visitor/Php/Symfony Expand file tree Collapse file tree 14 files changed +103
-43
lines changed Original file line number Diff line number Diff line change 3737 },
3838 "extra" : {
3939 "branch-alias" : {
40- "dev-master" : " 1.2 -dev"
40+ "dev-master" : " 1.4 -dev"
4141 }
4242 }
4343}
Original file line number Diff line number Diff line change 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 \Node \Stmt ;
16+
17+ trait FormTrait
18+ {
19+ private $ isFormType = false ;
20+
21+ /**
22+ * Check if this node is a form type.
23+ *
24+ * @param Node $node
25+ *
26+ * @return bool
27+ */
28+ private function isFormType (Node $ node )
29+ {
30+ // only Traverse *Type
31+ if ($ node instanceof Stmt \Class_) {
32+ $ this ->isFormType = 'Type ' === substr ($ node ->name , -4 );
33+ }
34+
35+ return $ this ->isFormType ;
36+ }
37+ }
Original file line number Diff line number Diff line change 1212namespace Translation \Extractor \Visitor \Php \Symfony ;
1313
1414use PhpParser \Node ;
15- use PhpParser \Node \Stmt ;
1615use PhpParser \NodeVisitor ;
1716use Translation \Extractor \Model \SourceLocation ;
1817use Translation \Extractor \Visitor \Php \BasePHPVisitor ;
2221 */
2322final class FormTypeChoices extends BasePHPVisitor implements NodeVisitor
2423{
24+ use FormTrait;
25+
2526 /**
2627 * @var int defaults to major version 3
2728 */
@@ -41,11 +42,8 @@ public function setSymfonyMajorVersion($sfMajorVersion)
4142
4243 public function enterNode (Node $ node )
4344 {
44- // only Traverse *Type
45- if ($ node instanceof Stmt \Class_) {
46- if ('Type ' !== substr ($ node ->name , -4 )) {
47- return ;
48- }
45+ if (!$ this ->isFormType ($ node )) {
46+ return ;
4947 }
5048
5149 if (null === $ this ->state && $ node instanceof Node \Expr \Assign) {
@@ -60,8 +58,8 @@ public function enterNode(Node $node)
6058 $ this ->state = null ;
6159 }
6260
63- // symfony 3 displays key by default, where symfony 2 displays value
64- $ useKey = 3 = == $ this ->symfonyMajorVersion ;
61+ // symfony 3 or 4 displays key by default, where symfony 2 displays value
62+ $ useKey = 2 ! == $ this ->symfonyMajorVersion ;
6563
6664 // remember choices in this node
6765 $ choicesNodes = [];
Original file line number Diff line number Diff line change 1212namespace Translation \Extractor \Visitor \Php \Symfony ;
1313
1414use PhpParser \Node ;
15- use PhpParser \Node \Stmt ;
1615use PhpParser \NodeVisitor ;
1716use Translation \Extractor \Visitor \Php \BasePHPVisitor ;
1817
2120 */
2221final class FormTypeEmptyValue extends BasePHPVisitor implements NodeVisitor
2322{
23+ use FormTrait;
24+
2425 public function enterNode (Node $ node )
2526 {
26- // only Traverse *Type
27- if ($ node instanceof Stmt \Class_) {
28- if ('Type ' !== substr ($ node ->name , -4 )) {
29- return ;
30- }
27+ if (!$ this ->isFormType ($ node )) {
28+ return ;
3129 }
3230
3331 if (!$ node instanceof Node \Expr \Array_) {
Original file line number Diff line number Diff line change 1212namespace Translation \Extractor \Visitor \Php \Symfony ;
1313
1414use PhpParser \Node ;
15- use PhpParser \Node \Stmt ;
1615use PhpParser \NodeVisitor ;
1716use Translation \Extractor \Visitor \Php \BasePHPVisitor ;
1817
2120 */
2221final class FormTypeInvalidMessage extends BasePHPVisitor implements NodeVisitor
2322{
23+ use FormTrait;
24+
2425 public function enterNode (Node $ node )
2526 {
26- // only Traverse *Type
27- if ($ node instanceof Stmt \Class_) {
28- if ('Type ' !== substr ($ node ->name , -4 )) {
29- return ;
30- }
27+ if (!$ this ->isFormType ($ node )) {
28+ return ;
3129 }
3230
3331 if (!$ node instanceof Node \Expr \Array_) {
Original file line number Diff line number Diff line change 1212namespace Translation \Extractor \Visitor \Php \Symfony ;
1313
1414use PhpParser \Node ;
15- use PhpParser \Node \Stmt ;
1615use PhpParser \NodeVisitor ;
1716use Translation \Extractor \Visitor \Php \BasePHPVisitor ;
1817
2120 */
2221final class FormTypeLabelExplicit extends BasePHPVisitor implements NodeVisitor
2322{
23+ use FormTrait;
24+
2425 public function enterNode (Node $ node )
2526 {
26- // only Traverse *Type
27- if ($ node instanceof Stmt \Class_) {
28- if ('Type ' !== substr ($ node ->name , -4 )) {
29- return ;
30- }
27+ if (!$ this ->isFormType ($ node )) {
28+ return ;
3129 }
3230
3331 // we could have chosen to traverse specifically the buildForm function or ->add()
Original file line number Diff line number Diff line change 1212namespace Translation \Extractor \Visitor \Php \Symfony ;
1313
1414use PhpParser \Node ;
15- use PhpParser \Node \Stmt ;
1615use PhpParser \NodeVisitor ;
1716use Translation \Extractor \Visitor \Php \BasePHPVisitor ;
1817
2120 */
2221final class FormTypeLabelImplicit extends BasePHPVisitor implements NodeVisitor
2322{
23+ use FormTrait;
24+
2425 public function enterNode (Node $ node )
2526 {
26- // only Traverse *Type
27- if ($ node instanceof Stmt \Class_) {
28- if ('Type ' !== substr ($ node ->name , -4 )) {
29- return ;
30- }
27+ if (!$ this ->isFormType ($ node )) {
28+ return ;
3129 }
3230
3331 $ domain = null ;
Original file line number Diff line number Diff line change 1212namespace Translation \Extractor \Visitor \Php \Symfony ;
1313
1414use PhpParser \Node ;
15- use PhpParser \Node \Stmt ;
1615use PhpParser \NodeVisitor ;
1716use Translation \Extractor \Visitor \Php \BasePHPVisitor ;
1817
2120 */
2221final class FormTypePlaceholder extends BasePHPVisitor implements NodeVisitor
2322{
23+ use FormTrait;
24+
2425 public function enterNode (Node $ node )
2526 {
26- // only Traverse *Type
27- if ($ node instanceof Stmt \Class_) {
28- if ('Type ' !== substr ($ node ->name , -4 )) {
29- return ;
30- }
27+ if (!$ this ->isFormType ($ node )) {
28+ return ;
3129 }
3230
3331 if (!$ node instanceof Node \Expr \Array_) {
Original file line number Diff line number Diff line change 99 * file that was distributed with this source code.
1010 */
1111
12- namespace Translation \extractor \ tests \Functional \Visitor \Php \Symfony ;
12+ namespace Translation \Extractor \ Tests \Functional \Visitor \Php \Symfony ;
1313
1414use Translation \Extractor \Tests \Functional \Visitor \Php \BasePHPVisitorTest ;
1515use Translation \Extractor \Tests \Resources ;
Original file line number Diff line number Diff line change 99 * file that was distributed with this source code.
1010 */
1111
12- namespace Translation \extractor \ tests \Functional \Visitor \Php \Symfony ;
12+ namespace Translation \Extractor \ Tests \Functional \Visitor \Php \Symfony ;
1313
1414use Translation \Extractor \Tests \Functional \Visitor \Php \BasePHPVisitorTest ;
1515use Translation \Extractor \Tests \Resources ;
You can’t perform that action at this time.
0 commit comments