[ExpressionLanguage] [5.0] add type-hints whenever possible

This commit is contained in:
Amrouche Hamza 2019-07-03 20:11:09 +02:00
parent cc9778ed2e
commit 26f6509361
No known key found for this signature in database
GPG Key ID: E45A3DA456145BC1
7 changed files with 13 additions and 29 deletions

View File

@ -78,11 +78,9 @@ class Compiler implements ResetInterface
/**
* Adds a raw string to the compiled code.
*
* @param string $string The string
*
* @return $this
*/
public function raw($string)
public function raw(string $string)
{
$this->source .= $string;
@ -92,11 +90,9 @@ class Compiler implements ResetInterface
/**
* Adds a quoted string to the compiled code.
*
* @param string $value The string
*
* @return $this
*/
public function string($value)
public function string(string $value)
{
$this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));

View File

@ -64,7 +64,6 @@ class ExpressionFunction
/**
* Creates an ExpressionFunction from a PHP function name.
*
* @param string $phpFunctionName The PHP function name
* @param string|null $expressionFunctionName The expression function name (default: same than the PHP function name)
*
* @return self
@ -73,7 +72,7 @@ class ExpressionFunction
* @throws \InvalidArgumentException if given PHP function name is in namespace
* and expression function name is not defined
*/
public static function fromPhp($phpFunctionName, $expressionFunctionName = null)
public static function fromPhp(string $phpFunctionName, string $expressionFunctionName = null)
{
$phpFunctionName = ltrim($phpFunctionName, '\\');
if (!\function_exists($phpFunctionName)) {

View File

@ -45,11 +45,10 @@ class ExpressionLanguage
* Compiles an expression source code.
*
* @param Expression|string $expression The expression to compile
* @param array $names An array of valid names
*
* @return string The compiled PHP source code
*/
public function compile($expression, $names = [])
public function compile($expression, array $names = [])
{
return $this->getCompiler()->compile($this->parse($expression, $names)->getNodes())->getSource();
}
@ -58,11 +57,10 @@ class ExpressionLanguage
* Evaluate an expression.
*
* @param Expression|string $expression The expression to compile
* @param array $values An array of values
*
* @return mixed The result of the evaluation of the expression
*/
public function evaluate($expression, $values = [])
public function evaluate($expression, array $values = [])
{
return $this->parse($expression, array_keys($values))->getNodes()->evaluate($this->functions, $values);
}
@ -71,11 +69,10 @@ class ExpressionLanguage
* Parses an expression.
*
* @param Expression|string $expression The expression to parse
* @param array $names An array of valid names
*
* @return ParsedExpression A ParsedExpression instance
*/
public function parse($expression, $names)
public function parse($expression, array $names)
{
if ($expression instanceof ParsedExpression) {
return $expression;
@ -104,7 +101,6 @@ class ExpressionLanguage
/**
* Registers a function.
*
* @param string $name The function name
* @param callable $compiler A callable able to compile the function
* @param callable $evaluator A callable able to evaluate the function
*
@ -112,7 +108,7 @@ class ExpressionLanguage
*
* @see ExpressionFunction
*/
public function register($name, callable $compiler, callable $evaluator)
public function register(string $name, callable $compiler, callable $evaluator)
{
if (null !== $this->parser) {
throw new \LogicException('Registering functions after calling evaluate(), compile() or parse() is not supported.');

View File

@ -21,13 +21,11 @@ class Lexer
/**
* Tokenizes an expression.
*
* @param string $expression The expression to tokenize
*
* @return TokenStream A token stream instance
*
* @throws SyntaxError
*/
public function tokenize($expression)
public function tokenize(string $expression)
{
$expression = str_replace(["\r", "\n", "\t", "\v", "\f"], ' ', $expression);
$cursor = 0;

View File

@ -85,14 +85,11 @@ class Parser
* variable 'container' can be used in the expression
* but the compiled code will use 'this'.
*
* @param TokenStream $stream A token stream instance
* @param array $names An array of valid names
*
* @return Node\Node A node tree
*
* @throws SyntaxError
*/
public function parse(TokenStream $stream, $names = [])
public function parse(TokenStream $stream, array $names = [])
{
$this->stream = $stream;
$this->names = $names;
@ -105,7 +102,7 @@ class Parser
return $node;
}
public function parseExpression($precedence = 0)
public function parseExpression(int $precedence = 0)
{
$expr = $this->getPrimary();
$token = $this->stream->current;

View File

@ -54,12 +54,11 @@ class Token
/**
* Tests the current token for a type and/or a value.
*
* @param array|int $type The type to test
* @param string|null $value The token value
* @param array|int $type The type to test
*
* @return bool
*/
public function test($type, $value = null)
public function test($type, string $value = null)
{
return $this->type === $type && (null === $value || $this->value == $value);
}

View File

@ -59,10 +59,9 @@ class TokenStream
* Tests a token.
*
* @param array|int $type The type to test
* @param string|null $value The token value
* @param string|null $message The syntax error message
*/
public function expect($type, $value = null, $message = null)
public function expect($type, string $value = null, string $message = null)
{
$token = $this->current;
if (!$token->test($type, $value)) {