public function testFunctionWithDefaultRelativeConstant() { /** @var $function Functions\ElementProjection */ $function = $this->buildFunction('', null, __NAMESPACE__, [], [O\Expression::parameter('value'), O\Expression::parameter('key'), O\Expression::parameter('excessive1', null, O\Expression::constant('__RELATIVE_CONSTANT')), O\Expression::parameter('excessive2', null, O\Expression::value([false]))]); $this->assertSame(false, $function->getParameters()->hasRequiredUnusedParameters()); $this->assertEquals([], $function->getParameters()->getRequiredUnusedParameters()); $this->assertEquals([O\Expression::parameter('excessive1', null, O\Expression::constant('__RELATIVE_CONSTANT')), O\Expression::parameter('excessive2', null, O\Expression::value([false]))], $function->getParameters()->getUnused()); $this->assertEquals(['excessive1' => O\Expression::constant('__RELATIVE_CONSTANT'), 'excessive2' => O\Expression::value([false])], $function->getParameters()->getUnusedParameterDefaultMap()); //This fails in PHP due to strange behaviour with closures and relative constants. //Bug reported: https://bugs.php.net/bug.php?id=67897 if (defined('HHVM_VERSION')) { define('__RELATIVE_CONSTANT', 'in global namespace'); $this->assertEquals(['excessive1' => 'in global namespace', 'excessive2' => [false]], $function->getEvaluationContextFactory()->getEvaluationContext()->getVariableTable()); //Should take precedence define(__NAMESPACE__ . '\\__RELATIVE_CONSTANT', 'in relative namespace'); $this->assertEquals(['excessive1' => 'in relative namespace', 'excessive2' => [false]], $function->getEvaluationContextFactory()->getEvaluationContext()->getVariableTable()); } }
private function parseScalarNode(Node\Scalar $node) { switch (true) { case $node instanceof Node\Scalar\DNumber: case $node instanceof Node\Scalar\LNumber: case $node instanceof Node\Scalar\String_: return Expression::value($node->value); case $node instanceof Node\Scalar\MagicConst\Line: return Expression::value($node->getAttribute('startLine')); case $node instanceof Node\Scalar\MagicConst: return Expression::constant($node->getName()); default: return; } }
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')]]; }
/** * Compiles the expression tree into debug code. * * @return string */ public final function compileDebug() { return (new DynamicExpressionWalker([ValueExpression::getType() => function (ValueExpression $expression) { $value = $expression->getValue(); return !is_scalar($value) && $value !== null ? Expression::constant('{' . Utilities::getTypeOrClass($expression->getValue()) . '}') : $expression; }]))->walk($this)->compile(); }
/** * @dataProvider parsers */ public function testTernary() { $this->assertParsedAs(function () { true ? true : false; }, [O\Expression::ternary(O\Expression::constant('true'), O\Expression::constant('true'), O\Expression::constant('false'))]); $this->assertParsedAs(function () { true ?: false; }, [O\Expression::ternary(O\Expression::constant('true'), null, O\Expression::constant('false'))]); }
/** * @dataProvider parsers */ public function testMagicMethod() { $this->assertParsedAs(function () { __METHOD__; }, [O\Expression::constant('__METHOD__')]); }
/** * @dataProvider parsers */ public function testConstantArrayDereference() { $this->assertParsedAs([ConstantArrayDereference::TYPE, 'constantArrayDereference'], [O\Expression::Index(O\Expression::constant('SOME_ARRAY'), O\Expression::value('foo'))]); }
protected function intersectWithFilteredSubScopeQuery() { return $this->scopeRequest([new Q\Segments\Operation(Q\Segments\Operation::INTERSECT, $this->scopeSource([new Q\Segments\Filter(new Q\Functions\ElementProjection($this->parameter(), self::SCOPE_TYPE, self::SCOPE_NAMESPACE, [$this->parameter() => 'this'], [], [O\Expression::returnExpression(O\Expression::constant('true'))]))]))]); }