From 753338059624d7e7e043156bf3512ae90ad8cfa1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 19 Mar 2019 12:31:52 +0100 Subject: [PATCH] changed false to null to allow for type hinting --- src/Environment.php | 30 +++------------------ src/ExpressionParser.php | 4 +-- src/ExtensionSet.php | 21 +++++---------- src/NodeVisitor/SafeAnalysisNodeVisitor.php | 5 ++-- 4 files changed, 13 insertions(+), 47 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 99a9e3a4a..5c879605c 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -781,18 +781,9 @@ class Environment } /** - * 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 - * * @internal */ - public function getFilter($name) + public function getFilter(string $name): ?TwigFilter { return $this->extensionSet->getFilter($name); } @@ -836,15 +827,9 @@ class Environment } /** - * Gets a test by name. - * - * @param string $name The test name - * - * @return TwigTest|false - * * @internal */ - public function getTest($name) + public function getTest(string $name): ?TwigTest { return $this->extensionSet->getTest($name); } @@ -855,18 +840,9 @@ class Environment } /** - * Get a function by name. - * - * Subclasses may override this method and load functions differently; - * so no list of functions is available. - * - * @param string $name function name - * - * @return TwigFunction|false - * * @internal */ - public function getFunction($name) + public function getFunction(string $name): ?TwigFunction { return $this->extensionSet->getFunction($name); } diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index f4690babd..42ff20c32 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -668,7 +668,7 @@ class ExpressionParser private function getFunctionNodeClass(string $name, int $line): string { - if (false === $function = $this->env->getFunction($name)) { + if (!$function = $this->env->getFunction($name)) { $e = new SyntaxError(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext()); $e->addSuggestions($name, array_keys($this->env->getFunctions())); @@ -694,7 +694,7 @@ class ExpressionParser private function getFilterNodeClass(string $name, int $line): string { - if (false === $filter = $this->env->getFilter($name)) { + if (!$filter = $this->env->getFilter($name)) { $e = new SyntaxError(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext()); $e->addSuggestions($name, array_keys($this->env->getFilters())); diff --git a/src/ExtensionSet.php b/src/ExtensionSet.php index 7cf7fa7ee..2bbeef43a 100644 --- a/src/ExtensionSet.php +++ b/src/ExtensionSet.php @@ -175,10 +175,7 @@ final class ExtensionSet return $this->functions; } - /** - * @return TwigFunction|false - */ - public function getFunction(string $name) + public function getFunction(string $name): ?TwigFunction { if (!$this->initialized) { $this->initExtensions(); @@ -205,7 +202,7 @@ final class ExtensionSet } } - return false; + return null; } public function registerUndefinedFunctionCallback(callable $callable) @@ -234,10 +231,7 @@ final class ExtensionSet return $this->filters; } - /** - * @return TwigFilter|false - */ - public function getFilter(string $name) + public function getFilter(string $name): ?TwigFilter { if (!$this->initialized) { $this->initExtensions(); @@ -264,7 +258,7 @@ final class ExtensionSet } } - return false; + return null; } public function registerUndefinedFilterCallback(callable $callable) @@ -362,10 +356,7 @@ final class ExtensionSet return $this->tests; } - /** - * @return TwigTest|false - */ - public function getTest(string $name) + public function getTest(string $name): ?TwigTest { if (!$this->initialized) { $this->initExtensions(); @@ -388,7 +379,7 @@ final class ExtensionSet } } - return false; + return null; } public function getUnaryOperators(): array diff --git a/src/NodeVisitor/SafeAnalysisNodeVisitor.php b/src/NodeVisitor/SafeAnalysisNodeVisitor.php index 6c1c260f9..2a2654192 100644 --- a/src/NodeVisitor/SafeAnalysisNodeVisitor.php +++ b/src/NodeVisitor/SafeAnalysisNodeVisitor.php @@ -95,7 +95,7 @@ final class SafeAnalysisNodeVisitor extends AbstractNodeVisitor // filter expression is safe when the filter is safe $name = $node->getNode('filter')->getAttribute('value'); $args = $node->getNode('arguments'); - if (false !== $filter = $env->getFilter($name)) { + if ($filter = $env->getFilter($name)) { $safe = $filter->getSafe($args); if (null === $safe) { $safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety()); @@ -108,8 +108,7 @@ final class SafeAnalysisNodeVisitor extends AbstractNodeVisitor // function expression is safe when the function is safe $name = $node->getAttribute('name'); $args = $node->getNode('arguments'); - $function = $env->getFunction($name); - if (false !== $function) { + if ($function = $env->getFunction($name)) { $this->setSafe($node, $function->getSafe($args)); } else { $this->setSafe($node, []);