Skip to content

Commit

Permalink
Code grooming
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Nov 5, 2015
1 parent fd84403 commit d4e3ada
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 43 deletions.
20 changes: 8 additions & 12 deletions lib/Twig/Environment.php
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down
12 changes: 7 additions & 5 deletions lib/Twig/Extension/Core.php
Expand Up @@ -817,9 +817,10 @@ function twig_join_filter($value, $glue = '')
* {# returns [aa, bb, cc] #}
* </pre>
*
* @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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node.php
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions lib/Twig/Node/SandboxedPrint.php
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions lib/Twig/Parser.php
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/Test/IntegrationTestCase.php
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/TokenParser/Block.php
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions lib/Twig/TokenParserBroker.php
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down
7 changes: 3 additions & 4 deletions test/Twig/Tests/EnvironmentTest.php
Expand Up @@ -11,6 +11,8 @@

class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
{
private $deprecations = array();

/**
* @expectedException LogicException
* @expectedExceptionMessage You must set a loader first.
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions test/Twig/Tests/Fixtures/tags/use/inheritance.test
Expand Up @@ -8,7 +8,7 @@
{% use "ancestor.twig" %}

{% block sub_container %}
<div class="overriden_sub_container">overriden sub_container</div>
<div class="overridden_sub_container">overridden sub_container</div>
{% endblock %}
--TEMPLATE(ancestor.twig)--
{% block container %}
Expand All @@ -21,5 +21,5 @@
--DATA--
return array()
--EXPECT--
<div class="container"> <div class="overriden_sub_container">overriden sub_container</div>
<div class="container"> <div class="overridden_sub_container">overridden sub_container</div>
</div>
4 changes: 2 additions & 2 deletions test/Twig/Tests/Fixtures/tags/use/inheritance2.test
Expand Up @@ -7,7 +7,7 @@
{{ block('container') }}
--TEMPLATE(parent.twig)--
{% block sub_container %}
<div class="overriden_sub_container">overriden sub_container</div>
<div class="overridden_sub_container">overridden sub_container</div>
{% endblock %}
--TEMPLATE(ancestor.twig)--
{% block container %}
Expand All @@ -20,5 +20,5 @@
--DATA--
return array()
--EXPECT--
<div class="container"> <div class="overriden_sub_container">overriden sub_container</div>
<div class="container"> <div class="overridden_sub_container">overridden sub_container</div>
</div>
2 changes: 1 addition & 1 deletion test/Twig/Tests/TemplateTest.php
Expand Up @@ -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));
}
}

Expand Down

0 comments on commit d4e3ada

Please sign in to comment.