Add return types missed in #4211

This commit is contained in:
Simon André
2024-08-17 23:16:22 +02:00
committed by Fabien Potencier
parent 15a58aaffb
commit 21f08dcfe2
7 changed files with 29 additions and 34 deletions
+20 -20
View File
@@ -360,7 +360,7 @@ This is used by many of the tests built into Twig::
class OddTestExpression extends TestExpression
{
public function compile(\Twig\Compiler $compiler)
public function compile(\Twig\Compiler $compiler): void
{
$compiler
->raw('(')
@@ -473,7 +473,7 @@ Now, let's see the actual code of this class::
class CustomSetTokenParser extends \Twig\TokenParser\AbstractTokenParser
{
public function parse(\Twig\Token $token)
public function parse(\Twig\Token $token): Node
{
$parser = $this->parser;
$stream = $parser->getStream();
@@ -486,7 +486,7 @@ Now, let's see the actual code of this class::
return new CustomSetNode($name, $value, $token->getLine(), $this->getTag());
}
public function getTag()
public function getTag(): string
{
return 'set';
}
@@ -535,7 +535,7 @@ The ``CustomSetNode`` class itself is quite short::
parent::__construct(['value' => $value], ['name' => $name], $line, $tag);
}
public function compile(\Twig\Compiler $compiler)
public function compile(\Twig\Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
@@ -597,42 +597,42 @@ An extension is a class that implements the following interface::
*
* @return \Twig\TokenParser\TokenParserInterface[]
*/
public function getTokenParsers();
public function getTokenParsers(): array
/**
* Returns the node visitor instances to add to the existing list.
*
* @return \Twig\NodeVisitor\NodeVisitorInterface[]
*/
public function getNodeVisitors();
public function getNodeVisitors(): array
/**
* Returns a list of filters to add to the existing list.
*
* @return \Twig\TwigFilter[]
*/
public function getFilters();
public function getFilters(): array
/**
* Returns a list of tests to add to the existing list.
*
* @return \Twig\TwigTest[]
*/
public function getTests();
public function getTests(): array
/**
* Returns a list of functions to add to the existing list.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions();
public function getFunctions(): array
/**
* Returns a list of operators to add to the existing list.
*
* @return array<array> First array of unary operators, second array of binary operators
*/
public function getOperators();
public function getOperators(): array
}
To keep your extension class clean and lean, inherit from the built-in
@@ -684,7 +684,7 @@ method::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('lipsum', 'generate_lipsum'),
@@ -703,7 +703,7 @@ environment::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFilters()
public function getFilters(): array
{
return [
new \Twig\TwigFilter('rot13', 'str_rot13'),
@@ -722,7 +722,7 @@ to the Twig environment::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getTokenParsers()
public function getTokenParsers(): array
{
return [new CustomSetTokenParser()];
}
@@ -742,7 +742,7 @@ the ``!``, ``||``, and ``&&`` operators::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getOperators()
public function getOperators(): array
{
return [
[
@@ -765,7 +765,7 @@ The ``getTests()`` method lets you add new test functions::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getTests()
public function getTests(): array
{
return [
new \Twig\TwigTest('even', 'twig_test_even'),
@@ -801,14 +801,14 @@ The simplest way to use methods is to define them on the extension itself::
$this->rot13Provider = $rot13Provider;
}
public function getFunctions()
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('rot13', [$this, 'rot13']),
];
}
public function rot13($value)
public function rot13($value): string
{
return $this->rot13Provider->rot13($value);
}
@@ -825,7 +825,7 @@ must be autoload-able)::
class RuntimeLoader implements \Twig\RuntimeLoader\RuntimeLoaderInterface
{
public function load($class)
public function load($class): void
{
// implement the logic to create an instance of $class
// and inject its dependencies
@@ -857,7 +857,7 @@ It is now possible to move the runtime logic to a new
$this->rot13Provider = $rot13Provider;
}
public function rot13($value)
public function rot13($value): string
{
return $this->rot13Provider->rot13($value);
}
@@ -865,7 +865,7 @@ It is now possible to move the runtime logic to a new
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('rot13', ['CustomRuntimeExtension', 'rot13']),
+4 -6
View File
@@ -253,11 +253,9 @@ All loaders implement the ``\Twig\Loader\LoaderInterface``::
*
* @param string $name The template logical name
*
* @return \Twig\Source
*
* @throws \Twig\Error\LoaderError When $name is not found
*/
public function getSourceContext($name);
public function getSourceContext($name): \Twig\Source;
/**
* Gets the cache key to use for the cache for a given template name.
@@ -268,7 +266,7 @@ All loaders implement the ``\Twig\Loader\LoaderInterface``::
*
* @throws \Twig\Error\LoaderError When $name is not found
*/
public function getCacheKey($name);
public function getCacheKey($name): string;
/**
* Returns true if the template is still fresh.
@@ -280,7 +278,7 @@ All loaders implement the ``\Twig\Loader\LoaderInterface``::
*
* @throws \Twig\Error\LoaderError When $name is not found
*/
public function isFresh($name, $time);
public function isFresh($name, $time): bool;
/**
* Check if we have the source code of a template, given its name.
@@ -289,7 +287,7 @@ All loaders implement the ``\Twig\Loader\LoaderInterface``::
*
* @return bool If the template source code is handled by this loader or not
*/
public function exists($name);
public function exists($name): bool;
}
The ``isFresh()`` method must return ``true`` if the current cached template
+1 -1
View File
@@ -370,7 +370,7 @@ Now, let's define a loader able to use this database::
return new \Twig\Source($source, $name);
}
public function exists(string $name)
public function exists(string $name): bool
{
return $name === $this->getValue('name', $name);
}
+1 -4
View File
@@ -42,8 +42,5 @@ interface LoaderInterface
*/
public function isFresh(string $name, int $time): bool;
/**
* @return bool
*/
public function exists(string $name);
public function exists(string $name): bool;
}
+1 -1
View File
@@ -30,7 +30,7 @@ class ContainerRuntimeLoader implements RuntimeLoaderInterface
$this->container = $container;
}
public function load(string $class)
public function load(string $class): ?object
{
return $this->container->has($class) ? $this->container->get($class) : null;
}
+1 -1
View File
@@ -31,7 +31,7 @@ class FactoryRuntimeLoader implements RuntimeLoaderInterface
$this->map = $map;
}
public function load(string $class)
public function load(string $class): ?object
{
if (!isset($this->map[$class])) {
return null;
+1 -1
View File
@@ -23,5 +23,5 @@ interface RuntimeLoaderInterface
*
* @return object|null The runtime instance or null if the loader does not know how to create the runtime for this class
*/
public function load(string $class);
public function load(string $class): ?object;
}