Skip to content

Commit 04e4e35

Browse files
authored
Make sure we do not extract implicit labels form HiddenType (#92)
* Make sure we do not extract implicit labels form HiddenType * cs
1 parent 027d83b commit 04e4e35

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Visitor/Php/Symfony/FormTypeLabelImplicit.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ public function enterNode(Node $node)
3434
if ($node instanceof Node\Expr\MethodCall
3535
&& ('add' === (string) $node->name || 'create' === (string) $node->name)
3636
&& $node->args[0]->value instanceof Node\Scalar\String_) {
37+
$skipLabel = false;
38+
// Check if the form type is "hidden"
39+
if (count($node->args) >= 2) {
40+
$type = $node->args[1]->value;
41+
if ($type instanceof Node\Scalar\String_ && 'Symfony\Component\Form\Extension\Core\Type\HiddenType' === $type->value
42+
|| $type instanceof Node\Expr\ClassConstFetch && 'HiddenType' === $type->class->parts[0]) {
43+
$skipLabel = true;
44+
}
45+
}
46+
3747
// now make sure we don't have 'label' in the array of options
38-
$customLabel = false;
3948
if (count($node->args) >= 3) {
4049
if ($node->args[2]->value instanceof Node\Expr\Array_) {
4150
foreach ($node->args[2]->value->items as $item) {
4251
if (isset($item->key) && 'label' === $item->key->value) {
43-
$customLabel = true;
52+
$skipLabel = true;
4453
}
4554

4655
if (isset($item->key) && 'translation_domain' === $item->key->value) {
@@ -58,7 +67,7 @@ public function enterNode(Node $node)
5867
}
5968

6069
// only if no custom label was found, proceed
61-
if (false === $customLabel && false !== $domain) {
70+
if (false === $skipLabel && false !== $domain) {
6271
$label = $node->args[0]->value->value;
6372
if (!empty($label)) {
6473
if (null !== $location = $this->getLocation($label, $node->getAttribute('startLine'), $node, ['domain' => $domain])) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Symfony\Component\Form\AbstractType;
4+
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
5+
6+
class EmptyValueType extends AbstractType
7+
{
8+
public function buildForm(FormBuilderInterface $builder, array $options)
9+
{
10+
$builder
11+
->add('github.issue_78a', HiddenType::class, array(
12+
))
13+
->add('github.issue_78b', 'Symfony\Component\Form\Extension\Core\Type\HiddenType', array(
14+
));
15+
16+
}
17+
}

tests/Smoke/AllExtractorsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function testNoException()
5050
$sc = $extractor->extract($finder);
5151
$this->translationExists($sc, 'trans.issue_34');
5252
$this->translationMissing($sc, 'trans.issue_62');
53+
$this->translationMissing($sc, 'github.issue_78a');
54+
$this->translationMissing($sc, 'github.issue_78b');
5355

5456
$source = $this->translationExists($sc, 'github.issue_82a');
5557
$this->assertEquals('custom', $source->getContext()['domain']);

0 commit comments

Comments
 (0)