From db5b4e681d49e6699c4a16d3ba491e6125117bb2 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 2 Jul 2019 00:30:52 +0200 Subject: [PATCH] [Templating] Added type-hints to all classes. --- Helper/Helper.php | 4 +-- Helper/HelperInterface.php | 4 +-- Helper/SlotsHelper.php | 19 ++++---------- Loader/CacheLoader.php | 5 ++-- Loader/ChainLoader.php | 5 ++-- Loader/FilesystemLoader.php | 9 +++---- Loader/LoaderInterface.php | 5 ++-- PhpEngine.php | 45 +++++++++----------------------- TemplateReference.php | 4 +-- TemplateReferenceInterface.php | 9 ++----- Tests/Loader/CacheLoaderTest.php | 2 +- Tests/Loader/LoaderTest.php | 2 +- Tests/PhpEngineTest.php | 2 +- 13 files changed, 35 insertions(+), 80 deletions(-) diff --git a/Helper/Helper.php b/Helper/Helper.php index 2f34fb2..d8f924d 100644 --- a/Helper/Helper.php +++ b/Helper/Helper.php @@ -25,10 +25,8 @@ abstract class Helper implements HelperInterface /** * Sets the default charset. - * - * @param string $charset The charset */ - public function setCharset($charset) + public function setCharset(string $charset) { $this->charset = $charset; } diff --git a/Helper/HelperInterface.php b/Helper/HelperInterface.php index ce67c4a..57c4b39 100644 --- a/Helper/HelperInterface.php +++ b/Helper/HelperInterface.php @@ -27,10 +27,8 @@ public function getName(); /** * Sets the default charset. - * - * @param string $charset The charset */ - public function setCharset($charset); + public function setCharset(string $charset); /** * Gets the default charset. diff --git a/Helper/SlotsHelper.php b/Helper/SlotsHelper.php index 80acdc9..0ccd511 100644 --- a/Helper/SlotsHelper.php +++ b/Helper/SlotsHelper.php @@ -27,11 +27,9 @@ class SlotsHelper extends Helper * This method starts an output buffer that will be * closed when the stop() method is called. * - * @param string $name The slot name - * * @throws \InvalidArgumentException if a slot with the same name is already started */ - public function start($name) + public function start(string $name) { if (\in_array($name, $this->openSlots)) { throw new \InvalidArgumentException(sprintf('A slot named "%s" is already started.', $name)); @@ -63,11 +61,9 @@ public function stop() /** * Returns true if the slot exists. * - * @param string $name The slot name - * * @return bool */ - public function has($name) + public function has(string $name) { return isset($this->slots[$name]); } @@ -75,23 +71,19 @@ public function has($name) /** * Gets the slot value. * - * @param string $name The slot name * @param bool|string $default The default slot content * * @return string The slot content */ - public function get($name, $default = false) + public function get(string $name, $default = false) { return isset($this->slots[$name]) ? $this->slots[$name] : $default; } /** * Sets a slot value. - * - * @param string $name The slot name - * @param string $content The slot content */ - public function set($name, $content) + public function set(string $name, string $content) { $this->slots[$name] = $content; } @@ -99,12 +91,11 @@ public function set($name, $content) /** * Outputs a slot. * - * @param string $name The slot name * @param bool|string $default The default slot content * * @return bool true if the slot is defined or if a default content has been provided, false otherwise */ - public function output($name, $default = false) + public function output(string $name, $default = false) { if (!isset($this->slots[$name])) { if (false !== $default) { diff --git a/Loader/CacheLoader.php b/Loader/CacheLoader.php index 0e00ef0..8d63a67 100644 --- a/Loader/CacheLoader.php +++ b/Loader/CacheLoader.php @@ -81,12 +81,11 @@ public function load(TemplateReferenceInterface $template) /** * Returns true if the template is still fresh. * - * @param TemplateReferenceInterface $template A template - * @param int $time The last modification time of the cached template (timestamp) + * @param int $time The last modification time of the cached template (timestamp) * * @return bool */ - public function isFresh(TemplateReferenceInterface $template, $time) + public function isFresh(TemplateReferenceInterface $template, int $time) { return $this->loader->isFresh($template, $time); } diff --git a/Loader/ChainLoader.php b/Loader/ChainLoader.php index 3e5f375..c85fded 100644 --- a/Loader/ChainLoader.php +++ b/Loader/ChainLoader.php @@ -60,12 +60,11 @@ public function load(TemplateReferenceInterface $template) /** * Returns true if the template is still fresh. * - * @param TemplateReferenceInterface $template A template - * @param int $time The last modification time of the cached template (timestamp) + * @param int $time The last modification time of the cached template (timestamp) * * @return bool */ - public function isFresh(TemplateReferenceInterface $template, $time) + public function isFresh(TemplateReferenceInterface $template, int $time) { foreach ($this->loaders as $loader) { return $loader->isFresh($template, $time); diff --git a/Loader/FilesystemLoader.php b/Loader/FilesystemLoader.php index 016018c..66aa3ca 100644 --- a/Loader/FilesystemLoader.php +++ b/Loader/FilesystemLoader.php @@ -78,12 +78,11 @@ public function load(TemplateReferenceInterface $template) /** * Returns true if the template is still fresh. * - * @param TemplateReferenceInterface $template A template - * @param int $time The last modification time of the cached template (timestamp) + * @param int $time The last modification time of the cached template (timestamp) * * @return bool true if the template is still fresh, false otherwise */ - public function isFresh(TemplateReferenceInterface $template, $time) + public function isFresh(TemplateReferenceInterface $template, int $time) { if (false === $storage = $this->load($template)) { return false; @@ -95,11 +94,9 @@ public function isFresh(TemplateReferenceInterface $template, $time) /** * Returns true if the file is an existing absolute path. * - * @param string $file A path - * * @return bool true if the path exists and is absolute, false otherwise */ - protected static function isAbsolutePath($file) + protected static function isAbsolutePath(string $file) { if ('/' == $file[0] || '\\' == $file[0] || (\strlen($file) > 3 && ctype_alpha($file[0]) diff --git a/Loader/LoaderInterface.php b/Loader/LoaderInterface.php index 0deb231..487fc49 100644 --- a/Loader/LoaderInterface.php +++ b/Loader/LoaderInterface.php @@ -31,10 +31,9 @@ public function load(TemplateReferenceInterface $template); /** * Returns true if the template is still fresh. * - * @param TemplateReferenceInterface $template A template - * @param int $time The last modification time of the cached template (timestamp) + * @param int $time The last modification time of the cached template (timestamp) * * @return bool */ - public function isFresh(TemplateReferenceInterface $template, $time); + public function isFresh(TemplateReferenceInterface $template, int $time); } diff --git a/PhpEngine.php b/PhpEngine.php index 9da9826..4442ded 100644 --- a/PhpEngine.php +++ b/PhpEngine.php @@ -43,9 +43,7 @@ class PhpEngine implements EngineInterface, \ArrayAccess private $evalParameters; /** - * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance - * @param LoaderInterface $loader A loader instance - * @param HelperInterface[] $helpers An array of helper instances + * @param HelperInterface[] $helpers An array of helper instances */ public function __construct(TemplateNameParserInterface $parser, LoaderInterface $loader, array $helpers = []) { @@ -120,9 +118,6 @@ public function supports($name) /** * Evaluates a template. * - * @param Storage $template The template to render - * @param array $parameters An array of parameters to pass to the template - * * @return string|false The evaluated template, or false if the engine is unable to render the template * * @throws \InvalidArgumentException @@ -241,11 +236,8 @@ public function setHelpers(array $helpers) /** * Sets a helper. - * - * @param HelperInterface $helper The helper instance - * @param string $alias An alias */ - public function set(HelperInterface $helper, $alias = null) + public function set(HelperInterface $helper, string $alias = null) { $this->helpers[$helper->getName()] = $helper; if (null !== $alias) { @@ -258,11 +250,9 @@ public function set(HelperInterface $helper, $alias = null) /** * Returns true if the helper if defined. * - * @param string $name The helper name - * * @return bool true if the helper is defined, false otherwise */ - public function has($name) + public function has(string $name) { return isset($this->helpers[$name]); } @@ -270,13 +260,11 @@ public function has($name) /** * Gets a helper value. * - * @param string $name The helper name - * * @return HelperInterface The helper instance * * @throws \InvalidArgumentException if the helper is not defined */ - public function get($name) + public function get(string $name) { if (!isset($this->helpers[$name])) { throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); @@ -290,7 +278,7 @@ public function get($name) * * @param string $template The decorator logical name */ - public function extend($template) + public function extend(string $template) { $this->parents[$this->current] = $template; } @@ -298,12 +286,11 @@ public function extend($template) /** * Escapes a string by using the current charset. * - * @param mixed $value A variable to escape - * @param string $context The context name + * @param mixed $value A variable to escape * * @return string The escaped value */ - public function escape($value, $context = 'html') + public function escape($value, string $context = 'html') { if (is_numeric($value)) { return $value; @@ -324,10 +311,8 @@ public function escape($value, $context = 'html') /** * Sets the charset to use. - * - * @param string $charset The charset */ - public function setCharset($charset) + public function setCharset(string $charset) { if ('UTF8' === $charset = strtoupper($charset)) { $charset = 'UTF-8'; // iconv on Windows requires "UTF-8" instead of "UTF8" @@ -351,11 +336,8 @@ public function getCharset() /** * Adds an escaper for the given context. - * - * @param string $context The escaper context (html, js, ...) - * @param callable $escaper A PHP callable */ - public function setEscaper($context, callable $escaper) + public function setEscaper(string $context, callable $escaper) { $this->escapers[$context] = $escaper; self::$escaperCache[$context] = []; @@ -364,13 +346,11 @@ public function setEscaper($context, callable $escaper) /** * Gets an escaper for a given context. * - * @param string $context The context name - * * @return callable A PHP callable * * @throws \InvalidArgumentException */ - public function getEscaper($context) + public function getEscaper(string $context) { if (!isset($this->escapers[$context])) { throw new \InvalidArgumentException(sprintf('No registered escaper for context "%s".', $context)); @@ -380,10 +360,9 @@ public function getEscaper($context) } /** - * @param string $name - * @param mixed $value + * @param mixed $value */ - public function addGlobal($name, $value) + public function addGlobal(string $name, $value) { $this->globals[$name] = $value; } diff --git a/TemplateReference.php b/TemplateReference.php index ddff4db..ab370b2 100644 --- a/TemplateReference.php +++ b/TemplateReference.php @@ -39,7 +39,7 @@ public function __toString() /** * {@inheritdoc} */ - public function set($name, $value) + public function set(string $name, string $value) { if (\array_key_exists($name, $this->parameters)) { $this->parameters[$name] = $value; @@ -53,7 +53,7 @@ public function set($name, $value) /** * {@inheritdoc} */ - public function get($name) + public function get(string $name) { if (\array_key_exists($name, $this->parameters)) { return $this->parameters[$name]; diff --git a/TemplateReferenceInterface.php b/TemplateReferenceInterface.php index 45d9421..7f23faf 100644 --- a/TemplateReferenceInterface.php +++ b/TemplateReferenceInterface.php @@ -28,25 +28,20 @@ public function all(); /** * Sets a template parameter. * - * @param string $name The parameter name - * @param string $value The parameter value - * * @return $this * * @throws \InvalidArgumentException if the parameter name is not supported */ - public function set($name, $value); + public function set(string $name, string $value); /** * Gets a template parameter. * - * @param string $name The parameter name - * * @return string The parameter value * * @throws \InvalidArgumentException if the parameter name is not supported */ - public function get($name); + public function get(string $name); /** * Returns the path to the template. diff --git a/Tests/Loader/CacheLoaderTest.php b/Tests/Loader/CacheLoaderTest.php index e66d623..dfe1c32 100644 --- a/Tests/Loader/CacheLoaderTest.php +++ b/Tests/Loader/CacheLoaderTest.php @@ -87,7 +87,7 @@ public function load(TemplateReferenceInterface $template) return false; } - public function isFresh(TemplateReferenceInterface $template, $time) + public function isFresh(TemplateReferenceInterface $template, int $time) { return false; } diff --git a/Tests/Loader/LoaderTest.php b/Tests/Loader/LoaderTest.php index da8c04c..f9af71e 100644 --- a/Tests/Loader/LoaderTest.php +++ b/Tests/Loader/LoaderTest.php @@ -42,7 +42,7 @@ public function getDebugger() return $this->debugger; } - public function isFresh(TemplateReferenceInterface $template, $time) + public function isFresh(TemplateReferenceInterface $template, int $time) { return false; } diff --git a/Tests/PhpEngineTest.php b/Tests/PhpEngineTest.php index 5b62100..4c7e747 100644 --- a/Tests/PhpEngineTest.php +++ b/Tests/PhpEngineTest.php @@ -219,7 +219,7 @@ public function load(TemplateReferenceInterface $template) return false; } - public function isFresh(TemplateReferenceInterface $template, $time) + public function isFresh(TemplateReferenceInterface $template, int $time) { return false; }