minor #4167 Fix various phpstan errors (fabpot)

This PR was merged into the 3.x branch.

Discussion
----------

Fix various phpstan errors

Commits
-------

ae4f2840 Fix various phpstan errors
This commit is contained in:
Fabien Potencier
2024-07-31 09:30:45 +02:00
11 changed files with 31 additions and 19 deletions
+1 -1
View File
@@ -40,6 +40,6 @@ abstract class AbstractExtension implements ExtensionInterface
public function getOperators()
{
return [];
return [[], []];
}
}
+1 -1
View File
@@ -1516,7 +1516,7 @@ final class CoreExtension extends AbstractExtension
throw new RuntimeError(\sprintf('The "batch" filter expects a sequence/mapping or "Traversable", got "%s".', \is_object($items) ? \get_class($items) : \gettype($items)));
}
$size = ceil($size);
$size = (int) ceil($size);
$result = array_chunk(self::toArray($items, $preserveKeys), $size, $preserveKeys);
+2 -2
View File
@@ -119,7 +119,7 @@ final class EscaperExtension extends AbstractExtension
* Defines a new escaper to be used via the escape filter.
*
* @param string $strategy The strategy name that should be used as a strategy in the escape call
* @param callable(Environment, string, string) $callable A valid PHP callable
* @param callable(Environment, string, string): string $callable A valid PHP callable
*
* @deprecated since Twig 3.10
*/
@@ -142,7 +142,7 @@ final class EscaperExtension extends AbstractExtension
/**
* Gets all defined escapers.
*
* @return array<callable(Environment, string, string)> An array of escapers
* @return array<string, callable(Environment, string, string): string> An array of escapers
*
* @deprecated since Twig 3.10
*/
+3 -4
View File
@@ -12,8 +12,7 @@
namespace Twig\Extension;
use Twig\ExpressionParser;
use Twig\Node\Expression\Binary\AbstractBinary;
use Twig\Node\Expression\Unary\AbstractUnary;
use Twig\Node\Expression\AbstractExpression;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\TokenParser\TokenParserInterface;
use Twig\TwigFilter;
@@ -68,8 +67,8 @@ interface ExtensionInterface
* @return array<array> First array of unary operators, second array of binary operators
*
* @psalm-return array{
* array<string, array{precedence: int, class: class-string<AbstractUnary>}>,
* array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}>
* array<string, array{precedence: int, class: class-string<AbstractExpression>}>,
* array<string, array{precedence: int, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}>
* }
*/
public function getOperators();
+5 -4
View File
@@ -15,6 +15,7 @@ use Twig\Error\RuntimeError;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\GlobalsInterface;
use Twig\Extension\StagingExtension;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\Binary\AbstractBinary;
use Twig\Node\Expression\Unary\AbstractUnary;
use Twig\NodeVisitor\NodeVisitorInterface;
@@ -39,9 +40,9 @@ final class ExtensionSet
private $tests;
/** @var array<string, TwigFunction> */
private $functions;
/** @var array<string, array{precedence: int, class: class-string<AbstractUnary>}> */
/** @var array<string, array{precedence: int, class: class-string<AbstractExpression>}> */
private $unaryOperators;
/** @var array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}> */
/** @var array<string, array{precedence: int, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}> */
private $binaryOperators;
/** @var array<string, mixed> */
private $globals;
@@ -391,7 +392,7 @@ final class ExtensionSet
}
/**
* @return array<string, array{precedence: int, class: class-string<AbstractUnary>}>
* @return array<string, array{precedence: int, class: class-string<AbstractExpression>}>
*/
public function getUnaryOperators(): array
{
@@ -403,7 +404,7 @@ final class ExtensionSet
}
/**
* @return array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}>
* @return array<string, array{precedence: int, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}>
*/
public function getBinaryOperators(): array
{
+1 -1
View File
@@ -279,7 +279,7 @@ abstract class CallExpression extends AbstractExpression
$isPhpVariadic = false;
if ($isVariadic) {
$argument = end($parameters);
$isArray = $argument && $argument->hasType() && 'array' === $argument->getType()->getName();
$isArray = $argument && $argument->hasType() && $argument->getType() instanceof \ReflectionNamedType && 'array' === $argument->getType()->getName();
if ($isArray && $argument->isDefaultValueAvailable() && [] === $argument->getDefaultValue()) {
array_pop($parameters);
} elseif ($argument && $argument->isVariadic()) {
+3 -1
View File
@@ -46,7 +46,9 @@ class MethodCallExpression extends AbstractExpression
->raw(', [')
;
$first = true;
foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
/** @var ArrayExpression */
$args = $this->getNode('arguments');
foreach ($args->getKeyValuePairs() as $pair) {
if (!$first) {
$compiler->raw(', ');
}
+5 -2
View File
@@ -31,10 +31,13 @@ class PrintNode extends Node implements NodeOutputInterface
public function compile(Compiler $compiler): void
{
/** @var AbstractExpression */
$expr = $this->getNode('expr');
$compiler
->addDebugInfo($this)
->write($this->getNode('expr')->isGenerator() ? 'yield from ' : 'yield ')
->subcompile($this->getNode('expr'))
->write($expr->isGenerator() ? 'yield from ' : 'yield ')
->subcompile($expr)
->raw(";\n")
;
}
+3 -2
View File
@@ -17,6 +17,7 @@ use Twig\Markup;
final class EscaperRuntime implements RuntimeExtensionInterface
{
/** @var array<string, callable(string $string, string $charset): string> */
private $escapers = [];
/** @internal */
@@ -36,7 +37,7 @@ final class EscaperRuntime implements RuntimeExtensionInterface
* Defines a new escaper to be used via the escape filter.
*
* @param string $strategy The strategy name that should be used as a strategy in the escape call
* @param callable(string $string, string $charset) $callable A valid PHP callable
* @param callable(string $string, string $charset): string $callable A valid PHP callable
*/
public function setEscaper($strategy, callable $callable)
{
@@ -46,7 +47,7 @@ final class EscaperRuntime implements RuntimeExtensionInterface
/**
* Gets all defined escapers.
*
* @return array<callable(string $string, string $charset)> An array of escapers
* @return array<string, callable(string $string, string $charset): string> An array of escapers
*/
public function getEscapers()
{
+5 -1
View File
@@ -53,7 +53,11 @@ abstract class NodeTestCase extends TestCase
protected function getEnvironment()
{
return $this->currentEnv = new Environment(new ArrayLoader());
if (!$this->currentEnv) {
$this->currentEnv = new Environment(new ArrayLoader());
}
return $this->currentEnv;
}
protected function getVariableGetter($name, $line = false)
+2
View File
@@ -60,6 +60,8 @@ final class DeprecationCollector
if (\E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}
return false;
});
foreach ($iterator as $name => $contents) {