Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/Visitor/Php/Symfony/FormTypeEmptyValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Extractor\Visitor\Php\Symfony;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitor;
use Translation\Extractor\Model\SourceLocation;
use Translation\Extractor\Visitor\Php\BasePHPVisitor;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class FormTypeEmptyValue extends BasePHPVisitor implements NodeVisitor
{
public function enterNode(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
if (substr($node->name, -4) !== 'Type') {
return;
}
}

if (!$node instanceof Node\Expr\Array_) {
return;
}

foreach ($node->items as $item) {
if (!$item->key instanceof Node\Scalar\String_) {
continue;
}

if ($item->key->value !== 'empty_value') {
continue;
}

if ($item->value instanceof Node\Scalar\String_) {
$this->storeValue($node, $item);
} elseif ($item->value instanceof Node\Expr\Array_) {
foreach ($item->value->items as $arrayItem) {
$this->storeValue($node, $arrayItem);
}
}
}
}

/**
* @param Node $node
* @param $item
*/
private function storeValue(Node $node, $item)
{
if (!$item->value instanceof Node\Scalar\String_) {
$this->addError($item, 'Form label is not a scalar string');

return;
}

$label = $item->value->value;
if (empty($label)) {
return;
}

$sl = new SourceLocation($label, $this->getAbsoluteFilePath(), $node->getAttribute('startLine'));
$this->collection->addLocation($sl);
}

public function leaveNode(Node $node)
{
}

public function beforeTraverse(array $nodes)
{
}

public function afterTraverse(array $nodes)
{
}
}
33 changes: 33 additions & 0 deletions tests/Functional/Visitor/Php/Symfony/FormEmptyValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\extractor\tests\Functional\Visitor\Php\Symfony;

use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
use Translation\Extractor\Tests\Resources;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeEmptyValue;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class FormEmptyValueTest extends BasePHPVisitorTest
{
public function testExtract()
{
$collection = $this->getSourceLocations(new FormTypeEmptyValue(), Resources\Php\Symfony\EmptyValueType::class);

$this->assertCount(3, $collection);

$this->assertEquals('gender.empty_value', $collection->get(0)->getMessage());
$this->assertEquals('birthday.form.year', $collection->get(1)->getMessage());
$this->assertEquals('birthday.form.month', $collection->get(2)->getMessage());
}
}
37 changes: 37 additions & 0 deletions tests/Resources/Php/Symfony/EmptyValueType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Translation\Extractor\Tests\Resources\Php\Symfony;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class EmptyValueType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('gender', ChoiceType::class, array(
'choices_as_values' => true,
'empty_value' => 'gender.empty_value', // <-- here
'choices' => array(
'gender.female' => 0,
'gender.male' => 1,
),
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'empty_value' => array(
'year' => 'birthday.form.year',
'month' => 'birthday.form.month',
),
'error_bubbling' => false,
));
}


}