Skip to content

Commit 06557d4

Browse files
authored
Make sure FormExtracotrs are disabled unless we extract from a form type (#74)
* Make sure FormExtracotrs are disabled unless we extract from a form type * cs * Check for sf 2 * using a FormTrait * Update composer alias * cs * Remove PHP 7 code * Remove more PHP 7 code * cs
1 parent b64f8f5 commit 06557d4

File tree

14 files changed

+103
-43
lines changed

14 files changed

+103
-43
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"extra": {
3939
"branch-alias": {
40-
"dev-master": "1.2-dev"
40+
"dev-master": "1.4-dev"
4141
}
4242
}
4343
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

src/Visitor/Php/Symfony/FormTypeChoices.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

1414
use PhpParser\Node;
15-
use PhpParser\Node\Stmt;
1615
use PhpParser\NodeVisitor;
1716
use Translation\Extractor\Model\SourceLocation;
1817
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
@@ -22,6 +21,8 @@
2221
*/
2322
final 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 = [];

src/Visitor/Php/Symfony/FormTypeEmptyValue.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

1414
use PhpParser\Node;
15-
use PhpParser\Node\Stmt;
1615
use PhpParser\NodeVisitor;
1716
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
1817

@@ -21,13 +20,12 @@
2120
*/
2221
final 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_) {

src/Visitor/Php/Symfony/FormTypeInvalidMessage.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

1414
use PhpParser\Node;
15-
use PhpParser\Node\Stmt;
1615
use PhpParser\NodeVisitor;
1716
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
1817

@@ -21,13 +20,12 @@
2120
*/
2221
final 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_) {

src/Visitor/Php/Symfony/FormTypeLabelExplicit.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

1414
use PhpParser\Node;
15-
use PhpParser\Node\Stmt;
1615
use PhpParser\NodeVisitor;
1716
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
1817

@@ -21,13 +20,12 @@
2120
*/
2221
final 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()

src/Visitor/Php/Symfony/FormTypeLabelImplicit.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

1414
use PhpParser\Node;
15-
use PhpParser\Node\Stmt;
1615
use PhpParser\NodeVisitor;
1716
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
1817

@@ -21,13 +20,12 @@
2120
*/
2221
final 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;

src/Visitor/Php/Symfony/FormTypePlaceholder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

1414
use PhpParser\Node;
15-
use PhpParser\Node\Stmt;
1615
use PhpParser\NodeVisitor;
1716
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
1817

@@ -21,13 +20,12 @@
2120
*/
2221
final 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_) {

tests/Functional/Visitor/Php/Symfony/FormEmptyValueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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

1414
use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
1515
use Translation\Extractor\Tests\Resources;

tests/Functional/Visitor/Php/Symfony/FormTypeInvalidMessageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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

1414
use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
1515
use Translation\Extractor\Tests\Resources;

0 commit comments

Comments
 (0)