Skip to content

Commit

Permalink
Merge branch '3.x' into 4.x
Browse files Browse the repository at this point in the history
* 3.x:
  Fxi some errors reported by phpstan
  • Loading branch information
fabpot committed Feb 5, 2024
2 parents 75c2f5f + 7e1945a commit f987153
Show file tree
Hide file tree
Showing 22 changed files with 41 additions and 45 deletions.
4 changes: 2 additions & 2 deletions extra/cache-extra/TokenParser/CacheTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public function parse(Token $token): Node
if (1 !== \count($args)) {
throw new SyntaxError(sprintf('The "ttl" modifier takes exactly one argument (%d given).', \count($args)), $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
$ttl = $args->getNode(0);
$ttl = $args->getNode('0');
break;
case 'tags':
if (1 !== \count($args)) {
throw new SyntaxError(sprintf('The "tags" modifier takes exactly one argument (%d given).', \count($args)), $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
$tags = $args->getNode(0);
$tags = $args->getNode('0');
break;
default:
throw new SyntaxError(sprintf('Unknown "%s" configuration.', $k), $stream->getCurrent()->getLine(), $stream->getSourceContext());
Expand Down
2 changes: 1 addition & 1 deletion src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ public function getLoader(): LoaderInterface

public function setCharset(string $charset)
{
if ('UTF8' === $charset = null === $charset ? null : strtoupper($charset)) {
if ('UTF8' === $charset = strtoupper($charset ?: '')) {
// iconv on Windows requires "UTF-8" instead of "UTF8"
$charset = 'UTF-8';
}
Expand Down
4 changes: 2 additions & 2 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,14 @@ public function getFunctionNode($name, $line)
throw new SyntaxError('The "block" function takes one argument (the block name).', $line, $this->parser->getStream()->getSourceContext());
}

return new BlockReferenceExpression($args->getNode(0), \count($args) > 1 ? $args->getNode(1) : null, $line);
return new BlockReferenceExpression($args->getNode('0'), \count($args) > 1 ? $args->getNode('1') : null, $line);
case 'attribute':
$args = $this->parseArguments();
if (\count($args) < 2) {
throw new SyntaxError('The "attribute" function takes at least two arguments (the variable and the attributes).', $line, $this->parser->getStream()->getSourceContext());
}

return new GetAttrExpression($args->getNode(0), $args->getNode(1), \count($args) > 2 ? $args->getNode(2) : null, Template::ANY_CALL, $line);
return new GetAttrExpression($args->getNode('0'), $args->getNode('1'), \count($args) > 2 ? $args->getNode('2') : null, Template::ANY_CALL, $line);
default:
if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) {
$arguments = new ArrayExpression([], $line);
Expand Down
16 changes: 7 additions & 9 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ public static function nl2br($string)
/**
* Removes whitespaces between HTML tags.
*
* @param string|null $string
* @param string|null $content
*
* @return string
*
Expand Down Expand Up @@ -1231,11 +1231,7 @@ public static function striptags($string, $allowable_tags = null)
*/
public static function titleStringFilter(Environment $env, $string)
{
if (null !== $charset = $env->getCharset()) {
return mb_convert_case($string ?? '', \MB_CASE_TITLE, $charset);
}

return ucwords(strtolower($string ?? ''));
return mb_convert_case($string ?? '', \MB_CASE_TITLE, $env->getCharset());
}

/**
Expand Down Expand Up @@ -1405,6 +1401,8 @@ public static function source(Environment $env, $name, $ignoreMissing = false)
if (!$ignoreMissing) {
throw $e;
}

return '';
}
}

Expand Down Expand Up @@ -1700,9 +1698,9 @@ public static function getAttribute(Environment $env, Source $source, $object, $
* {# fruits now contains ['apple', 'orange'] #}
* </pre>
*
* @param array|Traversable $array An array
* @param mixed $name The column name
* @param mixed $index The column to use as the index/keys for the returned array
* @param array|\Traversable $array An array
* @param mixed $name The column name
* @param mixed $index The column to use as the index/keys for the returned array
*
* @return array The array of values
*
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/EscaperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static function escapeFilterIsSafe(Node $filterArgs)
* @param string $charset The charset
* @param bool $autoescape Whether the function is called by the auto-escaping feature (true) or by the developer (false)
*
* @return string
* @return string|Markup
*
* @internal
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Expression/Filter/DefaultFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(Node $node, ConstantExpression $filterName, Node $ar

if ('default' === $filterName->getAttribute('value') && ($node instanceof NameExpression || $node instanceof GetAttrExpression)) {
$test = new DefinedTest(clone $node, 'defined', new Node(), $node->getTemplateLine());
$false = \count($arguments) ? $arguments->getNode(0) : new ConstantExpression('', $node->getTemplateLine());
$false = \count($arguments) ? $arguments->getNode('0') : new ConstantExpression('', $node->getTemplateLine());

$node = new ConditionalExpression($test, $default, $false, $node->getTemplateLine());
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/Node/Expression/Test/ConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public function compile(Compiler $compiler): void
->raw(' === constant(')
;

if ($this->getNode('arguments')->hasNode(1)) {
if ($this->getNode('arguments')->hasNode('1')) {
$compiler
->raw('get_class(')
->subcompile($this->getNode('arguments')->getNode(1))
->subcompile($this->getNode('arguments')->getNode('1'))
->raw(')."::".')
;
}

$compiler
->subcompile($this->getNode('arguments')->getNode(0))
->subcompile($this->getNode('arguments')->getNode('0'))
->raw('))')
;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Expression/Test/DivisiblebyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function compile(Compiler $compiler): void
->raw('(0 == ')
->subcompile($this->getNode('node'))
->raw(' % ')
->subcompile($this->getNode('arguments')->getNode(0))
->subcompile($this->getNode('arguments')->getNode('0'))
->raw(')')
;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Expression/Test/SameasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function compile(Compiler $compiler): void
->raw('(')
->subcompile($this->getNode('node'))
->raw(' === ')
->subcompile($this->getNode('arguments')->getNode(0))
->subcompile($this->getNode('arguments')->getNode('0'))
->raw(')')
;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Node/IfNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public function compile(Compiler $compiler): void
}

$compiler
->subcompile($this->getNode('tests')->getNode($i))
->subcompile($this->getNode('tests')->getNode((string) $i))
->raw(") {\n")
->indent()
;
// The node might not exists if the content is empty
if ($this->getNode('tests')->hasNode($i + 1)) {
$compiler->subcompile($this->getNode('tests')->getNode($i + 1));
if ($this->getNode('tests')->hasNode((string) ($i + 1))) {
$compiler->subcompile($this->getNode('tests')->getNode((string) ($i + 1)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Node/ModuleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ protected function compileIsTraitable(Compiler $compiler)
$traitable = !$this->hasNode('parent') && 0 === \count($this->getNode('macros'));
if ($traitable) {
if ($this->getNode('body') instanceof BodyNode) {
$nodes = $this->getNode('body')->getNode(0);
$nodes = $this->getNode('body')->getNode('0');
} else {
$nodes = $this->getNode('body');
}
Expand Down Expand Up @@ -425,7 +425,7 @@ protected function compileIsTraitable(Compiler $compiler)
->write(" */\n")
->write("public function isTraitable()\n", "{\n")
->indent()
->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
->write("return false;\n")
->outdent()
->write("}\n\n")
;
Expand Down
4 changes: 0 additions & 4 deletions src/NodeVisitor/EscaperNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ private function escapeInlinePrintNode(InlinePrint $node, Environment $env, stri

private function escapePrintNode(PrintNode $node, Environment $env, string $type): Node
{
if (false === $type) {
return $node;
}

$expression = $node->getNode('expr');

if ($this->isSafeFor($type, $expression, $env)) {
Expand Down
3 changes: 3 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public function parse(TokenStream $stream, $test = null, bool $dropNeedle = fals

$traverser = new NodeTraverser($this->env, $this->visitors);

/**
* @var ModuleNode $node
*/
$node = $traverser->traverse($node);

// restore previous stack so previous parse() call can resume working
Expand Down
1 change: 1 addition & 0 deletions src/Test/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e
}

if ($condition) {
$ret = '';
eval('$ret = '.$condition.';');
if (!$ret) {
$this->markTestSkipped($condition);
Expand Down
6 changes: 3 additions & 3 deletions src/TokenParser/ForTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public function parse(Token $token): Node
$stream->expect(/* Token::BLOCK_END_TYPE */ 3);

if (\count($targets) > 1) {
$keyTarget = $targets->getNode(0);
$keyTarget = $targets->getNode('0');
$keyTarget = new AssignNameExpression($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
$valueTarget = $targets->getNode(1);
$valueTarget = $targets->getNode('1');
} else {
$keyTarget = new AssignNameExpression('_key', $lineno);
$valueTarget = $targets->getNode(0);
$valueTarget = $targets->getNode('0');
}
$valueTarget = new AssignNameExpression($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());

Expand Down
4 changes: 1 addition & 3 deletions src/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public function next(): Token
*/
public function nextIf($primary, $secondary = null)
{
if ($this->tokens[$this->current]->test($primary, $secondary)) {
return $this->next();
}
return $this->tokens[$this->current]->test($primary, $secondary) ? $this->next() : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Cache/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FilesystemTest extends TestCase

protected function setUp(): void
{
$this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.hash('xxh128', uniqid(mt_rand(), true));
$this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.hash('xxh128', uniqid((string) mt_rand(), true));
$this->directory = sys_get_temp_dir().'/twig-test';
$this->cache = new FilesystemCache($this->directory);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testReprNumericValueWithLocale()
{
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));

$locale = setlocale(\LC_NUMERIC, 0);
$locale = setlocale(\LC_NUMERIC, '0');
if (false === $locale) {
$this->markTestSkipped('Your platform does not support locales.');
}
Expand All @@ -33,7 +33,7 @@ public function testReprNumericValueWithLocale()
}

$this->assertEquals('1.2', $compiler->repr(1.2)->getSource());
$this->assertStringContainsString('fr', strtolower(setlocale(\LC_NUMERIC, 0)));
$this->assertStringContainsString('fr', strtolower(setlocale(\LC_NUMERIC, '0')));

setlocale(\LC_NUMERIC, $locale);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ExpressionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testArrayExpression($template, $expected)
$parser = new Parser($env);
$expected->setSourceContext($source);

$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode('0')->getNode('expr'));
}

/**
Expand Down Expand Up @@ -217,7 +217,7 @@ public function testStringExpression($template, $expected)
$parser = new Parser($env);
$expected->setSourceContext($source);

$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode('0')->getNode('expr'));
}

public function getTestsForString()
Expand Down
2 changes: 1 addition & 1 deletion tests/Node/Expression/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testConstructor()
$elements = [new ConstantExpression('foo', 1), $foo = new ConstantExpression('bar', 1)];
$node = new ArrayExpression($elements, 1);

$this->assertEquals($foo, $node->getNode(1));
$this->assertEquals($foo, $node->getNode('1'));
}

public function getTests()
Expand Down
2 changes: 1 addition & 1 deletion tests/Node/ForTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testConstructor()
$this->assertEquals($keyTarget, $node->getNode('key_target'));
$this->assertEquals($valueTarget, $node->getNode('value_target'));
$this->assertEquals($seq, $node->getNode('seq'));
$this->assertEquals($body, $node->getNode('body')->getNode(0));
$this->assertEquals($body, $node->getNode('body')->getNode('0'));
$this->assertFalse($node->hasNode('else'));

$else = new PrintNode(new NameExpression('foo', 1), 1);
Expand Down
4 changes: 2 additions & 2 deletions tests/NodeVisitor/OptimizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testRenderBlockOptimizer()

$stream = $env->parse($env->tokenize(new Source('{{ block("foo") }}', 'index')));

$node = $stream->getNode('body')->getNode(0);
$node = $stream->getNode('body')->getNode('0');

$this->assertInstanceOf(BlockReferenceExpression::class, $node);
$this->assertTrue($node->getAttribute('output'));
Expand All @@ -40,7 +40,7 @@ public function testRenderParentBlockOptimizer()

$stream = $env->parse($env->tokenize(new Source('{% extends "foo" %}{% block content %}{{ parent() }}{% endblock %}', 'index')));

$node = $stream->getNode('blocks')->getNode('content')->getNode(0)->getNode('body');
$node = $stream->getNode('blocks')->getNode('content')->getNode('0')->getNode('body');

$this->assertInstanceOf(ParentExpression::class, $node);
$this->assertTrue($node->getAttribute('output'));
Expand Down

0 comments on commit f987153

Please sign in to comment.