Skip to content

Commit e2df107

Browse files
author
Olivier Dolbeau
authored
Merge pull request #148 from Guite/master
add extractor for form field titles
2 parents c5d58cf + 48d1c9b commit e2df107

File tree

8 files changed

+189
-6
lines changed

8 files changed

+189
-6
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,3 @@ parameters:
1414
message: "#^Access to an undefined property PhpParser\\\\Node\\:\\:\\$args\\.$#"
1515
count: 1
1616
path: src/Visitor/Php/Symfony/AbstractFormType.php
17-
18-
-
19-
message: "#^Access to an undefined property PhpParser\\\\Node\\\\Expr\\:\\:\\$value\\.$#"
20-
count: 1
21-
path: src/Visitor/Php/Symfony/FormTypePlaceholder.php

src/Visitor/Php/Symfony/FormTypePlaceholder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function enterNode(Node $node): ?Node
5555
$placeholderNode = $item;
5656
} elseif ('attr' === $item->key->value && $item->value instanceof Node\Expr\Array_) {
5757
foreach ($item->value->items as $attrValue) {
58+
if (!$attrValue->key instanceof Node\Scalar\String_) {
59+
continue;
60+
}
5861
if ('placeholder' === $attrValue->key->value) {
5962
$placeholderNode = $attrValue;
6063

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
17+
/**
18+
* @author Tobias Nyholm <[email protected]>
19+
*/
20+
final class FormTypeTitle extends AbstractFormType implements NodeVisitor
21+
{
22+
use FormTrait;
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function enterNode(Node $node): ?Node
28+
{
29+
if (!$this->isFormType($node)) {
30+
return null;
31+
}
32+
33+
parent::enterNode($node);
34+
35+
if (!$node instanceof Node\Expr\Array_) {
36+
return null;
37+
}
38+
39+
$titleNode = null;
40+
$domain = null;
41+
foreach ($node->items as $item) {
42+
if (!$item->key instanceof Node\Scalar\String_) {
43+
continue;
44+
}
45+
if ('translation_domain' === $item->key->value) {
46+
// Try to find translation domain
47+
if ($item->value instanceof Node\Scalar\String_) {
48+
$domain = $item->value->value;
49+
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
50+
$domain = false;
51+
}
52+
} elseif ('attr' === $item->key->value && $item->value instanceof Node\Expr\Array_) {
53+
foreach ($item->value->items as $attrValue) {
54+
if (!$attrValue->key instanceof Node\Scalar\String_) {
55+
continue;
56+
}
57+
if ('title' === $attrValue->key->value) {
58+
$titleNode = $attrValue;
59+
60+
break;
61+
}
62+
}
63+
}
64+
}
65+
66+
if (null === $titleNode) {
67+
return null;
68+
}
69+
70+
if ($titleNode->value instanceof Node\Scalar\String_) {
71+
$line = $titleNode->value->getAttribute('startLine');
72+
if (null !== $location = $this->getLocation($titleNode->value->value, $line, $titleNode, ['domain' => $domain])) {
73+
$this->lateCollect($location);
74+
}
75+
} else {
76+
$this->addError($titleNode, 'Form field title is not a scalar string');
77+
}
78+
79+
return null;
80+
}
81+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
2020
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
2121
use Translation\Extractor\Visitor\Php\Symfony\FormTypePlaceholder;
22+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeTitle;
2223

2324
/**
2425
* @author Rein Baarsma <[email protected]>
@@ -36,6 +37,7 @@ public function __construct()
3637
new FormTypeLabelExplicit(),
3738
new FormTypeLabelImplicit(),
3839
new FormTypePlaceholder(),
40+
new FormTypeTitle(),
3941
];
4042

4143
parent::__construct();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Tests\Functional\Visitor\Php\Symfony;
13+
14+
use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
15+
use Translation\Extractor\Tests\Resources;
16+
use Translation\Extractor\Visitor\Php\Symfony\ContainerAwareTrans;
17+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeTitle;
18+
19+
/**
20+
* @author Tobias Nyholm <[email protected]>
21+
*/
22+
final class FormTypeTitleTest extends BasePHPVisitorTest
23+
{
24+
public function testExtract()
25+
{
26+
$collection = $this->getSourceLocations(new FormTypeTitle(),
27+
Resources\Php\Symfony\TitleFormType::class);
28+
29+
$this->assertCount(1, $collection);
30+
$this->assertEquals('form.title.text', $collection->get(0)->getMessage());
31+
}
32+
33+
public function testExtractError()
34+
{
35+
$collection = $this->getSourceLocations(new FormTypeTitle(),
36+
Resources\Php\Symfony\TitleFormErrorType::class);
37+
38+
$errors = $collection->getErrors();
39+
$this->assertCount(1, $errors);
40+
}
41+
42+
public function testChildVisitationNotBlocked()
43+
{
44+
$collection = $this->getSourceLocations(
45+
[
46+
new FormTypeTitle(),
47+
new ContainerAwareTrans(),
48+
],
49+
Resources\Php\Symfony\ContainerAwareTrans::class
50+
);
51+
52+
$this->assertCount(6, $collection);
53+
54+
$this->assertEquals('trans0', $collection->get(0)->getMessage());
55+
$this->assertEquals('trans1', $collection->get(1)->getMessage());
56+
$this->assertEquals('trans_line', $collection->get(2)->getMessage());
57+
$this->assertEquals('variable', $collection->get(3)->getMessage());
58+
$this->assertEquals('my.pdf', $collection->get(4)->getMessage());
59+
$this->assertEquals('bar', $collection->get(5)->getMessage());
60+
}
61+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
8+
class TitleFormErrorType extends AbstractType
9+
{
10+
public function buildForm(FormBuilderInterface $builder, array $options)
11+
{
12+
$string = 'foo';
13+
$builder
14+
->add('field_with_attr_title', 'text', array(
15+
'label' => 'field.with.title',
16+
'attr' => array('title' => $string)
17+
))
18+
;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
8+
class TitleFormType extends AbstractType
9+
{
10+
public function buildForm(FormBuilderInterface $builder, array $options)
11+
{
12+
$builder
13+
->add('field_with_attr_title', 'text', array(
14+
'label' => 'field.with.title',
15+
'attr' => array('title' => 'form.title.text')
16+
))
17+
;
18+
}
19+
}

tests/Smoke/AllExtractorsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
3131
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
3232
use Translation\Extractor\Visitor\Php\Symfony\FormTypePlaceholder;
33+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeTitle;
3334
use Translation\Extractor\Visitor\Php\TranslateAnnotationVisitor;
3435
use Translation\Extractor\Visitor\Twig\TwigVisitor;
3536

@@ -89,7 +90,7 @@ public function testNoException()
8990
* It is okey to increase the error count if you adding more fixtures/code.
9091
* We just need to be aware that it changes.
9192
*/
92-
$this->assertCount(12, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.');
93+
$this->assertCount(13, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.');
9394
}
9495

9596
/**
@@ -149,6 +150,7 @@ private function getPHPFileExtractor(): PHPFileExtractor
149150
$file->addVisitor(new FormTypeLabelExplicit());
150151
$file->addVisitor(new FormTypeLabelImplicit());
151152
$file->addVisitor(new FormTypePlaceholder());
153+
$file->addVisitor(new FormTypeTitle());
152154
$file->addVisitor(new SourceLocationContainerVisitor());
153155
$file->addVisitor(new TranslateAnnotationVisitor());
154156

0 commit comments

Comments
 (0)