当前位置: 首页>>代码示例>>PHP>>正文


PHP Context::withScope方法代码示例

本文整理汇总了PHP中Phan\Language\Context::withScope方法的典型用法代码示例。如果您正苦于以下问题:PHP Context::withScope方法的具体用法?PHP Context::withScope怎么用?PHP Context::withScope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Phan\Language\Context的用法示例。


在下文中一共展示了Context::withScope方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: visitIf

 /**
  * @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 $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
     $common_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->getVariableWithName($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::intersectAll($type_set_list));
     };
     $scope = new Scope();
     foreach ($variable_map as $name => $variable) {
         // Skip variables that are only partially defined
         if (!$is_defined_on_all_branches($name)) {
             continue;
         }
         // Limit the type of the variable to the subset
         // of types that are common to all branches
         $variable = clone $variable;
         $variable->setUnionType($common_union_type($name));
         // Add the variable to the outgoing scope
         $scope->addVariable($variable);
     }
     // print '<'.implode("\t", $scope_list) . "\n";
     // print '>'.$scope."\n";
     // Set the new scope with only the variables and types
     // that are common to all branches
     return $this->context->withScope($scope);
 }
开发者ID:black-silence,项目名称:phan,代码行数:81,代码来源:ContextMergeVisitor.php


注:本文中的Phan\Language\Context::withScope方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。