From d4e3adaaeeecb4c1d549747368f6f15ce6078428 Mon Sep 17 00:00:00 2001 From: Possum Date: Thu, 5 Nov 2015 09:57:20 +0100 Subject: [PATCH] Code grooming --- lib/Twig/Environment.php | 20 ++++++++----------- lib/Twig/Extension/Core.php | 12 ++++++----- lib/Twig/Node.php | 2 +- lib/Twig/Node/SandboxedPrint.php | 5 ----- lib/Twig/Parser.php | 8 +++----- lib/Twig/Test/IntegrationTestCase.php | 4 ++-- lib/Twig/TokenParser/Block.php | 4 ++-- lib/Twig/TokenParserBroker.php | 5 +++-- test/Twig/Tests/EnvironmentTest.php | 7 +++---- .../Tests/Fixtures/tags/use/inheritance.test | 4 ++-- .../Tests/Fixtures/tags/use/inheritance2.test | 4 ++-- test/Twig/Tests/TemplateTest.php | 2 +- 12 files changed, 34 insertions(+), 43 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 7d4fb74438..6775d76fc1 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -961,13 +961,11 @@ public function getFilter($name) foreach ($this->filters as $pattern => $filter) { $pattern = str_replace('\\*', '(.*?)', preg_quote($pattern, '#'), $count); - if ($count) { - if (preg_match('#^'.$pattern.'$#', $name, $matches)) { - array_shift($matches); - $filter->setArguments($matches); + if ($count && preg_match('#^'.$pattern.'$#', $name, $matches)) { + array_shift($matches); + $filter->setArguments($matches); - return $filter; - } + return $filter; } } @@ -1112,13 +1110,11 @@ public function getFunction($name) foreach ($this->functions as $pattern => $function) { $pattern = str_replace('\\*', '(.*?)', preg_quote($pattern, '#'), $count); - if ($count) { - if (preg_match('#^'.$pattern.'$#', $name, $matches)) { - array_shift($matches); - $function->setArguments($matches); + if ($count && preg_match('#^'.$pattern.'$#', $name, $matches)) { + array_shift($matches); + $function->setArguments($matches); - return $function; - } + return $function; } } diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index bdfa0d9621..0aed990fc3 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -817,9 +817,10 @@ function twig_join_filter($value, $glue = '') * {# returns [aa, bb, cc] #} * * - * @param string $value A string - * @param string $delimiter The delimiter - * @param int $limit The limit + * @param Twig_Environment $env A Twig_Environment instance + * @param string $value A string + * @param string $delimiter The delimiter + * @param int $limit The limit * * @return array The split string as an array */ @@ -1476,8 +1477,9 @@ function twig_include(Twig_Environment $env, $context, $template, $variables = a /** * Returns a template content without rendering it. * - * @param string $name The template name - * @param bool $ignoreMissing Whether to ignore missing templates or not + * @param Twig_Environment $env + * @param string $name The template name + * @param bool $ignoreMissing Whether to ignore missing templates or not * * @return string The template source */ diff --git a/lib/Twig/Node.php b/lib/Twig/Node.php index 40d67fe591..45a8976810 100644 --- a/lib/Twig/Node.php +++ b/lib/Twig/Node.php @@ -101,7 +101,7 @@ public function toXml($asDom = false) $node->appendChild($child); } - return $asDom ? $dom : $dom->saveXml(); + return $asDom ? $dom : $dom->saveXML(); } public function compile(Twig_Compiler $compiler) diff --git a/lib/Twig/Node/SandboxedPrint.php b/lib/Twig/Node/SandboxedPrint.php index a192181b74..148dd2bb4a 100644 --- a/lib/Twig/Node/SandboxedPrint.php +++ b/lib/Twig/Node/SandboxedPrint.php @@ -21,11 +21,6 @@ */ class Twig_Node_SandboxedPrint extends Twig_Node_Print { - public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null) - { - parent::__construct($expr, $lineno, $tag); - } - public function compile(Twig_Compiler $compiler) { $compiler diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 6da82a4a3d..0f409dfd71 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -94,10 +94,8 @@ public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = fals try { $body = $this->subparse($test, $dropNeedle); - if (null !== $this->parent) { - if (null === $body = $this->filterBodyNodes($body)) { - $body = new Twig_Node(); - } + if (null !== $this->parent && null === $body = $this->filterBodyNodes($body)) { + $body = new Twig_Node(); } } catch (Twig_Error_Syntax $e) { if (!$e->getTemplateFile()) { @@ -148,7 +146,7 @@ public function subparse($test, $dropNeedle = false) $token = $this->getCurrentToken(); if ($token->getType() !== Twig_Token::NAME_TYPE) { - throw new Twig_Error_Syntax('A block must start with a tag name', $token->getLine(), $this->getFilename()); + throw new Twig_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->getFilename()); } if (null !== $test && call_user_func($test, $token)) { diff --git a/lib/Twig/Test/IntegrationTestCase.php b/lib/Twig/Test/IntegrationTestCase.php index 1ec575ee7d..39c435ea81 100644 --- a/lib/Twig/Test/IntegrationTestCase.php +++ b/lib/Twig/Test/IntegrationTestCase.php @@ -90,13 +90,13 @@ public function getTests($name, $legacyTests = false) if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) { $message = $match[1]; $condition = $match[2]; - $templates = $this->parseTemplates($match[3]); + $templates = static::parseTemplates($match[3]); $exception = $match[5]; $outputs = array(array(null, $match[4], null, '')); } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) { $message = $match[1]; $condition = $match[2]; - $templates = $this->parseTemplates($match[3]); + $templates = static::parseTemplates($match[3]); $exception = false; preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER); } else { diff --git a/lib/Twig/TokenParser/Block.php b/lib/Twig/TokenParser/Block.php index a39de003b0..542cc7d551 100644 --- a/lib/Twig/TokenParser/Block.php +++ b/lib/Twig/TokenParser/Block.php @@ -28,7 +28,7 @@ public function parse(Twig_Token $token) $stream = $this->parser->getStream(); $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); if ($this->parser->hasBlock($name)) { - throw new Twig_Error_Syntax(sprintf("The block '$name' has already been defined line %d", $this->parser->getBlock($name)->getLine()), $stream->getCurrent()->getLine(), $stream->getFilename()); + throw new Twig_Error_Syntax(sprintf("The block '%s' has already been defined line %d", $name, $this->parser->getBlock($name)->getLine()), $stream->getCurrent()->getLine(), $stream->getFilename()); } $this->parser->setBlock($name, $block = new Twig_Node_Block($name, new Twig_Node(array()), $lineno)); $this->parser->pushLocalScope(); @@ -40,7 +40,7 @@ public function parse(Twig_Token $token) $value = $token->getValue(); if ($value != $name) { - throw new Twig_Error_Syntax(sprintf('Expected endblock for block "%s" (but "%s" given)', $name, $value), $stream->getCurrent()->getLine(), $stream->getFilename()); + throw new Twig_Error_Syntax(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getFilename()); } } } else { diff --git a/lib/Twig/TokenParserBroker.php b/lib/Twig/TokenParserBroker.php index 6ca73fb38a..1f6c112805 100644 --- a/lib/Twig/TokenParserBroker.php +++ b/lib/Twig/TokenParserBroker.php @@ -28,6 +28,7 @@ class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface * * @param array|Traversable $parsers A Traversable of Twig_TokenParserInterface instances * @param array|Traversable $brokers A Traversable of Twig_TokenParserBrokerInterface instances + * @param bool $triggerDeprecationError */ public function __construct($parsers = array(), $brokers = array(), $triggerDeprecationError = true) { @@ -37,13 +38,13 @@ public function __construct($parsers = array(), $brokers = array(), $triggerDepr foreach ($parsers as $parser) { if (!$parser instanceof Twig_TokenParserInterface) { - throw new LogicException('$parsers must a an array of Twig_TokenParserInterface'); + throw new LogicException('$parsers must a an array of Twig_TokenParserInterface.'); } $this->parsers[$parser->getTag()] = $parser; } foreach ($brokers as $broker) { if (!$broker instanceof Twig_TokenParserBrokerInterface) { - throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface'); + throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface.'); } $this->brokers[] = $broker; } diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index a4907d17f2..18025374d0 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -11,6 +11,8 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase { + private $deprecations = array(); + /** * @expectedException LogicException * @expectedExceptionMessage You must set a loader first. @@ -282,7 +284,6 @@ public function testAddExtensionWithDeprecatedGetGlobals() $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension_WithGlobals()); - $this->deprecations = array(); set_error_handler(array($this, 'handleError')); $this->assertArrayHasKey('foo_global', $twig->getGlobals()); @@ -344,7 +345,6 @@ public function testInitRuntimeWithAnExtensionUsingInitRuntimeDeprecation() $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addExtension(new Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime()); - $this->deprecations = array(); set_error_handler(array($this, 'handleError')); $twig->initRuntime(); @@ -365,12 +365,11 @@ public function handleError($type, $msg) /** * @requires PHP 5.3 */ - public function testOverrideExtenion() + public function testOverrideExtension() { $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addExtension(new Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime()); - $this->deprecations = array(); set_error_handler(array($this, 'handleError')); $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension()); diff --git a/test/Twig/Tests/Fixtures/tags/use/inheritance.test b/test/Twig/Tests/Fixtures/tags/use/inheritance.test index 6368b08de0..0d0d470ee6 100644 --- a/test/Twig/Tests/Fixtures/tags/use/inheritance.test +++ b/test/Twig/Tests/Fixtures/tags/use/inheritance.test @@ -8,7 +8,7 @@ {% use "ancestor.twig" %} {% block sub_container %} -
overriden sub_container
+
overridden sub_container
{% endblock %} --TEMPLATE(ancestor.twig)-- {% block container %} @@ -21,5 +21,5 @@ --DATA-- return array() --EXPECT-- -
overriden sub_container
+
overridden sub_container
diff --git a/test/Twig/Tests/Fixtures/tags/use/inheritance2.test b/test/Twig/Tests/Fixtures/tags/use/inheritance2.test index 114e3015e9..df95599ce1 100644 --- a/test/Twig/Tests/Fixtures/tags/use/inheritance2.test +++ b/test/Twig/Tests/Fixtures/tags/use/inheritance2.test @@ -7,7 +7,7 @@ {{ block('container') }} --TEMPLATE(parent.twig)-- {% block sub_container %} -
overriden sub_container
+
overridden sub_container
{% endblock %} --TEMPLATE(ancestor.twig)-- {% block container %} @@ -20,5 +20,5 @@ --DATA-- return array() --EXPECT-- -
overriden sub_container
+
overridden sub_container
diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 37836fdc86..f014664964 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -666,7 +666,7 @@ class Twig_TemplateMagicMethodExceptionObject { public function __call($method, $arguments) { - throw new BadMethodCallException(sprintf('Unkown method %s', $method)); + throw new BadMethodCallException(sprintf('Unknown method "%s".', $method)); } }