8000 Throw exception · symfony/symfony@dcf7de6 · GitHub
[go: up one dir, main page]

Skip to content

Commit dcf7de6

Browse files
committed
Throw exception
1 parent b8d59e3 commit dcf7de6

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

src/Symfony/Component/ExpressionLanguage/Compiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class Compiler
2121
private $source;
2222
private $functions;
2323

24-
public function __construct(array &$functions)
24+
public function __construct(array $functions)
2525
{
26-
$this->functions = &$functions;
26+
$this->functions = $functions;
2727
}
2828

2929
public function getFunction($name)

src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ public function parse($expression, $names)
110110
* @param callable $compiler A callable able to compile the function
111111
* @param callable $evaluator A callable able to evaluate the function
112112
*
113+
* @throws \LogicException when register a function after calling evaluate/compilate the first time
114+
*
113115
* @see ExpressionFunction
114116
*/
115117
public function register($name, $compiler, $evaluator)
116118
{
119+
if ($this->parser) {
120+
throw new \LogicException('It\'s impossible to register function after calling evaluate/compilate the first time.');
121+
}
122+
117123
$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
118124
}
119125

src/Symfony/Component/ExpressionLanguage/Parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class Parser
3232
private $functions;
3333
private $names;
3434

35-
public function __construct(array &$functions)
35+
public function __construct(array $functions)
3636
{
37-
$this->functions = &$functions;
37+
$this->functions = $functions;
3838

3939
$this->unaryOperators = array(
4040
'not' => array('precedence' => 50),

src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\ExpressionLanguage\Tests;
1313

14-
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
1514
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1615
use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
1716

@@ -140,21 +139,23 @@ public function testCachingWithDifferentNamesOrder()
140139
$expressionLanguage->compile($expression, array('B' => 'b', 'a'));
141140
}
142141

143-
public function testAddingFunctionAfterEval()
142+
/**
143+
* @expectedException \LogicException
144+
*/
145+
public function testRegisterAfterEval()
144146
{
145147
$el = new ExpressionLanguage();
146148
$el->evaluate('1 + 1');
147-
$el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
148-
$result = $el->evaluate('fn()');
149-
$this->assertNull($result);
149+
$el->register('fn', function () {}, function () {});
150150
}
151151

152-
public function testAddingFunctionAfterCompile()
152+
/**
153+
* @expectedException \LogicException
154+
*/
155+
public function testRegisterAfterCompile()
153156
{
154157
$el = new ExpressionLanguage();
155158
$el->compile('1 + 1');
156-
$el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
157-
$result = $el->compile('fn()');
158-
$this->assertEmpty($result);
159+
$el->register('fn', function () {}, function () {});
159160
}
160161
}

src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ class ParserTest extends \PHPUnit_Framework_TestCase
2424
public function testParseWithInvalidName()
2525
{
2626
$lexer = new Lexer();
27-
$functions = array();
28-
$parser = new Parser($functions);
27+
$parser = new Parser(array());
2928
$parser->parse($lexer->tokenize('foo'));
3029
}
3130

@@ -36,8 +35,7 @@ public function testParseWithInvalidName()
3635
public function testParseWithZeroInNames()
3736
{
3837
$lexer = new Lexer();
39-
$functions = array();
40-
$parser = new Parser($functions);
38+
$parser = new Parser(array());
4139
$parser->parse($lexer->tokenize('foo'), array(0));
4240
}
4341

@@ -47,8 +45,7 @@ public function testParseWithZeroInNames()
4745
public function testParse($node, $expression, $names = array())
4846
{
4947
$lexer = new Lexer();
50-
$functions = array();
51-
$parser = new Parser($functions);
48+
$parser = new Parser(array());
5249
$this->assertEquals($node, $parser->parse($lexer->tokenize($expression), $names));
5350
}
5451

@@ -172,8 +169,7 @@ private function createGetAttrNode($node, $item, $type)
172169
public function testParseWithInvalidPostfixData($expr, $names = array())
173170
{
174171
$lexer = new Lexer();
175-
$functions = array();
176-
$parser = new Parser($functions);
172+
$parser = new Parser(array());
177173
$parser->parse($lexer->tokenize($expr), $names);
178174
}
179175

0 commit comments

Comments
 (0)
0