minor #32313 [Templating] Added type-hints to all classes (derrabus)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[Templating] Added type-hints to all classes

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32179
| License       | MIT
| Doc PR        | N/A

This PR added type-hints to all classes of the templating component.

Commits
-------

5626830e38 [Templating] Added type-hints to all classes.
This commit is contained in:
Fabien Potencier 2019-07-08 08:55:39 +02:00
commit e07195386f
13 changed files with 35 additions and 80 deletions

View File

@ -25,10 +25,8 @@ abstract class Helper implements HelperInterface
/** /**
* Sets the default charset. * Sets the default charset.
*
* @param string $charset The charset
*/ */
public function setCharset($charset) public function setCharset(string $charset)
{ {
$this->charset = $charset; $this->charset = $charset;
} }

View File

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

View File

@ -27,11 +27,9 @@ class SlotsHelper extends Helper
* This method starts an output buffer that will be * This method starts an output buffer that will be
* closed when the stop() method is called. * 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 * @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)) { if (\in_array($name, $this->openSlots)) {
throw new \InvalidArgumentException(sprintf('A slot named "%s" is already started.', $name)); 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. * Returns true if the slot exists.
* *
* @param string $name The slot name
*
* @return bool * @return bool
*/ */
public function has($name) public function has(string $name)
{ {
return isset($this->slots[$name]); return isset($this->slots[$name]);
} }
@ -75,23 +71,19 @@ class SlotsHelper extends Helper
/** /**
* Gets the slot value. * Gets the slot value.
* *
* @param string $name The slot name
* @param bool|string $default The default slot content * @param bool|string $default The default slot content
* *
* @return string The 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; return isset($this->slots[$name]) ? $this->slots[$name] : $default;
} }
/** /**
* Sets a slot value. * 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; $this->slots[$name] = $content;
} }
@ -99,12 +91,11 @@ class SlotsHelper extends Helper
/** /**
* Outputs a slot. * Outputs a slot.
* *
* @param string $name The slot name
* @param bool|string $default The default slot content * @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 * @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 (!isset($this->slots[$name])) {
if (false !== $default) { if (false !== $default) {

View File

@ -81,12 +81,11 @@ class CacheLoader extends Loader
/** /**
* Returns true if the template is still fresh. * 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 * @return bool
*/ */
public function isFresh(TemplateReferenceInterface $template, $time) public function isFresh(TemplateReferenceInterface $template, int $time)
{ {
return $this->loader->isFresh($template, $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. * 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 * @return bool
*/ */
public function isFresh(TemplateReferenceInterface $template, $time) public function isFresh(TemplateReferenceInterface $template, int $time)
{ {
foreach ($this->loaders as $loader) { foreach ($this->loaders as $loader) {
return $loader->isFresh($template, $time); return $loader->isFresh($template, $time);

View File

@ -78,12 +78,11 @@ class FilesystemLoader extends Loader
/** /**
* Returns true if the template is still fresh. * 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 * @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)) { if (false === $storage = $this->load($template)) {
return false; return false;
@ -95,11 +94,9 @@ class FilesystemLoader extends Loader
/** /**
* Returns true if the file is an existing absolute path. * 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 * @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] if ('/' == $file[0] || '\\' == $file[0]
|| (\strlen($file) > 3 && ctype_alpha($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. * 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 * @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; private $evalParameters;
/** /**
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance * @param HelperInterface[] $helpers An array of helper instances
* @param LoaderInterface $loader A loader instance
* @param HelperInterface[] $helpers An array of helper instances
*/ */
public function __construct(TemplateNameParserInterface $parser, LoaderInterface $loader, array $helpers = []) public function __construct(TemplateNameParserInterface $parser, LoaderInterface $loader, array $helpers = [])
{ {
@ -120,9 +118,6 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Evaluates a template. * 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 * @return string|false The evaluated template, or false if the engine is unable to render the template
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
@ -241,11 +236,8 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Sets a helper. * 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; $this->helpers[$helper->getName()] = $helper;
if (null !== $alias) { if (null !== $alias) {
@ -258,11 +250,9 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Returns true if the helper if defined. * Returns true if the helper if defined.
* *
* @param string $name The helper name
*
* @return bool true if the helper is defined, false otherwise * @return bool true if the helper is defined, false otherwise
*/ */
public function has($name) public function has(string $name)
{ {
return isset($this->helpers[$name]); return isset($this->helpers[$name]);
} }
@ -270,13 +260,11 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Gets a helper value. * Gets a helper value.
* *
* @param string $name The helper name
*
* @return HelperInterface The helper instance * @return HelperInterface The helper instance
* *
* @throws \InvalidArgumentException if the helper is not defined * @throws \InvalidArgumentException if the helper is not defined
*/ */
public function get($name) public function get(string $name)
{ {
if (!isset($this->helpers[$name])) { if (!isset($this->helpers[$name])) {
throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $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 * @param string $template The decorator logical name
*/ */
public function extend($template) public function extend(string $template)
{ {
$this->parents[$this->current] = $template; $this->parents[$this->current] = $template;
} }
@ -298,12 +286,11 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Escapes a string by using the current charset. * Escapes a string by using the current charset.
* *
* @param mixed $value A variable to escape * @param mixed $value A variable to escape
* @param string $context The context name
* *
* @return string The escaped value * @return string The escaped value
*/ */
public function escape($value, $context = 'html') public function escape($value, string $context = 'html')
{ {
if (is_numeric($value)) { if (is_numeric($value)) {
return $value; return $value;
@ -324,10 +311,8 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Sets the charset to use. * Sets the charset to use.
*
* @param string $charset The charset
*/ */
public function setCharset($charset) public function setCharset(string $charset)
{ {
if ('UTF8' === $charset = strtoupper($charset)) { if ('UTF8' === $charset = strtoupper($charset)) {
$charset = 'UTF-8'; // iconv on Windows requires "UTF-8" instead of "UTF8" $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. * 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; $this->escapers[$context] = $escaper;
self::$escaperCache[$context] = []; self::$escaperCache[$context] = [];
@ -364,13 +346,11 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Gets an escaper for a given context. * Gets an escaper for a given context.
* *
* @param string $context The context name
*
* @return callable A PHP callable * @return callable A PHP callable
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function getEscaper($context) public function getEscaper(string $context)
{ {
if (!isset($this->escapers[$context])) { if (!isset($this->escapers[$context])) {
throw new \InvalidArgumentException(sprintf('No registered escaper for context "%s".', $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; $this->globals[$name] = $value;
} }

View File

@ -39,7 +39,7 @@ class TemplateReference implements TemplateReferenceInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function set($name, $value) public function set(string $name, string $value)
{ {
if (\array_key_exists($name, $this->parameters)) { if (\array_key_exists($name, $this->parameters)) {
$this->parameters[$name] = $value; $this->parameters[$name] = $value;
@ -53,7 +53,7 @@ class TemplateReference implements TemplateReferenceInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get($name) public function get(string $name)
{ {
if (\array_key_exists($name, $this->parameters)) { if (\array_key_exists($name, $this->parameters)) {
return $this->parameters[$name]; return $this->parameters[$name];

View File

@ -28,25 +28,20 @@ interface TemplateReferenceInterface
/** /**
* Sets a template parameter. * Sets a template parameter.
* *
* @param string $name The parameter name
* @param string $value The parameter value
*
* @return $this * @return $this
* *
* @throws \InvalidArgumentException if the parameter name is not supported * @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. * Gets a template parameter.
* *
* @param string $name The parameter name
*
* @return string The parameter value * @return string The parameter value
* *
* @throws \InvalidArgumentException if the parameter name is not supported * @throws \InvalidArgumentException if the parameter name is not supported
*/ */
public function get($name); public function get(string $name);
/** /**
* Returns the path to the template. * Returns the path to the template.

View File

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

View File

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

View File

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