mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 10:26:32 +00:00
added some type hints on final classes
This commit is contained in:
+33
-64
@@ -67,14 +67,7 @@ final class ExtensionSet
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given extension is registered.
|
||||
*
|
||||
* @param string $class The extension class name
|
||||
*
|
||||
* @return bool Whether the extension is registered or not
|
||||
*/
|
||||
public function hasExtension($class)
|
||||
public function hasExtension(string $class): bool
|
||||
{
|
||||
$class = ltrim($class, '\\');
|
||||
if (!isset($this->extensions[$class]) && class_exists($class, false)) {
|
||||
@@ -85,14 +78,7 @@ final class ExtensionSet
|
||||
return isset($this->extensions[$class]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an extension by class name.
|
||||
*
|
||||
* @param string $class The extension class name
|
||||
*
|
||||
* @return ExtensionInterface
|
||||
*/
|
||||
public function getExtension($class)
|
||||
public function getExtension(string $class): ExtensionInterface
|
||||
{
|
||||
$class = ltrim($class, '\\');
|
||||
if (!isset($this->extensions[$class]) && class_exists($class, false)) {
|
||||
@@ -107,11 +93,6 @@ final class ExtensionSet
|
||||
return $this->extensions[$class];
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an array of extensions.
|
||||
*
|
||||
* @param array $extensions An array of extensions
|
||||
*/
|
||||
public function setExtensions(array $extensions)
|
||||
{
|
||||
foreach ($extensions as $extension) {
|
||||
@@ -120,26 +101,24 @@ final class ExtensionSet
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all registered extensions.
|
||||
*
|
||||
* @return array An array of extensions
|
||||
* @param ExtensionInterface[] $extensions
|
||||
*/
|
||||
public function getExtensions()
|
||||
public function getExtensions(): array
|
||||
{
|
||||
return $this->extensions;
|
||||
}
|
||||
|
||||
public function getSignature()
|
||||
public function getSignature(): string
|
||||
{
|
||||
return json_encode(array_keys($this->extensions));
|
||||
}
|
||||
|
||||
public function isInitialized()
|
||||
public function isInitialized(): bool
|
||||
{
|
||||
return $this->initialized || $this->runtimeInitialized;
|
||||
}
|
||||
|
||||
public function getLastModified()
|
||||
public function getLastModified(): int
|
||||
{
|
||||
if (0 !== $this->lastModified) {
|
||||
return $this->lastModified;
|
||||
@@ -181,7 +160,10 @@ final class ExtensionSet
|
||||
$this->staging->addFunction($function);
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
/**
|
||||
* @return TwigFunction[]
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -191,13 +173,9 @@ final class ExtensionSet
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a function by name.
|
||||
*
|
||||
* @param string $name function name
|
||||
*
|
||||
* @return TwigFunction|false
|
||||
*/
|
||||
public function getFunction($name)
|
||||
public function getFunction(string $name)
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -241,7 +219,10 @@ final class ExtensionSet
|
||||
$this->staging->addFilter($filter);
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
/**
|
||||
* @return TwigFilter[]
|
||||
*/
|
||||
public function getFilters(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -251,16 +232,9 @@ final class ExtensionSet
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a filter by name.
|
||||
*
|
||||
* Subclasses may override this method and load filters differently;
|
||||
* so no list of filters is available.
|
||||
*
|
||||
* @param string $name The filter name
|
||||
*
|
||||
* @return TwigFilter|false
|
||||
*/
|
||||
public function getFilter($name)
|
||||
public function getFilter(string $name)
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -304,7 +278,10 @@ final class ExtensionSet
|
||||
$this->staging->addNodeVisitor($visitor);
|
||||
}
|
||||
|
||||
public function getNodeVisitors()
|
||||
/**
|
||||
* @return NodeVisitorInterface[]
|
||||
*/
|
||||
public function getNodeVisitors(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -322,7 +299,10 @@ final class ExtensionSet
|
||||
$this->staging->addTokenParser($parser);
|
||||
}
|
||||
|
||||
public function getTokenParsers()
|
||||
/**
|
||||
* @return TokenParserInterface[]
|
||||
*/
|
||||
public function getTokenParsers(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -331,7 +311,7 @@ final class ExtensionSet
|
||||
return $this->parsers;
|
||||
}
|
||||
|
||||
public function getGlobals()
|
||||
public function getGlobals(): array
|
||||
{
|
||||
if (null !== $this->globals) {
|
||||
return $this->globals;
|
||||
@@ -367,7 +347,10 @@ final class ExtensionSet
|
||||
$this->staging->addTest($test);
|
||||
}
|
||||
|
||||
public function getTests()
|
||||
/**
|
||||
* @return TwigTest[]
|
||||
*/
|
||||
public function getTests(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -377,13 +360,9 @@ final class ExtensionSet
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a test by name.
|
||||
*
|
||||
* @param string $name The test name
|
||||
*
|
||||
* @return TwigTest|false
|
||||
*/
|
||||
public function getTest($name)
|
||||
public function getTest(string $name)
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -409,12 +388,7 @@ final class ExtensionSet
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the registered unary Operators.
|
||||
*
|
||||
* @return array An array of unary operators
|
||||
*/
|
||||
public function getUnaryOperators()
|
||||
public function getUnaryOperators(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
@@ -423,12 +397,7 @@ final class ExtensionSet
|
||||
return $this->unaryOperators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the registered binary Operators.
|
||||
*
|
||||
* @return array An array of binary operators
|
||||
*/
|
||||
public function getBinaryOperators()
|
||||
public function getBinaryOperators(): array
|
||||
{
|
||||
if (!$this->initialized) {
|
||||
$this->initExtensions();
|
||||
|
||||
@@ -44,10 +44,8 @@ final class NodeTraverser
|
||||
|
||||
/**
|
||||
* Traverses a node and calls the registered visitors.
|
||||
*
|
||||
* @return Node
|
||||
*/
|
||||
public function traverse(Node $node)
|
||||
public function traverse(Node $node): Node
|
||||
{
|
||||
ksort($this->visitors);
|
||||
foreach ($this->visitors as $visitors) {
|
||||
|
||||
+3
-3
@@ -27,14 +27,14 @@ final class Source
|
||||
* @param string $name The template logical name
|
||||
* @param string $path The filesystem path of the template if any
|
||||
*/
|
||||
public function __construct($code, $name, $path = '')
|
||||
public function __construct(string $code, string $name, string $path = '')
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->name = $name;
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
public function getCode()
|
||||
public function getCode(): string
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
@@ -44,7 +44,7 @@ final class Source
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getPath()
|
||||
public function getPath(): string
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
+8
-18
@@ -37,10 +37,8 @@ final class TemplateWrapper
|
||||
* Renders the template.
|
||||
*
|
||||
* @param array $context An array of parameters to pass to the template
|
||||
*
|
||||
* @return string The rendered template
|
||||
*/
|
||||
public function render($context = [])
|
||||
public function render(array $context = []): string
|
||||
{
|
||||
// using func_get_args() allows to not expose the blocks argument
|
||||
// as it should only be used by internal code
|
||||
@@ -52,7 +50,7 @@ final class TemplateWrapper
|
||||
*
|
||||
* @param array $context An array of parameters to pass to the template
|
||||
*/
|
||||
public function display($context = [])
|
||||
public function display(array $context = [])
|
||||
{
|
||||
// using func_get_args() allows to not expose the blocks argument
|
||||
// as it should only be used by internal code
|
||||
@@ -64,10 +62,8 @@ final class TemplateWrapper
|
||||
*
|
||||
* @param string $name The block name
|
||||
* @param array $context An array of parameters to pass to the template
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasBlock($name, $context = [])
|
||||
public function hasBlock(string $name, array $context = []): bool
|
||||
{
|
||||
return $this->template->hasBlock($name, $context);
|
||||
}
|
||||
@@ -79,7 +75,7 @@ final class TemplateWrapper
|
||||
*
|
||||
* @return string[] An array of defined template block names
|
||||
*/
|
||||
public function getBlockNames($context = [])
|
||||
public function getBlockNames(array $context = []): array
|
||||
{
|
||||
return $this->template->getBlockNames($context);
|
||||
}
|
||||
@@ -92,7 +88,7 @@ final class TemplateWrapper
|
||||
*
|
||||
* @return string The rendered block
|
||||
*/
|
||||
public function renderBlock($name, $context = [])
|
||||
public function renderBlock(string $name, array $context = []): string
|
||||
{
|
||||
$context = $this->env->mergeGlobals($context);
|
||||
$level = ob_get_level();
|
||||
@@ -116,23 +112,17 @@ final class TemplateWrapper
|
||||
* @param string $name The block name to render
|
||||
* @param array $context An array of parameters to pass to the template
|
||||
*/
|
||||
public function displayBlock($name, $context = [])
|
||||
public function displayBlock(string $name, array $context = [])
|
||||
{
|
||||
$this->template->displayBlock($name, $this->env->mergeGlobals($context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Source
|
||||
*/
|
||||
public function getSourceContext()
|
||||
public function getSourceContext(): Source
|
||||
{
|
||||
return $this->template->getSourceContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTemplatename()
|
||||
public function getTemplatename(): string
|
||||
{
|
||||
return $this->template->getTemplateName();
|
||||
}
|
||||
|
||||
+7
-24
@@ -43,10 +43,8 @@ final class TokenStream
|
||||
|
||||
/**
|
||||
* Sets the pointer to the next token and returns the old one.
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public function next()
|
||||
public function next(): Token
|
||||
{
|
||||
if (!isset($this->tokens[++$this->current])) {
|
||||
throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current - 1]->getLine(), $this->source);
|
||||
@@ -69,10 +67,8 @@ final class TokenStream
|
||||
|
||||
/**
|
||||
* Tests a token and returns it or throws a syntax error.
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public function expect($type, $value = null, $message = null)
|
||||
public function expect($type, $value = null, string $message = null): Token
|
||||
{
|
||||
$token = $this->tokens[$this->current];
|
||||
if (!$token->test($type, $value)) {
|
||||
@@ -92,12 +88,8 @@ final class TokenStream
|
||||
|
||||
/**
|
||||
* Looks at the next token.
|
||||
*
|
||||
* @param int $number
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public function look($number = 1)
|
||||
public function look(int $number = 1): Token
|
||||
{
|
||||
if (!isset($this->tokens[$this->current + $number])) {
|
||||
throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current + $number - 1]->getLine(), $this->source);
|
||||
@@ -108,28 +100,21 @@ final class TokenStream
|
||||
|
||||
/**
|
||||
* Tests the current token.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function test($primary, $secondary = null)
|
||||
public function test($primary, $secondary = null): bool
|
||||
{
|
||||
return $this->tokens[$this->current]->test($primary, $secondary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if end of stream was reached.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEOF()
|
||||
public function isEOF(): bool
|
||||
{
|
||||
return /* Token::EOF_TYPE */ -1 === $this->tokens[$this->current]->getType();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Token
|
||||
*/
|
||||
public function getCurrent()
|
||||
public function getCurrent(): Token
|
||||
{
|
||||
return $this->tokens[$this->current];
|
||||
}
|
||||
@@ -137,11 +122,9 @@ final class TokenStream
|
||||
/**
|
||||
* Gets the source associated with this stream.
|
||||
*
|
||||
* @return Source
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getSourceContext()
|
||||
public function getSourceContext(): Source
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
@@ -26,14 +26,6 @@ class Twig_Tests_ErrorTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertContains('test'.DIRECTORY_SEPARATOR.'Twig'.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.'ErrorTest.php', $error->getMessage());
|
||||
}
|
||||
|
||||
public function testErrorWithArrayFilename()
|
||||
{
|
||||
$error = new Error('foo');
|
||||
$error->setSourceContext(new Source('', ['foo' => 'bar']));
|
||||
|
||||
$this->assertEquals('foo in {"foo":"bar"}', $error->getMessage());
|
||||
}
|
||||
|
||||
public function testTwigExceptionGuessWithMissingVarAndArrayLoader()
|
||||
{
|
||||
$loader = new ArrayLoader([
|
||||
|
||||
@@ -392,7 +392,7 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
$twig = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['strict_variables' => true]);
|
||||
$getIsObject = new Twig_TemplateGetIsMethods();
|
||||
$template = new Twig_TemplateTest($twig, ['strict_variables' => true]);
|
||||
$template = new Twig_TemplateTest($twig, 'index.twig');
|
||||
// first time should not create a cache for "get"
|
||||
$this->assertNull(twig_get_attribute($twig, $template->getSourceContext(), $getIsObject, 'get'));
|
||||
// 0 should be in the method cache now, so this should fail
|
||||
|
||||
Reference in New Issue
Block a user