mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-21 00:32:32 +00:00
Fxi some errors reported by phpstan
This commit is contained in:
@@ -40,13 +40,13 @@ class CacheTokenParser extends AbstractTokenParser
|
||||
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());
|
||||
|
||||
+2
-2
@@ -437,7 +437,7 @@ class Environment
|
||||
$count = \count($names);
|
||||
foreach ($names as $name) {
|
||||
if ($name instanceof Template) {
|
||||
return $name;
|
||||
return new TemplateWrapper($this, $name);
|
||||
}
|
||||
if ($name instanceof TemplateWrapper) {
|
||||
return $name;
|
||||
@@ -535,7 +535,7 @@ class Environment
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
@@ -452,14 +452,14 @@ class ExpressionParser
|
||||
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);
|
||||
|
||||
@@ -1124,7 +1124,7 @@ final class CoreExtension extends AbstractExtension
|
||||
/**
|
||||
* Removes whitespaces between HTML tags.
|
||||
*
|
||||
* @param string|null $string
|
||||
* @param string|null $content
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
@@ -1241,11 +1241,7 @@ final class CoreExtension extends AbstractExtension
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1436,6 +1432,8 @@ final class CoreExtension extends AbstractExtension
|
||||
if (!$ignoreMissing) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1731,9 +1729,9 @@ final class CoreExtension extends AbstractExtension
|
||||
* {# 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
|
||||
*
|
||||
|
||||
@@ -175,7 +175,7 @@ final class EscaperExtension extends AbstractExtension
|
||||
* @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
|
||||
*/
|
||||
|
||||
@@ -35,7 +35,7 @@ class DefaultFilter extends FilterExpression
|
||||
|
||||
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 {
|
||||
|
||||
@@ -33,16 +33,16 @@ class ConstantTest extends TestExpression
|
||||
->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('))')
|
||||
;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ class DivisiblebyTest extends TestExpression
|
||||
->raw('(0 == ')
|
||||
->subcompile($this->getNode('node'))
|
||||
->raw(' % ')
|
||||
->subcompile($this->getNode('arguments')->getNode(0))
|
||||
->subcompile($this->getNode('arguments')->getNode('0'))
|
||||
->raw(')')
|
||||
;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class SameasTest extends TestExpression
|
||||
->raw('(')
|
||||
->subcompile($this->getNode('node'))
|
||||
->raw(' === ')
|
||||
->subcompile($this->getNode('arguments')->getNode(0))
|
||||
->subcompile($this->getNode('arguments')->getNode('0'))
|
||||
->raw(')')
|
||||
;
|
||||
}
|
||||
|
||||
+3
-3
@@ -47,13 +47,13 @@ class IfNode extends Node
|
||||
}
|
||||
|
||||
$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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -381,7 +381,7 @@ final class ModuleNode extends Node
|
||||
$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');
|
||||
}
|
||||
@@ -418,7 +418,7 @@ final class ModuleNode extends Node
|
||||
->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")
|
||||
;
|
||||
|
||||
@@ -131,10 +131,6 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
|
||||
|
||||
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)) {
|
||||
|
||||
@@ -101,6 +101,9 @@ class Parser
|
||||
|
||||
$traverser = new NodeTraverser($this->env, $this->visitors);
|
||||
|
||||
/**
|
||||
* @var ModuleNode $node
|
||||
*/
|
||||
$node = $traverser->traverse($node);
|
||||
|
||||
// restore previous stack so previous parse() call can resume working
|
||||
|
||||
@@ -149,6 +149,7 @@ abstract class IntegrationTestCase extends TestCase
|
||||
}
|
||||
|
||||
if ($condition) {
|
||||
$ret = '';
|
||||
eval('$ret = '.$condition.';');
|
||||
if (!$ret) {
|
||||
$this->markTestSkipped($condition);
|
||||
|
||||
@@ -49,12 +49,12 @@ final class ForTokenParser extends AbstractTokenParser
|
||||
$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());
|
||||
|
||||
|
||||
+1
-3
@@ -60,9 +60,7 @@ final class TokenStream
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,7 @@ class FilesystemTest extends TestCase
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$nonce = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', uniqid(mt_rand(), true));
|
||||
$nonce = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', uniqid((string) mt_rand(), true));
|
||||
$this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$nonce;
|
||||
$this->directory = sys_get_temp_dir().'/twig-test';
|
||||
$this->cache = new FilesystemCache($this->directory);
|
||||
|
||||
@@ -22,7 +22,7 @@ class CompilerTest extends TestCase
|
||||
{
|
||||
$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.');
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class CompilerTest extends TestCase
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ class ExpressionParserTest extends TestCase
|
||||
$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'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,7 +217,7 @@ class ExpressionParserTest extends TestCase
|
||||
$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()
|
||||
|
||||
@@ -22,7 +22,7 @@ class ArrayTest extends NodeTestCase
|
||||
$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()
|
||||
|
||||
@@ -33,7 +33,7 @@ class ForTest extends NodeTestCase
|
||||
$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);
|
||||
|
||||
@@ -28,7 +28,7 @@ class OptimizerTest extends TestCase
|
||||
|
||||
$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'));
|
||||
@@ -40,7 +40,7 @@ class OptimizerTest extends TestCase
|
||||
|
||||
$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'));
|
||||
|
||||
Reference in New Issue
Block a user