Skip to content

Commit

Permalink
minor #4167 Fix various phpstan errors (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Fix various phpstan errors

Commits
-------

ae4f284 Fix various phpstan errors
  • Loading branch information
fabpot committed Jul 31, 2024
2 parents b566513 + ae4f284 commit 0d09230
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Extension/AbstractExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public function getFunctions()

public function getOperators()
{
return [];
return [[], []];
}
}
2 changes: 1 addition & 1 deletion src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ public static function batch($items, $size, $fill = null, $preserveKeys = true):
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);

Expand Down
4 changes: 2 additions & 2 deletions src/Extension/EscaperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function getDefaultStrategy(string $name)
* 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
*/
Expand All @@ -142,7 +142,7 @@ public function setEscaper($strategy, callable $callable)
/**
* 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
*/
Expand Down
7 changes: 3 additions & 4 deletions src/Extension/ExtensionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,8 +67,8 @@ public function getFunctions();
* @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();
Expand Down
9 changes: 5 additions & 4 deletions src/ExtensionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -391,7 +392,7 @@ public function getTest(string $name): ?TwigTest
}

/**
* @return array<string, array{precedence: int, class: class-string<AbstractUnary>}>
* @return array<string, array{precedence: int, class: class-string<AbstractExpression>}>
*/
public function getUnaryOperators(): array
{
Expand All @@ -403,7 +404,7 @@ public function getUnaryOperators(): array
}

/**
* @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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Expression/CallExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private function getCallableParameters($callable, bool $isVariadic): array
$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()) {
Expand Down
4 changes: 3 additions & 1 deletion src/Node/Expression/MethodCallExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public function compile(Compiler $compiler): void
->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(', ');
}
Expand Down
7 changes: 5 additions & 2 deletions src/Node/PrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public function __construct(AbstractExpression $expr, int $lineno, ?string $tag

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")
;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Runtime/EscaperRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

final class EscaperRuntime implements RuntimeExtensionInterface
{
/** @var array<string, callable(string $string, string $charset): string> */
private $escapers = [];

/** @internal */
Expand All @@ -36,7 +37,7 @@ public function __construct($charset = 'UTF-8')
* 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)
{
Expand All @@ -46,7 +47,7 @@ public function setEscaper($strategy, callable $callable)
/**
* 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()
{
Expand Down
6 changes: 5 additions & 1 deletion src/Test/NodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ protected function getCompiler(?Environment $environment = null)

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)
Expand Down
2 changes: 2 additions & 0 deletions src/Util/DeprecationCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function collect(\Traversable $iterator): array
if (\E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}

return false;
});

foreach ($iterator as $name => $contents) {
Expand Down

0 comments on commit 0d09230

Please sign in to comment.