Skip to content
Permalink
Browse files

minor #32346 [ExpressionLanguage] [5.0] add type-hints whenever possi…

…ble (Simperfit)

This PR was merged into the 5.0-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | Contribute to #32179   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | not needed <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Add type-hint whenever possible.

Commits
-------

26f6509 [ExpressionLanguage] [5.0] add type-hints whenever possible
  • Loading branch information...
fabpot committed Jul 4, 2019
2 parents 6128276 + 26f6509 commit 90c1603e31266d69e6c092ef5e91b818de456e6f
@@ -78,11 +78,9 @@ public function subcompile(Node\Node $node)
/**
* 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 @@ public function raw($string)
/**
* 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\"\$\\"));
@@ -64,7 +64,6 @@ public function getEvaluator()
/**
* 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 @@ public function getEvaluator()
* @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)) {
@@ -45,11 +45,10 @@ public function __construct(CacheItemPoolInterface $cache = null, array $provide
* 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 @@ public function compile($expression, $names = [])
* 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 @@ public function evaluate($expression, $values = [])
* 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,15 +101,14 @@ public function parse($expression, $names)
/**
* 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
*
* @throws \LogicException when registering a function after calling evaluate(), compile() or parse()
*
* @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.');
@@ -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;
@@ -85,14 +85,11 @@ public function __construct(array $functions)
* 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 @@ public function parse(TokenStream $stream, $names = [])
return $node;
}
public function parseExpression($precedence = 0)
public function parseExpression(int $precedence = 0)
{
$expr = $this->getPrimary();
$token = $this->stream->current;
@@ -54,12 +54,11 @@ public function __toString()
/**
* 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);
}
@@ -59,10 +59,9 @@ public function next()
* 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)) {

0 comments on commit 90c1603

Please sign in to comment.
You can’t perform that action at this time.