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
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ parameters:
message: "#^Access to an undefined property PhpParser\\\\Node\\:\\:\\$args\\.$#"
count: 1
path: src/Visitor/Php/Symfony/AbstractFormType.php

-
message: "#^Access to an undefined property PhpParser\\\\Node\\\\Expr\\:\\:\\$value\\.$#"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 ignored error less! 🎉

count: 1
path: src/Visitor/Php/Symfony/FormTypePlaceholder.php
3 changes: 3 additions & 0 deletions src/Visitor/Php/Symfony/FormTypePlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function enterNode(Node $node): ?Node
$placeholderNode = $item;
} elseif ('attr' === $item->key->value && $item->value instanceof Node\Expr\Array_) {
foreach ($item->value->items as $attrValue) {
if (!$attrValue->key instanceof Node\Scalar\String_) {
continue;
}
if ('placeholder' === $attrValue->key->value) {
$placeholderNode = $attrValue;

Expand Down
81 changes: 81 additions & 0 deletions src/Visitor/Php/Symfony/FormTypeTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\NodeVisitor;

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

/**
* {@inheritdoc}
*/
public function enterNode(Node $node): ?Node
{
if (!$this->isFormType($node)) {
return null;
}

parent::enterNode($node);

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

$titleNode = null;
$domain = null;
foreach ($node->items as $item) {
if (!$item->key instanceof Node\Scalar\String_) {
continue;
}
if ('translation_domain' === $item->key->value) {
// Try to find translation domain
if ($item->value instanceof Node\Scalar\String_) {
$domain = $item->value->value;
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
$domain = false;
}
} elseif ('attr' === $item->key->value && $item->value instanceof Node\Expr\Array_) {
foreach ($item->value->items as $attrValue) {
if (!$attrValue->key instanceof Node\Scalar\String_) {
continue;
}
if ('title' === $attrValue->key->value) {
$titleNode = $attrValue;

break;
}
}
}
}

if (null === $titleNode) {
return null;
}

if ($titleNode->value instanceof Node\Scalar\String_) {
$line = $titleNode->value->getAttribute('startLine');
if (null !== $location = $this->getLocation($titleNode->value->value, $line, $titleNode, ['domain' => $domain])) {
$this->lateCollect($location);
}
} else {
$this->addError($titleNode, 'Form field title is not a scalar string');
}

return null;
}
}
2 changes: 2 additions & 0 deletions tests/Functional/Visitor/Php/Symfony/FormTypeLabelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
use Translation\Extractor\Visitor\Php\Symfony\FormTypePlaceholder;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeTitle;

/**
* @author Rein Baarsma <rein@solidwebcode.com>
Expand All @@ -36,6 +37,7 @@ public function __construct()
new FormTypeLabelExplicit(),
new FormTypeLabelImplicit(),
new FormTypePlaceholder(),
new FormTypeTitle(),
];

parent::__construct();
Expand Down
61 changes: 61 additions & 0 deletions tests/Functional/Visitor/Php/Symfony/FormTypeTitleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\ContainerAwareTrans;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeTitle;

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

$this->assertCount(1, $collection);
$this->assertEquals('form.title.text', $collection->get(0)->getMessage());
}

public function testExtractError()
{
$collection = $this->getSourceLocations(new FormTypeTitle(),
Resources\Php\Symfony\TitleFormErrorType::class);

$errors = $collection->getErrors();
$this->assertCount(1, $errors);
}

public function testChildVisitationNotBlocked()
{
$collection = $this->getSourceLocations(
[
new FormTypeTitle(),
new ContainerAwareTrans(),
],
Resources\Php\Symfony\ContainerAwareTrans::class
);

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

$this->assertEquals('trans0', $collection->get(0)->getMessage());
$this->assertEquals('trans1', $collection->get(1)->getMessage());
$this->assertEquals('trans_line', $collection->get(2)->getMessage());
$this->assertEquals('variable', $collection->get(3)->getMessage());
$this->assertEquals('my.pdf', $collection->get(4)->getMessage());
$this->assertEquals('bar', $collection->get(5)->getMessage());
}
}
20 changes: 20 additions & 0 deletions tests/Resources/Php/Symfony/TitleFormErrorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class TitleFormErrorType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$string = 'foo';
$builder
->add('field_with_attr_title', 'text', array(
'label' => 'field.with.title',
'attr' => array('title' => $string)
))
;
}
}
19 changes: 19 additions & 0 deletions tests/Resources/Php/Symfony/TitleFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class TitleFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field_with_attr_title', 'text', array(
'label' => 'field.with.title',
'attr' => array('title' => 'form.title.text')
))
;
}
}
4 changes: 3 additions & 1 deletion tests/Smoke/AllExtractorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
use Translation\Extractor\Visitor\Php\Symfony\FormTypePlaceholder;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeTitle;
use Translation\Extractor\Visitor\Php\TranslateAnnotationVisitor;
use Translation\Extractor\Visitor\Twig\TwigVisitor;

Expand Down Expand Up @@ -89,7 +90,7 @@ public function testNoException()
* 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(12, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.');
$this->assertCount(13, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.');
}

/**
Expand Down Expand Up @@ -149,6 +150,7 @@ private function getPHPFileExtractor(): PHPFileExtractor
$file->addVisitor(new FormTypeLabelExplicit());
$file->addVisitor(new FormTypeLabelImplicit());
$file->addVisitor(new FormTypePlaceholder());
$file->addVisitor(new FormTypeTitle());
$file->addVisitor(new SourceLocationContainerVisitor());
$file->addVisitor(new TranslateAnnotationVisitor());

Expand Down