[Templating] Added type-hints to all classes.

This commit is contained in:
Alexander M. Turek 2019-07-02 00:30:52 +02:00
parent b057243f10
commit 5626830e38
13 changed files with 35 additions and 80 deletions

View File

@ -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;
}

View File

@ -27,10 +27,8 @@ interface HelperInterface
/**
* Sets the default charset.
*
* @param string $charset The charset
*/
public function setCharset($charset);
public function setCharset(string $charset);
/**
* Gets the default charset.

View File

@ -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 @@ class SlotsHelper extends Helper
/**
* 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 @@ class SlotsHelper extends Helper
/**
* 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 @@ class SlotsHelper extends Helper
/**
* 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) {

View File

@ -81,12 +81,11 @@ class CacheLoader extends Loader
/**
* 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);
}

View File

@ -60,12 +60,11 @@ class ChainLoader extends Loader
/**
* 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);

View File

@ -78,12 +78,11 @@ class FilesystemLoader extends Loader
/**
* 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 @@ class FilesystemLoader extends Loader
/**
* 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])

View File

@ -31,10 +31,9 @@ interface LoaderInterface
/**
* 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);
}

View File

@ -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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
*
* @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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/**
* 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 @@ class PhpEngine implements EngineInterface, \ArrayAccess
}
/**
* @param string $name
* @param mixed $value
* @param mixed $value
*/
public function addGlobal($name, $value)
public function addGlobal(string $name, $value)
{
$this->globals[$name] = $value;
}

View File

@ -39,7 +39,7 @@ class TemplateReference implements TemplateReferenceInterface
/**
* {@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 @@ class TemplateReference implements TemplateReferenceInterface
/**
* {@inheritdoc}
*/
public function get($name)
public function get(string $name)
{
if (\array_key_exists($name, $this->parameters)) {
return $this->parameters[$name];

View File

@ -28,25 +28,20 @@ interface TemplateReferenceInterface
/**
* 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.

View File

@ -87,7 +87,7 @@ class ProjectTemplateLoaderVar extends Loader
return false;
}
public function isFresh(TemplateReferenceInterface $template, $time)
public function isFresh(TemplateReferenceInterface $template, int $time)
{
return false;
}

View File

@ -42,7 +42,7 @@ class ProjectTemplateLoader4 extends Loader
return $this->debugger;
}
public function isFresh(TemplateReferenceInterface $template, $time)
public function isFresh(TemplateReferenceInterface $template, int $time)
{
return false;
}

View File

@ -219,7 +219,7 @@ class ProjectTemplateLoader extends Loader
return false;
}
public function isFresh(TemplateReferenceInterface $template, $time)
public function isFresh(TemplateReferenceInterface $template, int $time)
{
return false;
}