Skip to content

Commit ea9a958

Browse files
noofaqNyholm
authored andcommitted
Support @ignore annotation in $builder->add to prevent implicit label (#87)
* Support @ignore annotation in $builder->add to prevent implicit label In some cases it is inconvenient that FormTypeLabelImplicit adds label when it does not see custom label in the definition (even if it can be added but not in implicit way). Example of such use case is using generator function (or merging option arrays). Example code: ``` $getFieldOptions = function() { return ['label' => false] }; $builder->add('something', TextType::class, $getFieldOptions()) ``` Phrase "something" is added to dictionary. My proposed change allows usage of /** @ignore */ annotation to prevent this implicit add as in the example code below: ``` $builder->add(/** @ignore */'something', TextType::class, $getFieldOptions()) ``` After that change phrase "something" is not added into the dictionary * tests for issue87 - support for Ignore annotation in FormTypeLabelImplicit visitor * cs * cs
1 parent 04e4e35 commit ea9a958

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

src/Visitor/Php/Symfony/FormTypeLabelExplicit.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ public function enterNode(Node $node)
2828
}
2929
parent::enterNode($node);
3030

31-
// we could have chosen to traverse specifically the buildForm function or ->add()
32-
// we will probably miss some easy to catch instances when the actual array of options
33-
// is provided statically or through another function.
34-
// I don't see any disadvantages now to simply parsing arrays and JMSTranslationBundle has
35-
// been doing it like this for quite some time without major problems.
31+
/*
32+
* We could have chosen to traverse specifically the buildForm function or ->add()
33+
* we will probably miss some easy to catch instances when the actual array of options
34+
* is provided statically or through another function.
35+
*
36+
* I don't see any disadvantages now to simply parsing arrays and JMSTranslationBundle has
37+
* been doing it like this for quite some time without major problems.
38+
*/
3639
if (!$node instanceof Node\Expr\Array_) {
3740
return;
3841
}

src/Visitor/Php/Symfony/FormTypeLabelImplicit.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,23 @@ public function enterNode(Node $node)
6161
}
6262
}
6363
}
64-
// actually there's another case here.. if the 3rd argument is anything else, it could well be
65-
// that label is set through a static array. This will not be a common use-case so yeah in this case
66-
// it may be the translation is double.
64+
/*
65+
* Actually there's another case here.. if the 3rd argument is anything else, it could well be
66+
* that label is set through a static array. This will not be a common use-case so yeah in this case
67+
* it may be the translation is double.
68+
*/
6769
}
6870

6971
// only if no custom label was found, proceed
7072
if (false === $skipLabel && false !== $domain) {
73+
/*
74+
* Pass DocComment (if available) from first argument (name of Form field) allowing usage of Ignore
75+
* annotation to disable implicit add; use case: when form options are generated by external method.
76+
*/
77+
if ($node->args[0]->getDocComment()) {
78+
$node->setDocComment($node->args[0]->getDocComment());
79+
}
80+
7181
$label = $node->args[0]->value->value;
7282
if (!empty($label)) {
7383
if (null !== $location = $this->getLocation($label, $node->getAttribute('startLine'), $node, ['domain' => $domain])) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ public function testExtract()
2424
{
2525
$collection = $this->getSourceLocations(new FormTypeLabelImplicit(), Resources\Php\Symfony\ImplicitLabelType::class);
2626

27-
$this->assertCount(3, $collection, print_r($collection, true));
27+
$this->assertCount(4, $collection, print_r($collection, true));
2828
$this->assertEquals('find1', $collection->get(0)->getMessage());
2929
$this->assertEquals('bigger_find2', $collection->get(1)->getMessage());
3030
$this->assertEquals('camelFind3', $collection->get(2)->getMessage());
31+
32+
//issue87: support for Ignore annotation
33+
$this->assertEquals('issue87-willBeAdded', $collection->get(3)->getMessage());
3134
}
3235
}

tests/Resources/Php/Symfony/ImplicitLabelType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
1515
$builder->add(function () { return 'skip3'; });
1616
// Symfony will throw an error I guess, but at least extractions skip it
1717
$builder->add('');
18+
19+
//issue87: support for Ignore annotation
20+
$generateOptions = function() { return ['label' => false]; };
21+
$builder->add('issue87-willBeAdded', null, $generateOptions);
22+
$builder->add(/** @Ignore */'issue87-shouldNotBeAdded', null, $generateOptions);
1823
}
1924
}

0 commit comments

Comments
 (0)