Author: Elliot Levin (elliotlevin@hotmail.com)
Example #1
0
 protected function parseQueryExpression(callable $queryFunction, O\IEvaluationContext &$evaluationContext = null)
 {
     $reflection = $this->functionInterpreter->getReflection($queryFunction);
     $evaluationContext = $reflection->asEvaluationContext();
     $function = $this->functionInterpreter->getStructure($reflection);
     $expressions = $function->getBodyExpressions();
     $this->assertCount(1, $expressions);
     //Resolve the parameter variable with the queryable value and $this
     $parameterName = $reflection->getSignature()->getParameterExpressions()[0]->getName();
     $expression = $expressions[0];
     foreach ([$parameterName => $this->queryable, 'this' => $this] as $variable => $value) {
         $variableReplacer = new O\DynamicExpressionWalker([O\VariableExpression::getType() => function (O\VariableExpression $expression) use($variable, &$value) {
             if ($expression->getName() instanceof O\ValueExpression && $expression->getName()->getValue() === $variable) {
                 return O\Expression::value($value);
             } else {
                 return $expression;
             }
         }, O\ClosureExpression::getType() => function ($closure) {
             return $closure;
         }]);
         $expression = $variableReplacer->walk($expression);
     }
     if ($expression instanceof O\ReturnExpression) {
         return $expression->getValue();
     } else {
         return $expression;
     }
 }
 /**
  * @param callable $function
  * @param array    $variableTypeMap
  * @param mixed    $expression
  *
  * @return ITypeAnalysis
  */
 protected function getAnalysis(callable $function, array $variableTypeMap = [], &$expression = null)
 {
     $reflection = $this->functionInterpreter->getReflection($function);
     foreach ($reflection->getSignature()->getParameterExpressions() as $parameterExpression) {
         $variableTypeMap[$parameterExpression->getName()] = $this->typeSystem->getTypeFromTypeHint($parameterExpression->getTypeHint());
     }
     $analysisContext = $this->expressionAnalyser->createAnalysisContext($reflection->asEvaluationContext());
     foreach ($variableTypeMap as $variable => $type) {
         $analysisContext->setExpressionType(O\Expression::variable(O\Expression::value($variable)), $type);
     }
     $bodyExpressions = $this->functionInterpreter->getStructure($reflection)->getBodyExpressions();
     foreach ($bodyExpressions as $expression) {
         if ($expression instanceof O\ReturnExpression) {
             return $this->expressionAnalyser->analyse($analysisContext, $expression->getValue());
         } elseif (count($bodyExpressions) === 1) {
             return $this->expressionAnalyser->analyse($analysisContext, $expression);
         } else {
             $this->expressionAnalyser->analyse($analysisContext, $expression);
         }
     }
 }
 public function hash($value)
 {
     return $this->functionInterpreter->getReflection($value)->getGlobalHash();
 }
Example #4
0
 public function testInternalFunctionsProduceDifferentGlobalHashes()
 {
     $reflection1 = $this->interpreter->getReflection('strlen');
     $reflection2 = $this->interpreter->getReflection('strpos');
     $this->assertNotSame($reflection1->getGlobalHash(), $reflection2->getGlobalHash());
 }
Example #5
0
 protected final function assertParametersAre(callable $function, array $parameterExpressions)
 {
     $this->verifyImplementation();
     $reflection = $this->currentImplementation->getReflection($function);
     $this->assertEquals($reflection->getSignature()->getParameterExpressions(), $parameterExpressions);
 }