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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.2-dev"
"dev-master": "1.4-dev"
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/Visitor/Php/Symfony/FormTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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;

trait FormTrait
{
private $isFormType = false;

/**
* Check if this node is a form type.
*
* @param Node $node
*
* @return bool
*/
private function isFormType(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
$this->isFormType = 'Type' === substr($node->name, -4);
}

return $this->isFormType;
}
}
14 changes: 6 additions & 8 deletions src/Visitor/Php/Symfony/FormTypeChoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand All @@ -22,6 +21,8 @@
*/
final class FormTypeChoices extends BasePHPVisitor implements NodeVisitor
{
use FormTrait;

/**
* @var int defaults to major version 3
*/
Expand All @@ -41,11 +42,8 @@ public function setSymfonyMajorVersion($sfMajorVersion)

public function enterNode(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
if ('Type' !== substr($node->name, -4)) {
return;
}
if (!$this->isFormType($node)) {
return;
}

if (null === $this->state && $node instanceof Node\Expr\Assign) {
Expand All @@ -60,8 +58,8 @@ public function enterNode(Node $node)
$this->state = null;
}

// symfony 3 displays key by default, where symfony 2 displays value
$useKey = 3 === $this->symfonyMajorVersion;
// symfony 3 or 4 displays key by default, where symfony 2 displays value
$useKey = 2 !== $this->symfonyMajorVersion;

// remember choices in this node
$choicesNodes = [];
Expand Down
10 changes: 4 additions & 6 deletions src/Visitor/Php/Symfony/FormTypeEmptyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Translation\Extractor\Visitor\Php\Symfony;

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

Expand All @@ -21,13 +20,12 @@
*/
final class FormTypeEmptyValue extends BasePHPVisitor implements NodeVisitor
{
use FormTrait;

public function enterNode(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
if ('Type' !== substr($node->name, -4)) {
return;
}
if (!$this->isFormType($node)) {
return;
}

if (!$node instanceof Node\Expr\Array_) {
Expand Down
10 changes: 4 additions & 6 deletions src/Visitor/Php/Symfony/FormTypeInvalidMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Translation\Extractor\Visitor\Php\Symfony;

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

Expand All @@ -21,13 +20,12 @@
*/
final class FormTypeInvalidMessage extends BasePHPVisitor implements NodeVisitor
{
use FormTrait;

public function enterNode(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
if ('Type' !== substr($node->name, -4)) {
return;
}
if (!$this->isFormType($node)) {
return;
}

if (!$node instanceof Node\Expr\Array_) {
Expand Down
10 changes: 4 additions & 6 deletions src/Visitor/Php/Symfony/FormTypeLabelExplicit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Translation\Extractor\Visitor\Php\Symfony;

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

Expand All @@ -21,13 +20,12 @@
*/
final class FormTypeLabelExplicit extends BasePHPVisitor implements NodeVisitor
{
use FormTrait;

public function enterNode(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
if ('Type' !== substr($node->name, -4)) {
return;
}
if (!$this->isFormType($node)) {
return;
}

// we could have chosen to traverse specifically the buildForm function or ->add()
Expand Down
10 changes: 4 additions & 6 deletions src/Visitor/Php/Symfony/FormTypeLabelImplicit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Translation\Extractor\Visitor\Php\Symfony;

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

Expand All @@ -21,13 +20,12 @@
*/
final class FormTypeLabelImplicit extends BasePHPVisitor implements NodeVisitor
{
use FormTrait;

public function enterNode(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
if ('Type' !== substr($node->name, -4)) {
return;
}
if (!$this->isFormType($node)) {
return;
}

// use add() function and look at first argument and if that's a string
Expand Down
10 changes: 4 additions & 6 deletions src/Visitor/Php/Symfony/FormTypePlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Translation\Extractor\Visitor\Php\Symfony;

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

Expand All @@ -21,13 +20,12 @@
*/
final class FormTypePlaceholder extends BasePHPVisitor implements NodeVisitor
{
use FormTrait;

public function enterNode(Node $node)
{
// only Traverse *Type
if ($node instanceof Stmt\Class_) {
if ('Type' !== substr($node->name, -4)) {
return;
}
if (!$this->isFormType($node)) {
return;
}

if (!$node instanceof Node\Expr\Array_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

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

use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
use Translation\Extractor\Tests\Resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

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

use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
use Translation\Extractor\Tests\Resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

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

use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
use Translation\Extractor\Tests\Resources;
Expand Down
29 changes: 29 additions & 0 deletions tests/Resources/Github/Issue_71.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace App\Controller\Test;


use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class TestController extends Controller
{
public function foo()
{
$foo = [
['label' => $this->get('translator')->trans('this.should.be.translated.without.errors')], //ERROR: Form label is not a scalar string
[/** @Ignore */'label' => $this->get('translator')->trans('this.should.be.translated.without.errors.and.now.it.works')],
['label_' => $this->get('translator')->trans('this.works.correctly')],
['label' => $this->shouldBeIgnored()], //ERROR: Form label is not a scalar string
[/** @Ignore */'label' => $this->shouldBeIgnoredAndGoesWithoutError()],
];
}

private function shouldBeIgnored()
{
}

private function shouldBeIgnoredAndGoesWithoutError()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class FormInvalidMessage extends AbstractType
class FormInvalidMessageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
Expand Down
6 changes: 6 additions & 0 deletions tests/Smoke/AllExtractorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public function testNoException()
$sc = $extractor->extract($finder);
$this->translationExists($sc, 'trans.issue_34');
$this->translationMissing($sc, 'trans.issue_62');

/*
* It is okey to increase the error count if you adding more fixtures/code.
* We just need to be aware that it changes.
*/
$this->assertCount(9, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.');
}

/**
Expand Down