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
61 changes: 41 additions & 20 deletions src/Visitor/Php/Symfony/FormTypePlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,74 @@

use PhpParser\Node;
use PhpParser\NodeVisitor;
use Translation\Extractor\Visitor\Php\BasePHPVisitor;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class FormTypePlaceholder extends BasePHPVisitor implements NodeVisitor
final class FormTypePlaceholder extends AbstractFormType implements NodeVisitor
{
use FormTrait;

private $arrayNodeVisited = [];

public function enterNode(Node $node)
{
if (!$this->isFormType($node)) {
return;
}
parent::enterNode($node);

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

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

if ('placeholder' === $item->key->value) {
if ('translation_domain' === $item->key->value) {
// Try to find translation domain
if ($item->value instanceof Node\Scalar\String_) {
$line = $item->value->getAttribute('startLine');
$this->addLocation($item->value->value, $line, $item);
$domain = $item->value->value;
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
// 'placeholder' => false,
// Do noting
} else {
$this->addError($item, 'Form placeholder is not a scalar string');
$domain = false;
}
} elseif ('placeholder' === $item->key->value) {
$placeholderNode = $item;
} elseif ('attr' === $item->key->value) {
foreach ($item->value->items as $attrValue) {
if ('placeholder' === $attrValue->key->value) {
$placeholderNode = $attrValue;

break;
}
}
}
}
}

public function leaveNode(Node $node)
{
}

public function beforeTraverse(array $nodes)
{
}
if (null !== $placeholderNode) {
/**
* Make sure we do not visit the same placeholder node twice.
*/
$hash = spl_object_hash($placeholderNode);
if (isset($this->arrayNodeVisited[$hash])) {
return;
}
$this->arrayNodeVisited[$hash] = true;

public function afterTraverse(array $nodes)
{
if ($placeholderNode->value instanceof Node\Scalar\String_) {
$line = $placeholderNode->value->getAttribute('startLine');
if (null !== $location = $this->getLocation($placeholderNode->value->value, $line, $placeholderNode, ['domain' => $domain])) {
$this->lateCollect($location);
}
} elseif ($placeholderNode->value instanceof Node\Expr\ConstFetch && 'false' === $placeholderNode->value->name->toString()) {
// 'placeholder' => false,
// Do noting
} else {
$this->addError($placeholderNode, 'Form placeholder is not a scalar string');
}
}
}
}
40 changes: 40 additions & 0 deletions tests/Resources/Github/issue_96.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class GlobalTranslationDomainWithPlaceholderType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', null, [
'attr' => array(
'placeholder' => 'github.issue_96a.placeholder',
),
'label' => 'github.issue_96a.label'
])
->add('password', null, [
'attr' => array(
'placeholder' => 'github.issue_96b.placeholder',
),
'label' => 'github.issue_96b.label',
'translation_domain' => 'foobar'
])
->add('age', null, [
'placeholder' => 'github.issue_96c.placeholder',
'label' => 'github.issue_96c.label',
'translation_domain' => 'foobar'
])
->add('location', null, [
'placeholder' => 'github.issue_96d.placeholder',
'label' => 'github.issue_96d.label',
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'translation_domain' => 'custom',
]);
}
}
9 changes: 9 additions & 0 deletions tests/Smoke/AllExtractorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function testNoException()
$source = $this->translationExists($sc, 'github.issue_82c');
$this->assertEquals('custom', $source->getContext()['domain']);

$source = $this->translationExists($sc, 'github.issue_96a.placeholder');
$this->assertEquals('custom', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_96b.placeholder');
$this->assertEquals('foobar', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_96c.placeholder');
$this->assertEquals('foobar', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_96d.placeholder');
$this->assertEquals('custom', $source->getContext()['domain']);

/*
* It is okey to increase the error count if you adding more fixtures/code.
* We just need to be aware that it changes.
Expand Down