public function testFunctionGetsReturnWithoutValueExpression() { /** @var $function Functions\ProjectionBase */ $function = $this->functionWithEmptyReturnStatement(); $this->assertSame(true, $function->hasReturnExpression()); $this->assertEquals(O\Expression::returnExpression(), $function->getReturnExpression()); $this->assertSame(false, $function->hasReturnValueExpression()); $this->assertEquals(null, $function->getReturnValueExpression()); }
/** * @dataProvider parsers */ public function testReturnValueStatement() { $this->assertParsedAs(function () { return 1; }, [O\Expression::returnExpression(O\Expression::value(1))]); $this->assertParsedAs(function () { return null; }, [O\Expression::returnExpression(O\Expression::constant('null'))]); $this->assertParsedAs(function () { return ''; }, [O\Expression::returnExpression(O\Expression::value(''))]); }
public function testFunctionWithReturnStatement() { $function = $this->functionWithReturnStatement(); $this->assertSame('', $function->getCallableId()); $this->assertSame(1, $function->getParameters()->count()); $this->assertEquals([O\Expression::parameter('foo')], $function->getParameters()->getAll()); $this->assertEquals(['param' => 'scope'], $function->getParameterScopedVariableMap()); $this->assertEquals(['', 'param'], $function->getParameterIds()); $this->assertSame(true, $function->hasScopeType()); $this->assertSame(__CLASS__, $function->getScopeType()); $this->assertSame(true, $function->hasNamespace()); $this->assertSame(__NAMESPACE__, $function->getNamespace()); $this->assertSame(4, $function->countBodyExpressions()); $this->assertSame(2, $function->countBodyExpressionsUntilReturn()); $this->assertEquals([O\Expression::value('before'), O\Expression::returnExpression(O\Expression::value('return')), O\Expression::value('after'), O\Expression::functionCall(O\Expression::value('boom'))], $function->getBodyExpressions()); $this->assertEquals([O\Expression::value('before'), O\Expression::returnExpression(O\Expression::value('return'))], $function->getBodyExpressionsUntilReturn()); $this->assertEquals(new O\EvaluationContext(__NAMESPACE__, __CLASS__, null, ['scope' => 'value']), $function->getEvaluationContextFactory()->getEvaluationContext(new ResolvedParameterRegistry(['param' => 'value']))); }
private function parseStatementNode(Node\Stmt $node) { switch (true) { case $node instanceof Node\Stmt\Return_: return Expression::returnExpression($node->expr !== null ? $this->parseNode($node->expr) : null); case $node instanceof Node\Stmt\Throw_: return Expression::throwExpression($this->parseNode($node->expr)); case $node instanceof Node\Stmt\Unset_: return Expression::unsetExpression($this->parseNodes($node->vars)); default: $this->verifyNotControlStructure($node); throw new ASTException('Cannot parse AST with unknown statement node: %s', get_class($node)); } }
public function expressions() { return [[O\Expression::arrayExpression([])], [O\Expression::arrayItem(null, O\Expression::value(0), false)], [O\Expression::assign(O\Expression::value(0), O\Operators\Assignment::EQUAL, O\Expression::value(0))], [O\Expression::binaryOperation(O\Expression::value(0), O\Operators\Binary::ADDITION, O\Expression::value(0))], [O\Expression::unaryOperation(O\Operators\Unary::PLUS, O\Expression::value(0))], [O\Expression::cast(O\Operators\Cast::STRING, O\Expression::value(0))], [O\Expression::closure(false, false, [], [], [])], [O\Expression::closureUsedVariable('var')], [O\Expression::emptyExpression(O\Expression::value(0))], [O\Expression::field(O\Expression::value(0), O\Expression::value(0))], [O\Expression::functionCall(O\Expression::value(0))], [O\Expression::index(O\Expression::value(0), O\Expression::value(0))], [O\Expression::invocation(O\Expression::value(0))], [O\Expression::issetExpression([O\Expression::value(0)])], [O\Expression::unsetExpression([O\Expression::value(0)])], [O\Expression::methodCall(O\Expression::value(0), O\Expression::value(0))], [O\Expression::newExpression(O\Expression::value(0))], [O\Expression::parameter('')], [O\Expression::argument(O\Expression::value(0))], [O\Expression::returnExpression()], [O\Expression::staticMethodCall(O\Expression::value(0), O\Expression::value(0))], [O\Expression::staticField(O\Expression::value(0), O\Expression::value(0))], [O\Expression::ternary(O\Expression::value(0), null, O\Expression::value(0))], [O\Expression::throwExpression(O\Expression::value(0))], [O\Expression::value(0)], [O\Expression::variable(O\Expression::value(0))], [O\Expression::constant('foo')], [O\Expression::classConstant(O\Expression::value(0), 'foo')]]; }
/** * @dataProvider parsers */ public function testInvokableObject() { $this->assertParsedAs(new InvokableObject(), [O\Expression::returnExpression(O\Expression::value(1.1))]); }
/** * Creates an expression evaluator for the expression with * the supplied context. * * @param IEvaluationContext|null $context * * @return IEvaluator */ public function asEvaluator(IEvaluationContext $context = null) { return CompiledEvaluator::fromExpressions([Expression::returnExpression($this)], $context); }
protected final function assertReturn(callable $function, O\Expression $valueExpression) { return $this->assertParsedAs($function, [O\Expression::returnExpression($valueExpression)]); }
/** * @dataProvider parsers */ public function testThatChainedMethodCalls() { $this->assertReturn(function (\Pinq\ITraversable $traversable) { return $traversable->asArray(); }, O\Expression::methodCall(O\Expression::variable(O\Expression::value('traversable')), O\Expression::value('asArray'))); $this->assertReturn(function (\Pinq\ITraversable $traversable) { return $traversable->where(function ($i) { return $i > 0; })->all(function ($i) { return $i % 2 === 0; }); }, O\Expression::methodCall(O\Expression::methodCall(O\Expression::variable(O\Expression::value('traversable')), O\Expression::value('where'), [O\Expression::argument(O\Expression::closure(false, false, [O\Expression::parameter('i')], [], [O\Expression::returnExpression(O\Expression::binaryOperation(O\Expression::variable(O\Expression::value('i')), O\Operators\Binary::GREATER_THAN, O\Expression::value(0)))]))]), O\Expression::value('all'), [O\Expression::argument(O\Expression::closure(false, false, [O\Expression::parameter('i')], [], [O\Expression::returnExpression(O\Expression::binaryOperation(O\Expression::binaryOperation(O\Expression::variable(O\Expression::value('i')), O\Operators\Binary::MODULUS, O\Expression::value(2)), O\Operators\Binary::IDENTITY, O\Expression::value(0)))]))])); }
protected function exampleFromDocsQuery() { $rowExpression = O\Expression::variable(O\Expression::value('row')); return $this->scopeRequest([new Q\Segments\Filter(new Q\Functions\ElementProjection($this->parameter(), self::SCOPE_TYPE, self::SCOPE_NAMESPACE, [$this->parameter() => 'this'], [O\Expression::parameter('row')], [O\Expression::returnExpression(O\Expression::binaryOperation(O\Expression::index($rowExpression, O\Expression::value('age')), O\Operators\Binary::LESS_THAN_OR_EQUAL_TO, O\Expression::value(50)))])), new Q\Segments\OrderBy([new Q\Segments\Ordering($this->indexProjection('firstName'), $this->parameter()), new Q\Segments\Ordering($this->indexProjection('lastName'), $this->parameter())]), new Q\Segments\Range($this->parameter(), $this->parameter()), new Q\Segments\IndexBy($this->indexProjection('phoneNumber')), new Q\Segments\Select(new Q\Functions\ElementProjection($this->parameter(), self::SCOPE_TYPE, self::SCOPE_NAMESPACE, [$this->parameter() => 'this'], [O\Expression::parameter('row')], [O\Expression::returnExpression(O\Expression::arrayExpression([O\Expression::arrayItem(O\Expression::value('fullName'), O\Expression::binaryOperation(O\Expression::binaryOperation(O\Expression::index($rowExpression, O\Expression::value('firstName')), O\Operators\Binary::CONCATENATION, O\Expression::value(' ')), O\Operators\Binary::CONCATENATION, O\Expression::index($rowExpression, O\Expression::value('lastName')))), O\Expression::arrayItem(O\Expression::value('address'), O\Expression::index($rowExpression, O\Expression::value('address'))), O\Expression::arrayItem(O\Expression::value('dateOfBirth'), O\Expression::index($rowExpression, O\Expression::value('dateOfBirth')))]))]))]); }
public function testWithSpecialVariableNames() { $this->assertSame('1,2,3', O\CompiledEvaluator::fromExpressions([O\Expression::returnExpression(O\Expression::variable(O\Expression::value('a special var--')))], O\EvaluationContext::globalScope(null, ['a special var--' => '1,2,3']))->evaluate()); }