From 48fdd6c785ad5feecb843b8fc0efcf1103ed9bc6 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 6 Jan 2018 15:58:24 +0100 Subject: [PATCH 1/7] Adding Desc filter --- Changelog.md | 10 ++++ Resources/config/edit_in_place.yml | 2 +- Resources/config/services.yml | 4 +- Twig/EditInPlaceExtension.php | 79 ++++++++++++++++++++++++++++++ Twig/TranslationExtension.php | 59 +++------------------- 5 files changed, 100 insertions(+), 54 deletions(-) create mode 100644 Twig/EditInPlaceExtension.php diff --git a/Changelog.md b/Changelog.md index 8a4a8e39..77eea3d6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,16 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee ### Added +- Support for `desc` filter in Twig. + +### Changed + +- Twig extension `TranslationExtension` was renamed to `EditInPlaceExtension` + +## 0.5.0 + +### Added + - Symfony 4 support - New `--cache` option on the `translation:download` allowing to clear the cache automatically if the downloaded translations have changed. - Support for Yandex translator diff --git a/Resources/config/edit_in_place.yml b/Resources/config/edit_in_place.yml index 6d89c949..43bfbda2 100644 --- a/Resources/config/edit_in_place.yml +++ b/Resources/config/edit_in_place.yml @@ -23,7 +23,7 @@ services: php_translation.edit_in_place.extension.trans: public: false - class: Translation\Bundle\Twig\TranslationExtension + class: Translation\Bundle\Twig\EditInPlaceExtension arguments: - '@php_translator.edit_in_place.xtrans_html_translator' calls: diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 89bd5b9e..d697e8a3 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -56,6 +56,6 @@ services: arguments: [] php_translation.twig_extension: - class: Translation\Extractor\Twig\TranslationExtension + class: Translation\Bundle\Twig\TranslationExtension tags: - - { name: twig.extension } \ No newline at end of file + - { name: twig.extension } diff --git a/Twig/EditInPlaceExtension.php b/Twig/EditInPlaceExtension.php new file mode 100644 index 00000000..14eb9923 --- /dev/null +++ b/Twig/EditInPlaceExtension.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Twig; + +use Symfony\Component\HttpFoundation\RequestStack; +use Translation\Bundle\EditInPlace\ActivatorInterface; + +/** + * Override the `trans` functions `is_safe` option to allow HTML output from the + * translator. This extension is used by for the EditInPlace feature. + * + * @author Damien Alexandre + */ +final class EditInPlaceExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension +{ + /** + * @var ActivatorInterface + */ + private $activator; + + /** + * @var RequestStack + */ + private $requestStack; + + /** + * {@inheritdoc} + */ + public function getFilters() + { + return [ + new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]), + new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]), + ]; + } + + /** + * Escape output if the EditInPlace is disabled. + * + * @return array + */ + public function isSafe($node) + { + return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? ['html'] : []; + } + + /** + * @param ActivatorInterface $activator + */ + public function setActivator(ActivatorInterface $activator) + { + $this->activator = $activator; + } + + /** + * @param RequestStack $requestStack + */ + public function setRequestStack(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return self::class; + } +} diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 2da44391..e2a5cdd3 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -11,69 +11,26 @@ namespace Translation\Bundle\Twig; -use Symfony\Component\HttpFoundation\RequestStack; -use Translation\Bundle\EditInPlace\ActivatorInterface; - -/** - * Override the `trans` functions `is_safe` option to allow HTML output from the - * translator. This extension is used by for the EditInPlace feature. - * - * @author Damien Alexandre - */ -class TranslationExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension +class TranslationExtension extends \Twig_Extension { - /** - * @var ActivatorInterface - */ - private $activator; - - /** - * @var RequestStack - */ - private $requestStack; - - /** - * {@inheritdoc} - */ public function getFilters() { return [ - new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]), - new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]), + new \Twig_SimpleFilter('desc', [$this, 'runDescFilter']), ]; } - /** - * Escape output if the EditInPlace is disabled. - * - * @return array - */ - public function isSafe($node) + public function runDescFilter($translation, $description) { - return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? ['html'] : []; - } + if (empty($translation)) { + return $description; + } - /** - * @param ActivatorInterface $activator - */ - public function setActivator(ActivatorInterface $activator) - { - $this->activator = $activator; - } - - /** - * @param RequestStack $requestStack - */ - public function setRequestStack(RequestStack $requestStack) - { - $this->requestStack = $requestStack; + return $translation; } - /** - * {@inheritdoc} - */ public function getName() { - return self::class; + return 'php-translation'; } } From d3aeb1e48a5975f1de0669c848dd740787314cf1 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 6 Jan 2018 16:16:40 +0100 Subject: [PATCH 2/7] Make sue we save Desc when importing --- Service/Importer.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Service/Importer.php b/Service/Importer.php index b8125f0f..1760b77d 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -120,6 +120,9 @@ private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, S $meta = $this->getMetadata($catalogue, $key, $domain); $meta->addCategory('file-source', sprintf('%s:%s', substr($sourceLocation->getPath(), $trimLength), $sourceLocation->getLine())); + if (isset($sourceLocation->getContext()['desc'])) { + $meta->addCategory('desc', $sourceLocation->getContext()['desc']); + } $this->setMetadata($catalogue, $key, $domain, $meta); } } From c8228cda7c1b742af364059b840896a9242d5e78 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 6 Jan 2018 16:23:55 +0100 Subject: [PATCH 3/7] Make the class final --- Twig/TranslationExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index e2a5cdd3..9cbe87e3 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -11,7 +11,7 @@ namespace Translation\Bundle\Twig; -class TranslationExtension extends \Twig_Extension +final class TranslationExtension extends \Twig_Extension { public function getFilters() { From 10c9654b3d1b410588a6b0e3798a411417a01422 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 14 Jan 2018 19:33:12 +0100 Subject: [PATCH 4/7] Adding visitors to twig filter --- Resources/config/extractors.yml | 2 +- Twig/Node/Transchoice.php | 50 ++++++++ Twig/TranslationExtension.php | 96 ++++++++++++-- Twig/Visitor/DefaultApplyingNodeVisitor.php | 132 ++++++++++++++++++++ Twig/{ => Visitor}/DummyTwigVisitor.php | 2 +- Twig/Visitor/NormalizingNodeVisitor.php | 57 +++++++++ Twig/Visitor/RemovingNodeVisitor.php | 69 ++++++++++ 7 files changed, 399 insertions(+), 9 deletions(-) create mode 100644 Twig/Node/Transchoice.php create mode 100644 Twig/Visitor/DefaultApplyingNodeVisitor.php rename Twig/{ => Visitor}/DummyTwigVisitor.php (91%) create mode 100644 Twig/Visitor/NormalizingNodeVisitor.php create mode 100644 Twig/Visitor/RemovingNodeVisitor.php diff --git a/Resources/config/extractors.yml b/Resources/config/extractors.yml index efb9c280..4edbfa48 100644 --- a/Resources/config/extractors.yml +++ b/Resources/config/extractors.yml @@ -73,7 +73,7 @@ services: public: false php_translation.extractor.twig.visitor.twig: - class: Translation\Bundle\Twig\DummyTwigVisitor + class: Translation\Bundle\Twig\Visitor\DummyTwigVisitor factory: ["@php_translation.extractor.twig.factory", create] tags: - { name: 'php_translation.visitor', type: 'twig' } diff --git a/Twig/Node/Transchoice.php b/Twig/Node/Transchoice.php new file mode 100644 index 00000000..4d0fd935 --- /dev/null +++ b/Twig/Node/Transchoice.php @@ -0,0 +1,50 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Translation\Bundle\Twig\Node; + +class Transchoice extends \Twig_Node_Expression +{ + public function __construct(\Twig_Node_Expression_Array $arguments, $lineno) + { + parent::__construct(array('arguments' => $arguments), array(), $lineno); + } + + public function compile(\Twig_Compiler $compiler) + { + $compiler->raw( + sprintf( + '$this->env->getExtension(\'%s\')->%s(', + 'JMS\TranslationBundle\Twig\TranslationExtension', + 'transchoiceWithDefault' + ) + ); + + $first = true; + foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) { + if (!$first) { + $compiler->raw(', '); + } + $first = false; + + $compiler->subcompile($pair['value']); + } + + $compiler->raw(')'); + } +} diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 9cbe87e3..620e71b4 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -11,22 +11,104 @@ namespace Translation\Bundle\Twig; +use Symfony\Component\Translation\TranslatorInterface; +use Translation\Bundle\Twig\Visitor\DefaultApplyingNodeVisitor; +use Translation\Bundle\Twig\Visitor\NormalizingNodeVisitor; +use Translation\Bundle\Twig\Visitor\RemovingNodeVisitor; + +/** + * + * @author Johannes M. Schmitt + * @author Tobias Nyholm + */ final class TranslationExtension extends \Twig_Extension { + /** + * @var TranslatorInterface + */ + private $translator; + + /** + * @var bool + */ + private $debug; + + /** + * @param TranslatorInterface $translator + * @param bool $debug + */ + public function __construct(TranslatorInterface $translator, $debug = false) + { + $this->translator = $translator; + $this->debug = $debug; + } + + /** + * @return array + */ public function getFilters() { - return [ - new \Twig_SimpleFilter('desc', [$this, 'runDescFilter']), - ]; + return array( + new \Twig_SimpleFilter('desc', array($this, 'desc')), + new \Twig_SimpleFilter('meaning', array($this, 'meaning')), + ); + } + + /** + * @return array + */ + public function getNodeVisitors() + { + $visitors = array( + new NormalizingNodeVisitor(), + new RemovingNodeVisitor(), + ); + + if ($this->debug) { + $visitors[] = new DefaultApplyingNodeVisitor(); + } + + return $visitors; } - public function runDescFilter($translation, $description) + /** + * @param string $message + * @param string $defaultMessage + * @param int $count + * @param array $arguments + * @param null|string $domain + * @param null|string $locale + * @return string + */ + public function transchoiceWithDefault($message, $defaultMessage, $count, array $arguments = array(), $domain = null, $locale = null) { - if (empty($translation)) { - return $description; + if (null === $domain) { + $domain = 'messages'; } - return $translation; + if (false == $this->translator->getCatalogue($locale)->defines($message, $domain)) { + return $this->translator->transChoice($defaultMessage, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale); + } + + return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale); + } + + /** + * @param $v + * @return mixed + */ + public function desc($v) + { + return $v; + } + + /** + * @param $v + * @return mixed + */ + public function meaning($v) + { + return $v; } public function getName() diff --git a/Twig/Visitor/DefaultApplyingNodeVisitor.php b/Twig/Visitor/DefaultApplyingNodeVisitor.php new file mode 100644 index 00000000..32a08490 --- /dev/null +++ b/Twig/Visitor/DefaultApplyingNodeVisitor.php @@ -0,0 +1,132 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Twig\Visitor; + +use Translation\Bundle\Twig\Node\Transchoice; + +/** + * Applies the value of the "desc" filter if the "trans" filter has no + * translations. + * + * This is only active in your development environment. + * + * @author Johannes M. Schmitt + */ +class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor +{ + /** + * @var bool + */ + private $enabled = true; + + /** + * @param $bool + */ + public function setEnabled($bool) + { + $this->enabled = (bool) $bool; + } + + /** + * @param \Twig_Node $node + * @param \Twig_Environment $env + * @return \Twig_Node + */ + public function doEnterNode(\Twig_Node $node, \Twig_Environment $env) + { + if (!$this->enabled) { + return $node; + } + + if ($node instanceof \Twig_Node_Expression_Filter && 'desc' === $node->getNode('filter')->getAttribute('value')) { + $transNode = $node->getNode('node'); + while ($transNode instanceof \Twig_Node_Expression_Filter + && 'trans' !== $transNode->getNode('filter')->getAttribute('value') + && 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) { + $transNode = $transNode->getNode('node'); + } + + if (!$transNode instanceof \Twig_Node_Expression_Filter) { + throw new \RuntimeException(sprintf('The "desc" filter must be applied after a "trans", or "transchoice" filter.')); + } + + $wrappingNode = $node->getNode('node'); + $testNode = clone $wrappingNode; + $defaultNode = $node->getNode('arguments')->getNode(0); + + // if the |transchoice filter is used, delegate the call to the TranslationExtension + // so that we can catch a possible exception when the default translation has not yet + // been extracted + if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) { + $transchoiceArguments = new \Twig_Node_Expression_Array(array(), $transNode->getTemplateLine()); + $transchoiceArguments->addElement($wrappingNode->getNode('node')); + $transchoiceArguments->addElement($defaultNode); + foreach ($wrappingNode->getNode('arguments') as $arg) { + $transchoiceArguments->addElement($arg); + } + + $transchoiceNode = new Transchoice($transchoiceArguments, $transNode->getTemplateLine()); + $node->setNode('node', $transchoiceNode); + + return $node; + } + + // if the |trans filter has replacements parameters + // (e.g. |trans({'%foo%': 'bar'})) + if ($wrappingNode->getNode('arguments')->hasNode(0)) { + $lineno = $wrappingNode->getTemplateLine(); + + // remove the replacements from the test node + $testNode->setNode('arguments', clone $testNode->getNode('arguments')); + $testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array(array(), $lineno)); + + // wrap the default node in a |replace filter + $defaultNode = new \Twig_Node_Expression_Filter( + clone $node->getNode('arguments')->getNode(0), + new \Twig_Node_Expression_Constant('replace', $lineno), + new \Twig_Node(array( + clone $wrappingNode->getNode('arguments')->getNode(0) + )), + $lineno + ); + } + + $condition = new \Twig_Node_Expression_Conditional( + new \Twig_Node_Expression_Binary_Equal($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()), + $defaultNode, + clone $wrappingNode, + $wrappingNode->getTemplateLine() + ); + $node->setNode('node', $condition); + } + + return $node; + } + + /** + * @param \Twig_Node $node + * @param \Twig_Environment $env + * @return \Twig_Node + */ + public function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) + { + return $node; + } + + /** + * @return int + */ + public function getPriority() + { + return -2; + } +} diff --git a/Twig/DummyTwigVisitor.php b/Twig/Visitor/DummyTwigVisitor.php similarity index 91% rename from Twig/DummyTwigVisitor.php rename to Twig/Visitor/DummyTwigVisitor.php index 467ca2be..6e983e28 100644 --- a/Twig/DummyTwigVisitor.php +++ b/Twig/Visitor/DummyTwigVisitor.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Translation\Bundle\Twig; +namespace Translation\Bundle\Twig\Visitor; /** * This dummy extractor is used to be compatible with both Twig1 and Twig2. It is only used in dependency injection diff --git a/Twig/Visitor/NormalizingNodeVisitor.php b/Twig/Visitor/NormalizingNodeVisitor.php new file mode 100644 index 00000000..1625747f --- /dev/null +++ b/Twig/Visitor/NormalizingNodeVisitor.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Twig\Visitor; + +/** + * Performs equivalence transformations on the AST to ensure that + * subsequent visitors do not need to be aware of different syntaxes. + * + * E.g. "foo" ~ "bar" ~ "baz" would become "foobarbaz" + * + * @author Johannes M. Schmitt + */ +class NormalizingNodeVisitor extends \Twig_BaseNodeVisitor +{ + /** + * @param \Twig_Node $node + * @param \Twig_Environment $env + * @return \Twig_Node + */ + protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) + { + return $node; + } + + /** + * @param \Twig_Node $node + * @param \Twig_Environment $env + * @return \Twig_Node_Expression_Constant|\Twig_Node + */ + protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) + { + if ($node instanceof \Twig_Node_Expression_Binary_Concat + && ($left = $node->getNode('left')) instanceof \Twig_Node_Expression_Constant + && ($right = $node->getNode('right')) instanceof \Twig_Node_Expression_Constant) { + return new \Twig_Node_Expression_Constant($left->getAttribute('value').$right->getAttribute('value'), $left->getTemplateLine()); + } + + return $node; + } + + /** + * @return int + */ + public function getPriority() + { + return -3; + } +} diff --git a/Twig/Visitor/RemovingNodeVisitor.php b/Twig/Visitor/RemovingNodeVisitor.php new file mode 100644 index 00000000..be5a6302 --- /dev/null +++ b/Twig/Visitor/RemovingNodeVisitor.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Twig\Visitor; + +/** + * Removes translation metadata filters from the AST. + * + * @author Johannes M. Schmitt + */ +class RemovingNodeVisitor extends \Twig_BaseNodeVisitor +{ + /** + * @var bool + */ + private $enabled = true; + + /** + * @param $bool + */ + public function setEnabled($bool) + { + $this->enabled = (bool) $bool; + } + + /** + * @param \Twig_Node $node + * @param \Twig_Environment $env + * @return \Twig_Node + */ + protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) + { + if ($this->enabled && $node instanceof \Twig_Node_Expression_Filter) { + $name = $node->getNode('filter')->getAttribute('value'); + + if ('desc' === $name || 'meaning' === $name) { + return $this->enterNode($node->getNode('node'), $env); + } + } + + return $node; + } + + /** + * @param \Twig_Node $node + * @param \Twig_Environment $env + * @return \Twig_Node + */ + protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) + { + return $node; + } + + /** + * @return int + */ + public function getPriority() + { + return -1; + } +} From f46590345f96638ef3437ccc0207e15c85d93e00 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 14 Jan 2018 19:49:59 +0100 Subject: [PATCH 5/7] Added tests --- Resources/config/services.yml | 1 + Tests/Unit/Twig/BaseTwigTestCase.php | 39 +++++++++++++++++++ .../Twig/DefaultApplyingNodeVisitorTest.php | 30 ++++++++++++++ .../Fixture/apply_default_value.html.twig | 4 ++ .../apply_default_value_compiled.html.twig | 4 ++ .../binary_concat_of_constants.html.twig | 3 ++ ...ary_concat_of_constants_compiled.html.twig | 1 + .../Twig/Fixture/simple_template.html.twig | 21 ++++++++++ .../simple_template_compiled.html.twig | 21 ++++++++++ .../Unit/Twig/NormalizingNodeVisitorTest.php | 30 ++++++++++++++ Tests/Unit/Twig/RemovingNodeVisitorTest.php | 30 ++++++++++++++ Twig/Node/Transchoice.php | 2 +- Twig/TranslationExtension.php | 2 +- Twig/Visitor/DefaultApplyingNodeVisitor.php | 2 +- Twig/Visitor/NormalizingNodeVisitor.php | 2 +- Twig/Visitor/RemovingNodeVisitor.php | 2 +- 16 files changed, 189 insertions(+), 5 deletions(-) create mode 100644 Tests/Unit/Twig/BaseTwigTestCase.php create mode 100644 Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php create mode 100644 Tests/Unit/Twig/Fixture/apply_default_value.html.twig create mode 100644 Tests/Unit/Twig/Fixture/apply_default_value_compiled.html.twig create mode 100644 Tests/Unit/Twig/Fixture/binary_concat_of_constants.html.twig create mode 100644 Tests/Unit/Twig/Fixture/binary_concat_of_constants_compiled.html.twig create mode 100644 Tests/Unit/Twig/Fixture/simple_template.html.twig create mode 100644 Tests/Unit/Twig/Fixture/simple_template_compiled.html.twig create mode 100644 Tests/Unit/Twig/NormalizingNodeVisitorTest.php create mode 100644 Tests/Unit/Twig/RemovingNodeVisitorTest.php diff --git a/Resources/config/services.yml b/Resources/config/services.yml index d697e8a3..3877bd97 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -57,5 +57,6 @@ services: php_translation.twig_extension: class: Translation\Bundle\Twig\TranslationExtension + arguments: ['@translator', "%kernel.debug%"] tags: - { name: twig.extension } diff --git a/Tests/Unit/Twig/BaseTwigTestCase.php b/Tests/Unit/Twig/BaseTwigTestCase.php new file mode 100644 index 00000000..65282b08 --- /dev/null +++ b/Tests/Unit/Twig/BaseTwigTestCase.php @@ -0,0 +1,39 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Translation\Bundle\Tests\Unit\Twig; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Translation\MessageSelector; +use Symfony\Component\Translation\IdentityTranslator; +use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension; +use Translation\Bundle\Twig\TranslationExtension; + +abstract class BaseTwigTestCase extends TestCase +{ + final protected function parse($file, $debug = false) + { + $content = file_get_contents(__DIR__.'/Fixture/'.$file); + + $env = new \Twig_Environment(new \Twig_Loader_Array(array())); + $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector()))); + $env->addExtension(new TranslationExtension($translator, $debug)); + + return $env->parse($env->tokenize(new \Twig_Source($content, null)))->getNode('body'); + } +} diff --git a/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php b/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php new file mode 100644 index 00000000..5b234364 --- /dev/null +++ b/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php @@ -0,0 +1,30 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Translation\Bundle\Tests\Unit\Twig; + +class DefaultApplyingNodeVisitorTest extends BaseTwigTestCase +{ + public function testApply() + { + $this->assertEquals( + $this->parse('apply_default_value_compiled.html.twig', true), + $this->parse('apply_default_value.html.twig', true) + ); + } +} diff --git a/Tests/Unit/Twig/Fixture/apply_default_value.html.twig b/Tests/Unit/Twig/Fixture/apply_default_value.html.twig new file mode 100644 index 00000000..ccb4d458 --- /dev/null +++ b/Tests/Unit/Twig/Fixture/apply_default_value.html.twig @@ -0,0 +1,4 @@ +{{ "form.label.firstname"|trans|desc("Firstname") }} + +{{ "foo.%bar%"|trans({"%bar%": "baz"})|desc("Foo %bar%") }} + diff --git a/Tests/Unit/Twig/Fixture/apply_default_value_compiled.html.twig b/Tests/Unit/Twig/Fixture/apply_default_value_compiled.html.twig new file mode 100644 index 00000000..254e765d --- /dev/null +++ b/Tests/Unit/Twig/Fixture/apply_default_value_compiled.html.twig @@ -0,0 +1,4 @@ +{{ "form.label.firstname"|trans == "form.label.firstname" ? "Firstname" : "form.label.firstname"|trans }} + +{{ "foo.%bar%"|trans({}) == "foo.%bar%" ? "Foo %bar%"|replace({"%bar%": "baz"}) : "foo.%bar%"|trans({"%bar%": "baz"}) }} + diff --git a/Tests/Unit/Twig/Fixture/binary_concat_of_constants.html.twig b/Tests/Unit/Twig/Fixture/binary_concat_of_constants.html.twig new file mode 100644 index 00000000..99cc553e --- /dev/null +++ b/Tests/Unit/Twig/Fixture/binary_concat_of_constants.html.twig @@ -0,0 +1,3 @@ +{{ "foo" + ~ "bar" + ~ "baz" }} \ No newline at end of file diff --git a/Tests/Unit/Twig/Fixture/binary_concat_of_constants_compiled.html.twig b/Tests/Unit/Twig/Fixture/binary_concat_of_constants_compiled.html.twig new file mode 100644 index 00000000..ed9aff23 --- /dev/null +++ b/Tests/Unit/Twig/Fixture/binary_concat_of_constants_compiled.html.twig @@ -0,0 +1 @@ +{{ "foobarbaz" }} \ No newline at end of file diff --git a/Tests/Unit/Twig/Fixture/simple_template.html.twig b/Tests/Unit/Twig/Fixture/simple_template.html.twig new file mode 100644 index 00000000..2d56ebef --- /dev/null +++ b/Tests/Unit/Twig/Fixture/simple_template.html.twig @@ -0,0 +1,21 @@ +{{ "text.foo"|trans|desc("Foo Bar")|meaning("Some Meaning")}} + +{{ "text.bar"|trans|desc("Foo") }} + +{{ "text.baz"|trans|meaning("Bar") }} + +{{ "text.foo_bar"|trans({}, "foo") }} + +{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} + +{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %} + +{{ "foo.bar" | trans }} + +{{ "foo.bar2" | transchoice(5) }} + +{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} + +{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }} + +{% trans %}text.default_domain{% endtrans %} \ No newline at end of file diff --git a/Tests/Unit/Twig/Fixture/simple_template_compiled.html.twig b/Tests/Unit/Twig/Fixture/simple_template_compiled.html.twig new file mode 100644 index 00000000..4f262627 --- /dev/null +++ b/Tests/Unit/Twig/Fixture/simple_template_compiled.html.twig @@ -0,0 +1,21 @@ +{{ "text.foo"|trans }} + +{{ "text.bar"|trans }} + +{{ "text.baz"|trans }} + +{{ "text.foo_bar"|trans({}, "foo") }} + +{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} + +{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %} + +{{ "foo.bar" | trans }} + +{{ "foo.bar2" | transchoice(5) }} + +{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} + +{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }} + +{% trans %}text.default_domain{% endtrans %} \ No newline at end of file diff --git a/Tests/Unit/Twig/NormalizingNodeVisitorTest.php b/Tests/Unit/Twig/NormalizingNodeVisitorTest.php new file mode 100644 index 00000000..447c3d68 --- /dev/null +++ b/Tests/Unit/Twig/NormalizingNodeVisitorTest.php @@ -0,0 +1,30 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Translation\Bundle\Tests\Unit\Twig; + +class NormalizingNodeVisitorTest extends BaseTwigTestCase +{ + public function testBinaryConcatOfConstants() + { + $this->assertEquals( + $this->parse('binary_concat_of_constants_compiled.html.twig'), + $this->parse('binary_concat_of_constants.html.twig') + ); + } +} diff --git a/Tests/Unit/Twig/RemovingNodeVisitorTest.php b/Tests/Unit/Twig/RemovingNodeVisitorTest.php new file mode 100644 index 00000000..58deb0fd --- /dev/null +++ b/Tests/Unit/Twig/RemovingNodeVisitorTest.php @@ -0,0 +1,30 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Translation\Bundle\Tests\Unit\Twig; + +class RemovingNodeVisitorTest extends BaseTwigTestCase +{ + public function testRemovalWithSimpleTemplate() + { + $expected = $this->parse('simple_template_compiled.html.twig'); + $actual = $this->parse('simple_template.html.twig'); + + $this->assertEquals($expected, $actual); + } +} diff --git a/Twig/Node/Transchoice.php b/Twig/Node/Transchoice.php index 4d0fd935..2b754fa8 100644 --- a/Twig/Node/Transchoice.php +++ b/Twig/Node/Transchoice.php @@ -30,7 +30,7 @@ public function compile(\Twig_Compiler $compiler) $compiler->raw( sprintf( '$this->env->getExtension(\'%s\')->%s(', - 'JMS\TranslationBundle\Twig\TranslationExtension', + 'Translation\Bundle\Twig\TranslationExtension', 'transchoiceWithDefault' ) ); diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 620e71b4..72621e26 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -86,7 +86,7 @@ public function transchoiceWithDefault($message, $defaultMessage, $count, array $domain = 'messages'; } - if (false == $this->translator->getCatalogue($locale)->defines($message, $domain)) { + if (false === $this->translator->getCatalogue($locale)->defines($message, $domain)) { return $this->translator->transChoice($defaultMessage, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale); } diff --git a/Twig/Visitor/DefaultApplyingNodeVisitor.php b/Twig/Visitor/DefaultApplyingNodeVisitor.php index 32a08490..78d632dd 100644 --- a/Twig/Visitor/DefaultApplyingNodeVisitor.php +++ b/Twig/Visitor/DefaultApplyingNodeVisitor.php @@ -21,7 +21,7 @@ * * @author Johannes M. Schmitt */ -class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor +final class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor { /** * @var bool diff --git a/Twig/Visitor/NormalizingNodeVisitor.php b/Twig/Visitor/NormalizingNodeVisitor.php index 1625747f..ee8d0cdc 100644 --- a/Twig/Visitor/NormalizingNodeVisitor.php +++ b/Twig/Visitor/NormalizingNodeVisitor.php @@ -19,7 +19,7 @@ * * @author Johannes M. Schmitt */ -class NormalizingNodeVisitor extends \Twig_BaseNodeVisitor +final class NormalizingNodeVisitor extends \Twig_BaseNodeVisitor { /** * @param \Twig_Node $node diff --git a/Twig/Visitor/RemovingNodeVisitor.php b/Twig/Visitor/RemovingNodeVisitor.php index be5a6302..f08ff4e0 100644 --- a/Twig/Visitor/RemovingNodeVisitor.php +++ b/Twig/Visitor/RemovingNodeVisitor.php @@ -16,7 +16,7 @@ * * @author Johannes M. Schmitt */ -class RemovingNodeVisitor extends \Twig_BaseNodeVisitor +final class RemovingNodeVisitor extends \Twig_BaseNodeVisitor { /** * @var bool From 843c2abb2c38fcdd0deee529ef772aed2f4fca65 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 14 Jan 2018 19:59:35 +0100 Subject: [PATCH 6/7] cs --- Tests/Unit/Twig/BaseTwigTestCase.php | 17 +++------- .../Twig/DefaultApplyingNodeVisitorTest.php | 15 +++------ .../Unit/Twig/NormalizingNodeVisitorTest.php | 15 +++------ Tests/Unit/Twig/RemovingNodeVisitorTest.php | 15 +++------ Twig/Node/Transchoice.php | 17 +++------- Twig/TranslationExtension.php | 32 ++++++++++--------- Twig/Visitor/DefaultApplyingNodeVisitor.php | 18 ++++++----- Twig/Visitor/NormalizingNodeVisitor.php | 6 ++-- Twig/Visitor/RemovingNodeVisitor.php | 6 ++-- 9 files changed, 57 insertions(+), 84 deletions(-) diff --git a/Tests/Unit/Twig/BaseTwigTestCase.php b/Tests/Unit/Twig/BaseTwigTestCase.php index 65282b08..76d8d6dd 100644 --- a/Tests/Unit/Twig/BaseTwigTestCase.php +++ b/Tests/Unit/Twig/BaseTwigTestCase.php @@ -1,19 +1,12 @@ + * This file is part of the PHP Translation package. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * (c) PHP Translation team * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace Translation\Bundle\Tests\Unit\Twig; @@ -30,7 +23,7 @@ final protected function parse($file, $debug = false) { $content = file_get_contents(__DIR__.'/Fixture/'.$file); - $env = new \Twig_Environment(new \Twig_Loader_Array(array())); + $env = new \Twig_Environment(new \Twig_Loader_Array([])); $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector()))); $env->addExtension(new TranslationExtension($translator, $debug)); diff --git a/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php b/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php index 5b234364..7f80f65b 100644 --- a/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php +++ b/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php @@ -1,19 +1,12 @@ + * This file is part of the PHP Translation package. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * (c) PHP Translation team * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace Translation\Bundle\Tests\Unit\Twig; diff --git a/Tests/Unit/Twig/NormalizingNodeVisitorTest.php b/Tests/Unit/Twig/NormalizingNodeVisitorTest.php index 447c3d68..aee3bcb0 100644 --- a/Tests/Unit/Twig/NormalizingNodeVisitorTest.php +++ b/Tests/Unit/Twig/NormalizingNodeVisitorTest.php @@ -1,19 +1,12 @@ + * This file is part of the PHP Translation package. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * (c) PHP Translation team * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace Translation\Bundle\Tests\Unit\Twig; diff --git a/Tests/Unit/Twig/RemovingNodeVisitorTest.php b/Tests/Unit/Twig/RemovingNodeVisitorTest.php index 58deb0fd..b2521ca0 100644 --- a/Tests/Unit/Twig/RemovingNodeVisitorTest.php +++ b/Tests/Unit/Twig/RemovingNodeVisitorTest.php @@ -1,19 +1,12 @@ + * This file is part of the PHP Translation package. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * (c) PHP Translation team * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace Translation\Bundle\Tests\Unit\Twig; diff --git a/Twig/Node/Transchoice.php b/Twig/Node/Transchoice.php index 2b754fa8..654688f3 100644 --- a/Twig/Node/Transchoice.php +++ b/Twig/Node/Transchoice.php @@ -1,19 +1,12 @@ + * This file is part of the PHP Translation package. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * (c) PHP Translation team * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace Translation\Bundle\Twig\Node; @@ -22,7 +15,7 @@ class Transchoice extends \Twig_Node_Expression { public function __construct(\Twig_Node_Expression_Array $arguments, $lineno) { - parent::__construct(array('arguments' => $arguments), array(), $lineno); + parent::__construct(['arguments' => $arguments], [], $lineno); } public function compile(\Twig_Compiler $compiler) diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 72621e26..02c556a2 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -17,7 +17,6 @@ use Translation\Bundle\Twig\Visitor\RemovingNodeVisitor; /** - * * @author Johannes M. Schmitt * @author Tobias Nyholm */ @@ -35,7 +34,7 @@ final class TranslationExtension extends \Twig_Extension /** * @param TranslatorInterface $translator - * @param bool $debug + * @param bool $debug */ public function __construct(TranslatorInterface $translator, $debug = false) { @@ -48,10 +47,10 @@ public function __construct(TranslatorInterface $translator, $debug = false) */ public function getFilters() { - return array( - new \Twig_SimpleFilter('desc', array($this, 'desc')), - new \Twig_SimpleFilter('meaning', array($this, 'meaning')), - ); + return [ + new \Twig_SimpleFilter('desc', [$this, 'desc']), + new \Twig_SimpleFilter('meaning', [$this, 'meaning']), + ]; } /** @@ -59,10 +58,10 @@ public function getFilters() */ public function getNodeVisitors() { - $visitors = array( + $visitors = [ new NormalizingNodeVisitor(), new RemovingNodeVisitor(), - ); + ]; if ($this->debug) { $visitors[] = new DefaultApplyingNodeVisitor(); @@ -72,29 +71,31 @@ public function getNodeVisitors() } /** - * @param string $message - * @param string $defaultMessage - * @param int $count - * @param array $arguments + * @param string $message + * @param string $defaultMessage + * @param int $count + * @param array $arguments * @param null|string $domain * @param null|string $locale + * * @return string */ - public function transchoiceWithDefault($message, $defaultMessage, $count, array $arguments = array(), $domain = null, $locale = null) + public function transchoiceWithDefault($message, $defaultMessage, $count, array $arguments = [], $domain = null, $locale = null) { if (null === $domain) { $domain = 'messages'; } if (false === $this->translator->getCatalogue($locale)->defines($message, $domain)) { - return $this->translator->transChoice($defaultMessage, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale); + return $this->translator->transChoice($defaultMessage, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale); } - return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale); + return $this->translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale); } /** * @param $v + * * @return mixed */ public function desc($v) @@ -104,6 +105,7 @@ public function desc($v) /** * @param $v + * * @return mixed */ public function meaning($v) diff --git a/Twig/Visitor/DefaultApplyingNodeVisitor.php b/Twig/Visitor/DefaultApplyingNodeVisitor.php index 78d632dd..a6618eaa 100644 --- a/Twig/Visitor/DefaultApplyingNodeVisitor.php +++ b/Twig/Visitor/DefaultApplyingNodeVisitor.php @@ -37,8 +37,9 @@ public function setEnabled($bool) } /** - * @param \Twig_Node $node + * @param \Twig_Node $node * @param \Twig_Environment $env + * * @return \Twig_Node */ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env) @@ -67,7 +68,7 @@ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env) // so that we can catch a possible exception when the default translation has not yet // been extracted if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) { - $transchoiceArguments = new \Twig_Node_Expression_Array(array(), $transNode->getTemplateLine()); + $transchoiceArguments = new \Twig_Node_Expression_Array([], $transNode->getTemplateLine()); $transchoiceArguments->addElement($wrappingNode->getNode('node')); $transchoiceArguments->addElement($defaultNode); foreach ($wrappingNode->getNode('arguments') as $arg) { @@ -83,19 +84,19 @@ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env) // if the |trans filter has replacements parameters // (e.g. |trans({'%foo%': 'bar'})) if ($wrappingNode->getNode('arguments')->hasNode(0)) { - $lineno = $wrappingNode->getTemplateLine(); + $lineno = $wrappingNode->getTemplateLine(); // remove the replacements from the test node $testNode->setNode('arguments', clone $testNode->getNode('arguments')); - $testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array(array(), $lineno)); + $testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array([], $lineno)); // wrap the default node in a |replace filter $defaultNode = new \Twig_Node_Expression_Filter( clone $node->getNode('arguments')->getNode(0), new \Twig_Node_Expression_Constant('replace', $lineno), - new \Twig_Node(array( - clone $wrappingNode->getNode('arguments')->getNode(0) - )), + new \Twig_Node([ + clone $wrappingNode->getNode('arguments')->getNode(0), + ]), $lineno ); } @@ -113,8 +114,9 @@ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env) } /** - * @param \Twig_Node $node + * @param \Twig_Node $node * @param \Twig_Environment $env + * * @return \Twig_Node */ public function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) diff --git a/Twig/Visitor/NormalizingNodeVisitor.php b/Twig/Visitor/NormalizingNodeVisitor.php index ee8d0cdc..9b600bd4 100644 --- a/Twig/Visitor/NormalizingNodeVisitor.php +++ b/Twig/Visitor/NormalizingNodeVisitor.php @@ -22,8 +22,9 @@ final class NormalizingNodeVisitor extends \Twig_BaseNodeVisitor { /** - * @param \Twig_Node $node + * @param \Twig_Node $node * @param \Twig_Environment $env + * * @return \Twig_Node */ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) @@ -32,8 +33,9 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) } /** - * @param \Twig_Node $node + * @param \Twig_Node $node * @param \Twig_Environment $env + * * @return \Twig_Node_Expression_Constant|\Twig_Node */ protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) diff --git a/Twig/Visitor/RemovingNodeVisitor.php b/Twig/Visitor/RemovingNodeVisitor.php index f08ff4e0..ff69a3f3 100644 --- a/Twig/Visitor/RemovingNodeVisitor.php +++ b/Twig/Visitor/RemovingNodeVisitor.php @@ -32,8 +32,9 @@ public function setEnabled($bool) } /** - * @param \Twig_Node $node + * @param \Twig_Node $node * @param \Twig_Environment $env + * * @return \Twig_Node */ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) @@ -50,8 +51,9 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) } /** - * @param \Twig_Node $node + * @param \Twig_Node $node * @param \Twig_Environment $env + * * @return \Twig_Node */ protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) From d9318f2a08d176a6a3ff8fd7d3bc154cbbf92e1c Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 14 Jan 2018 20:00:35 +0100 Subject: [PATCH 7/7] Addd author docs --- Tests/Unit/Twig/BaseTwigTestCase.php | 3 +++ Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php | 3 +++ Tests/Unit/Twig/NormalizingNodeVisitorTest.php | 3 +++ Tests/Unit/Twig/RemovingNodeVisitorTest.php | 3 +++ 4 files changed, 12 insertions(+) diff --git a/Tests/Unit/Twig/BaseTwigTestCase.php b/Tests/Unit/Twig/BaseTwigTestCase.php index 76d8d6dd..7ef74766 100644 --- a/Tests/Unit/Twig/BaseTwigTestCase.php +++ b/Tests/Unit/Twig/BaseTwigTestCase.php @@ -17,6 +17,9 @@ use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension; use Translation\Bundle\Twig\TranslationExtension; +/** + * @author Johannes M. Schmitt + */ abstract class BaseTwigTestCase extends TestCase { final protected function parse($file, $debug = false) diff --git a/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php b/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php index 7f80f65b..bee3c488 100644 --- a/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php +++ b/Tests/Unit/Twig/DefaultApplyingNodeVisitorTest.php @@ -11,6 +11,9 @@ namespace Translation\Bundle\Tests\Unit\Twig; +/** + * @author Johannes M. Schmitt + */ class DefaultApplyingNodeVisitorTest extends BaseTwigTestCase { public function testApply() diff --git a/Tests/Unit/Twig/NormalizingNodeVisitorTest.php b/Tests/Unit/Twig/NormalizingNodeVisitorTest.php index aee3bcb0..1879b102 100644 --- a/Tests/Unit/Twig/NormalizingNodeVisitorTest.php +++ b/Tests/Unit/Twig/NormalizingNodeVisitorTest.php @@ -11,6 +11,9 @@ namespace Translation\Bundle\Tests\Unit\Twig; +/** + * @author Johannes M. Schmitt + */ class NormalizingNodeVisitorTest extends BaseTwigTestCase { public function testBinaryConcatOfConstants() diff --git a/Tests/Unit/Twig/RemovingNodeVisitorTest.php b/Tests/Unit/Twig/RemovingNodeVisitorTest.php index b2521ca0..09b92b4d 100644 --- a/Tests/Unit/Twig/RemovingNodeVisitorTest.php +++ b/Tests/Unit/Twig/RemovingNodeVisitorTest.php @@ -11,6 +11,9 @@ namespace Translation\Bundle\Tests\Unit\Twig; +/** + * @author Johannes M. Schmitt + */ class RemovingNodeVisitorTest extends BaseTwigTestCase { public function testRemovalWithSimpleTemplate()