本文整理汇总了PHP中Phan\Language\Context::getIsStrictTypes方法的典型用法代码示例。如果您正苦于以下问题:PHP Context::getIsStrictTypes方法的具体用法?PHP Context::getIsStrictTypes怎么用?PHP Context::getIsStrictTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phan\Language\Context
的用法示例。
在下文中一共展示了Context::getIsStrictTypes方法的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 $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);
}