diff --git a/doc/advanced.rst b/doc/advanced.rst index 5c25aea66..398bffb65 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -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 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']), diff --git a/doc/api.rst b/doc/api.rst index 219bdec33..6a5dd39d2 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -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 diff --git a/doc/recipes.rst b/doc/recipes.rst index 7febe3e55..fe39afab7 100644 --- a/doc/recipes.rst +++ b/doc/recipes.rst @@ -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); } diff --git a/src/Loader/LoaderInterface.php b/src/Loader/LoaderInterface.php index fec7e85ff..5bccbef80 100644 --- a/src/Loader/LoaderInterface.php +++ b/src/Loader/LoaderInterface.php @@ -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; } diff --git a/src/RuntimeLoader/ContainerRuntimeLoader.php b/src/RuntimeLoader/ContainerRuntimeLoader.php index 36435ea2d..6646cfbaa 100644 --- a/src/RuntimeLoader/ContainerRuntimeLoader.php +++ b/src/RuntimeLoader/ContainerRuntimeLoader.php @@ -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; } diff --git a/src/RuntimeLoader/FactoryRuntimeLoader.php b/src/RuntimeLoader/FactoryRuntimeLoader.php index 6aad95caf..5adfa395d 100644 --- a/src/RuntimeLoader/FactoryRuntimeLoader.php +++ b/src/RuntimeLoader/FactoryRuntimeLoader.php @@ -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; diff --git a/src/RuntimeLoader/RuntimeLoaderInterface.php b/src/RuntimeLoader/RuntimeLoaderInterface.php index 9e5b2048e..9d7d96d8e 100644 --- a/src/RuntimeLoader/RuntimeLoaderInterface.php +++ b/src/RuntimeLoader/RuntimeLoaderInterface.php @@ -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; }