Skip to content

Commit

Permalink
[Templating] Added type-hints to all classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jul 6, 2019
1 parent fa57928 commit db5b4e6
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 80 deletions.
4 changes: 1 addition & 3 deletions Helper/Helper.php
Expand Up @@ -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;
}
Expand Down
4 changes: 1 addition & 3 deletions Helper/HelperInterface.php
Expand Up @@ -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.
Expand Down
19 changes: 5 additions & 14 deletions Helper/SlotsHelper.php
Expand Up @@ -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));
Expand Down Expand Up @@ -63,48 +61,41 @@ 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]);
}

/**
* 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;
}

/**
* 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) {
Expand Down
5 changes: 2 additions & 3 deletions Loader/CacheLoader.php
Expand Up @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions Loader/ChainLoader.php
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions Loader/FilesystemLoader.php
Expand Up @@ -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;
Expand All @@ -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])
Expand Down
5 changes: 2 additions & 3 deletions Loader/LoaderInterface.php
Expand Up @@ -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);
}
45 changes: 12 additions & 33 deletions PhpEngine.php
Expand Up @@ -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 = [])
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -258,25 +250,21 @@ 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]);
}

/**
* 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));
Expand All @@ -290,20 +278,19 @@ 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;
}

/**
* 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;
Expand All @@ -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"
Expand All @@ -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] = [];
Expand All @@ -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));
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions TemplateReference.php
Expand Up @@ -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;
Expand All @@ -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];
Expand Down
9 changes: 2 additions & 7 deletions TemplateReferenceInterface.php
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Tests/Loader/CacheLoaderTest.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Loader/LoaderTest.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/PhpEngineTest.php
Expand Up @@ -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;
}
Expand Down

0 comments on commit db5b4e6

Please sign in to comment.