This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/ExpressionLanguage
Fabien Potencier b74a887cd9 minor #9487 unify constructor initialization style throughout symfony (Tobion)
This PR was merged into the master branch.

Discussion
----------

unify constructor initialization style throughout symfony

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | n/a

In almost all classes symfony uses property initialization when the value is static. Constructor initialization is only used for things that actually have logic, like passed parameters or dynamic values. IMHO it makes the code much more readable because property definition, phpdoc and default value is in one place. Also one can easily see what the constructor implements for logic like overridden default value of a parent class. Otherwise the real deal is just hidden behind 10 property initializations. One more advantage is that it requires less code. As you can see, the code was almost cut in half (210 additions and 395 deletions).
I unified it accordingly across symfony. Sometimes it was [not even consistent within one class](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Config/Definition/BaseNode.php#L32). At the same time I recognized some errors like missing parent constructor call, or undefined properties or private properties that are not even used.

I then realized that a few Kernel tests were not passing because they were deeply implementation specific like modifying booted flag with a custom `KernelForTest->setIsBooted();`. I improved and refactored the kernel tests in the __second commit__.

__Third commit__ unifies short ternary operator, e.g. `$foo ?: new Foo()`. __Forth commit__ unifies missing parentheses, e.g. `new Foo()`.

Commits
-------

077a089 unify missing parentheses
2888594 unify short ternary operator
2a9daff [HttpKernel] better written kernel tests
111ac18 unify constructor initialization style throughout symfony
2013-11-22 18:42:00 +01:00
..
Node unify constructor initialization style throughout symfony 2013-11-11 19:40:07 +01:00
ParserCache Fixed typos 2013-11-09 12:55:32 +01:00
Resources/bin [ExpressionLanguage] fixed typo 2013-10-04 11:41:51 +02:00
Tests [ExpressionLanguage] Fixed conflict between punctation and range 2013-11-18 22:05:18 +01:00
.gitignore [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
CHANGELOG.md [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Compiler.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
composer.json [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Expression.php added optimized versions of expressions 2013-09-19 12:59:12 +02:00
ExpressionLanguage.php minor #9487 unify constructor initialization style throughout symfony (Tobion) 2013-11-22 18:42:00 +01:00
Lexer.php minor #9518 Fixed typos (pborreli) 2013-11-19 19:29:47 +01:00
LICENSE [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
ParsedExpression.php added optimized versions of expressions 2013-09-19 12:59:12 +02:00
Parser.php Fixed typos 2013-11-16 15:13:54 +00:00
phpunit.xml.dist [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
README.md Added missing parameter to the compile function 2013-11-08 16:59:08 +01:00
SerializedParsedExpression.php Removed dead code (unused use statements). 2013-10-16 13:59:56 +02:00
SyntaxError.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Token.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
TokenStream.php unify constructor initialization style throughout symfony 2013-11-11 19:40:07 +01:00

ExpressionLanguage Component

The ExpressionLanguage component provides an engine that can compile and evaluate expressions:

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

$language = new ExpressionLanguage();

echo $language->evaluate('1 + foo', array('foo' => 2));
// would output 3

echo $language->compile('1 + foo', array('foo'));
// would output (1 + $foo)

By default, the engine implements simple math and logic functions, method calls, property accesses, and array accesses.

You can extend your DSL with functions:

$compiler = function ($arg) {
    return sprintf('strtoupper(%s)', $arg);
};
$evaluator = function (array $variables, $value) {
    return strtoupper($value);
};
$language->register('upper', $compiler, $evaluator);

echo $language->evaluate('"foo" ~ upper(foo)', array('foo' => 'bar'));
// would output fooBAR

echo $language->compile('"foo" ~ upper(foo)');
// would output ("foo" . strtoupper($foo))

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/ExpressionLanguage/
$ composer.phar install --dev
$ phpunit