added some type hints on private methods

This commit is contained in:
Fabien Potencier
2019-03-16 13:20:08 +01:00
parent 6d35153bd2
commit 241f95cb25
12 changed files with 29 additions and 32 deletions
+13 -11
View File
@@ -27,6 +27,8 @@ use Twig\Node\Expression\Unary\NegUnary;
use Twig\Node\Expression\Unary\NotUnary; use Twig\Node\Expression\Unary\NotUnary;
use Twig\Node\Expression\Unary\PosUnary; use Twig\Node\Expression\Unary\PosUnary;
use Twig\Node\Node; use Twig\Node\Node;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\TestExpression;
/** /**
* Parses expressions. * Parses expressions.
@@ -88,7 +90,7 @@ class ExpressionParser
return $expr; return $expr;
} }
private function getPrimary() private function getPrimary(): AbstractExpression
{ {
$token = $this->parser->getCurrentToken(); $token = $this->parser->getCurrentToken();
@@ -110,7 +112,7 @@ class ExpressionParser
return $this->parsePrimaryExpression(); return $this->parsePrimaryExpression();
} }
private function parseConditionalExpression($expr) private function parseConditionalExpression($expr): AbstractExpression
{ {
while ($this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, '?')) { while ($this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, '?')) {
if (!$this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ':')) { if (!$this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ':')) {
@@ -131,12 +133,12 @@ class ExpressionParser
return $expr; return $expr;
} }
private function isUnary(Token $token) private function isUnary(Token $token): bool
{ {
return $token->test(/* Token::OPERATOR_TYPE */ 8) && isset($this->unaryOperators[$token->getValue()]); return $token->test(/* Token::OPERATOR_TYPE */ 8) && isset($this->unaryOperators[$token->getValue()]);
} }
private function isBinary(Token $token) private function isBinary(Token $token): bool
{ {
return $token->test(/* Token::OPERATOR_TYPE */ 8) && isset($this->binaryOperators[$token->getValue()]); return $token->test(/* Token::OPERATOR_TYPE */ 8) && isset($this->binaryOperators[$token->getValue()]);
} }
@@ -598,12 +600,12 @@ class ExpressionParser
return new Node($targets); return new Node($targets);
} }
private function parseNotTestExpression(Node $node) private function parseNotTestExpression(Node $node): NotUnary
{ {
return new NotUnary($this->parseTestExpression($node), $this->parser->getCurrentToken()->getLine()); return new NotUnary($this->parseTestExpression($node), $this->parser->getCurrentToken()->getLine());
} }
private function parseTestExpression(Node $node) private function parseTestExpression(Node $node): TestExpression
{ {
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
list($name, $test) = $this->getTest($node->getTemplateLine()); list($name, $test) = $this->getTest($node->getTemplateLine());
@@ -617,7 +619,7 @@ class ExpressionParser
return new $class($node, $name, $arguments, $this->parser->getCurrentToken()->getLine()); return new $class($node, $name, $arguments, $this->parser->getCurrentToken()->getLine());
} }
private function getTest($line) private function getTest(int $line): array
{ {
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue();
@@ -643,7 +645,7 @@ class ExpressionParser
throw $e; throw $e;
} }
private function getTestNodeClass($test) private function getTestNodeClass(TwigTest $test): string
{ {
if ($test->isDeprecated()) { if ($test->isDeprecated()) {
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
@@ -664,7 +666,7 @@ class ExpressionParser
return $test->getNodeClass(); return $test->getNodeClass();
} }
private function getFunctionNodeClass($name, $line) private function getFunctionNodeClass(string $name, int $line): string
{ {
if (false === $function = $this->env->getFunction($name)) { if (false === $function = $this->env->getFunction($name)) {
$e = new SyntaxError(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext()); $e = new SyntaxError(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext());
@@ -690,7 +692,7 @@ class ExpressionParser
return $function->getNodeClass(); return $function->getNodeClass();
} }
private function getFilterNodeClass($name, $line) private function getFilterNodeClass(string $name, int $line): string
{ {
if (false === $filter = $this->env->getFilter($name)) { if (false === $filter = $this->env->getFilter($name)) {
$e = new SyntaxError(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext()); $e = new SyntaxError(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext());
@@ -717,7 +719,7 @@ class ExpressionParser
} }
// checks that the node only contains "constant" elements // checks that the node only contains "constant" elements
private function checkConstantExpression(Node $node) private function checkConstantExpression(Node $node): bool
{ {
if (!($node instanceof ConstantExpression || $node instanceof ArrayExpression if (!($node instanceof ConstantExpression || $node instanceof ArrayExpression
|| $node instanceof NegUnary || $node instanceof PosUnary || $node instanceof NegUnary || $node instanceof PosUnary
@@ -49,7 +49,7 @@ class BlockReferenceExpression extends AbstractExpression
} }
} }
private function compileTemplateCall(Compiler $compiler, $method) private function compileTemplateCall(Compiler $compiler, string $method): Compiler
{ {
if (!$this->hasNode('template')) { if (!$this->hasNode('template')) {
$compiler->write('$this'); $compiler->write('$this');
@@ -66,12 +66,11 @@ class BlockReferenceExpression extends AbstractExpression
} }
$compiler->raw(sprintf('->%s', $method)); $compiler->raw(sprintf('->%s', $method));
$this->compileBlockArguments($compiler);
return $compiler; return $this->compileBlockArguments($compiler);
} }
private function compileBlockArguments(Compiler $compiler) private function compileBlockArguments(Compiler $compiler): Compiler
{ {
$compiler $compiler
->raw('(') ->raw('(')
+1 -1
View File
@@ -230,7 +230,7 @@ abstract class CallExpression extends AbstractExpression
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $name)); return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $name));
} }
private function getCallableParameters($callable, $isVariadic) private function getCallableParameters($callable, bool $isVariadic): array
{ {
list($r) = $this->reflectCallable($callable); list($r) = $this->reflectCallable($callable);
if (null === $r) { if (null === $r) {
+1 -1
View File
@@ -59,7 +59,7 @@ final class NodeTraverser
return $node; return $node;
} }
private function traverseForVisitor(NodeVisitorInterface $visitor, Node $node) private function traverseForVisitor(NodeVisitorInterface $visitor, Node $node): Node
{ {
$node = $visitor->enterNode($node, $this->env); $node = $visitor->enterNode($node, $this->env);
+1 -1
View File
@@ -147,7 +147,7 @@ final class EscaperNodeVisitor extends AbstractNodeVisitor
return $this->defaultStrategy ? $this->defaultStrategy : false; return $this->defaultStrategy ? $this->defaultStrategy : false;
} }
private function getEscaperFilter($type, Node $node) private function getEscaperFilter(string $type, Node $node): FilterExpression
{ {
$line = $node->getTemplateLine(); $line = $node->getTemplateLine();
$name = new ConstantExpression('escape', $line); $name = new ConstantExpression('escape', $line);
+2 -6
View File
@@ -90,10 +90,8 @@ final class OptimizerNodeVisitor extends AbstractNodeVisitor
* It replaces: * It replaces:
* *
* * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()" * * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
*
* @return Node
*/ */
private function optimizePrintNode(Node $node, Environment $env) private function optimizePrintNode(Node $node, Environment $env): Node
{ {
if (!$node instanceof PrintNode) { if (!$node instanceof PrintNode) {
return $node; return $node;
@@ -114,10 +112,8 @@ final class OptimizerNodeVisitor extends AbstractNodeVisitor
/** /**
* Removes "raw" filters. * Removes "raw" filters.
*
* @return Node
*/ */
private function optimizeRawFilter(Node $node, Environment $env) private function optimizeRawFilter(Node $node, Environment $env): Node
{ {
if ($node instanceof FilterExpression && 'raw' == $node->getNode('filter')->getAttribute('value')) { if ($node instanceof FilterExpression && 'raw' == $node->getNode('filter')->getAttribute('value')) {
return $node->getNode('node'); return $node->getNode('node');
+1 -1
View File
@@ -134,7 +134,7 @@ final class SafeAnalysisNodeVisitor extends AbstractNodeVisitor
return $node; return $node;
} }
private function intersectSafe(array $a = null, array $b = null) private function intersectSafe(array $a = null, array $b = null): array
{ {
if (null === $a || null === $b) { if (null === $a || null === $b) {
return []; return [];
+2 -2
View File
@@ -110,7 +110,7 @@ final class SandboxNodeVisitor extends AbstractNodeVisitor
return $node; return $node;
} }
private function wrapNode(Node $node, $name) private function wrapNode(Node $node, string $name)
{ {
$expr = $node->getNode($name); $expr = $node->getNode($name);
if ($expr instanceof NameExpression || $expr instanceof GetAttrExpression) { if ($expr instanceof NameExpression || $expr instanceof GetAttrExpression) {
@@ -118,7 +118,7 @@ final class SandboxNodeVisitor extends AbstractNodeVisitor
} }
} }
private function wrapArrayNode(Node $node, $name) private function wrapArrayNode(Node $node, string $name)
{ {
$args = $node->getNode($name); $args = $node->getNode($name);
foreach ($args as $name => $_) { foreach ($args as $name => $_) {
+1 -1
View File
@@ -335,7 +335,7 @@ class Parser
return $this->stream->getCurrent(); return $this->stream->getCurrent();
} }
private function filterBodyNodes(Node $node, $nested = false) private function filterBodyNodes(Node $node, bool $nested = false)
{ {
// check that the body does not contain non-empty output nodes // check that the body does not contain non-empty output nodes
if ( if (
+1 -1
View File
@@ -31,7 +31,7 @@ abstract class BaseDumper
abstract protected function formatTime(Profile $profile, $percent); abstract protected function formatTime(Profile $profile, $percent);
private function dumpProfile(Profile $profile, $prefix = '', $sibling = false) private function dumpProfile(Profile $profile, $prefix = '', $sibling = false): string
{ {
if ($profile->isRoot()) { if ($profile->isRoot()) {
$this->root = $profile->getDuration(); $this->root = $profile->getDuration();
+2 -2
View File
@@ -40,7 +40,7 @@ EOF;
return $str; return $str;
} }
private function dumpChildren($parent, Profile $profile, &$data) private function dumpChildren(string $parent, Profile $profile, &$data)
{ {
foreach ($profile as $p) { foreach ($profile as $p) {
if ($p->isTemplate()) { if ($p->isTemplate()) {
@@ -53,7 +53,7 @@ EOF;
} }
} }
private function dumpProfile($edge, Profile $profile, &$data) private function dumpProfile(string $edge, Profile $profile, &$data)
{ {
if (isset($data[$edge])) { if (isset($data[$edge])) {
++$data[$edge]['ct']; ++$data[$edge]['ct'];
@@ -64,7 +64,7 @@ final class ProfilerNodeVisitor extends AbstractNodeVisitor
return $node; return $node;
} }
private function getVarName() private function getVarName(): string
{ {
return sprintf('__internal_%s', hash('sha256', $this->extensionName)); return sprintf('__internal_%s', hash('sha256', $this->extensionName));
} }