Beispiel #1
0
 /**
  * @param Func $function
  * A function to add to the code base
  *
  * @return void
  */
 public function addFunction(Func $function)
 {
     // Add it to the map of functions
     $this->fqsen_func_map[$function->getFQSEN()] = $function;
     // Add it to the set of functions and methods
     $this->func_and_method_set->attach($function);
 }
Beispiel #2
0
 /**
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitIf(Node $node) : Context
 {
     // Get the list of scopes for each branch of the
     // conditional
     $scope_list = array_map(function (Context $context) {
         return $context->getScope();
     }, $this->child_context_list);
     $has_else = array_reduce($node->children ?? [], function (bool $carry, $child_node) {
         return $carry || $child_node instanceof Node && empty($child_node->children['cond']);
     }, false);
     // If we're not guaranteed to hit at least one
     // branch, mark the incoming scope as a possibility
     if (!$has_else) {
         $scope_list[] = $this->context->getScope();
     }
     // If there weren't multiple branches, continue on
     // as if the conditional never happened
     if (count($scope_list) < 2) {
         return array_values($this->child_context_list)[0];
     }
     // Get a list of all variables in all scopes
     $variable_map = [];
     foreach ($scope_list as $i => $scope) {
         foreach ($scope->getVariableMap() as $name => $variable) {
             $variable_map[$name] = $variable;
         }
     }
     // A function that determins if a variable is defined on
     // every branch
     $is_defined_on_all_branches = function (string $variable_name) use($scope_list) {
         return array_reduce($scope_list, function (bool $has_variable, Scope $scope) use($variable_name) {
             return $has_variable && $scope->hasVariableWithName($variable_name);
         }, true);
     };
     // Get the intersection of all types for all versions of
     // the variable from every side of the branch
     $union_type = function (string $variable_name) use($scope_list) {
         // Get a list of all variables with the given name from
         // each scope
         $variable_list = array_filter(array_map(function (Scope $scope) use($variable_name) {
             if (!$scope->hasVariableWithName($variable_name)) {
                 return null;
             }
             return $scope->getVariableByName($variable_name);
         }, $scope_list));
         // Get the list of types for each version of the variable
         $type_set_list = array_map(function (Variable $variable) : Set {
             return $variable->getUnionType()->getTypeSet();
         }, $variable_list);
         if (count($type_set_list) < 2) {
             return new UnionType($type_set_list[0] ?? []);
         }
         return new UnionType(Set::unionAll($type_set_list));
     };
     // Clone the incoming scope so we can modify it
     // with the outgoing merged scope
     $scope = clone $this->context->getScope();
     foreach ($variable_map as $name => $variable) {
         // Skip variables that are only partially defined
         if (!$is_defined_on_all_branches($name)) {
             if ($this->context->getIsStrictTypes()) {
                 continue;
             } else {
                 $variable->getUnionType()->addType(NullType::instance());
             }
         }
         // Limit the type of the variable to the subset
         // of types that are common to all branches
         $variable = clone $variable;
         $variable->setUnionType($union_type($name));
         // Add the variable to the outgoing scope
         $scope->addVariable($variable);
     }
     // Set the new scope with only the variables and types
     // that are common to all branches
     return $this->context->withScope($scope);
 }
Beispiel #3
0
 /**
  * @return UnionType
  * Get a new type for each type in this union which is
  * the generic array version of this type. For instance,
  * 'int|float' will produce 'int[]|float[]'.
  */
 public function asGenericArrayTypes() : UnionType
 {
     return new UnionType($this->type_set->map(function (Type $type) : Type {
         return $type->asGenericArrayType();
     }));
 }