本文整理汇总了PHP中Phan\Language\UnionType::canCastToUnionType方法的典型用法代码示例。如果您正苦于以下问题:PHP UnionType::canCastToUnionType方法的具体用法?PHP UnionType::canCastToUnionType怎么用?PHP UnionType::canCastToUnionType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phan\Language\UnionType
的用法示例。
在下文中一共展示了UnionType::canCastToUnionType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: visitPropDecl
/**
* Visit a node with kind `\ast\AST_PROP_DECL`
*
* @param Node $node
* A node to parse
*
* @return Context
* A new or an unchanged context resulting from
* parsing the node
*/
public function visitPropDecl(Node $node) : Context
{
// Bomb out if we're not in a class context
$clazz = $this->getContextClass();
// Get a comment on the property declaration
$comment = Comment::fromStringInContext($node->children[0]->docComment ?? '', $this->context);
foreach ($node->children ?? [] as $i => $child_node) {
// Ignore children which are not property elements
if (!$child_node || $child_node->kind != \ast\AST_PROP_ELEM) {
continue;
}
// If something goes wrong will getting the type of
// a property, we'll store it as a future union
// type and try to figure it out later
$future_union_type = null;
try {
// Get the type of the default
$union_type = UnionType::fromNode($this->context, $this->code_base, $child_node->children['default'], false);
} catch (IssueException $exception) {
$future_union_type = new FutureUnionType($this->code_base, $this->context, $child_node->children['default']);
$union_type = new UnionType();
}
// Don't set 'null' as the type if thats the default
// given that its the default default.
if ($union_type->isType(NullType::instance())) {
$union_type = new UnionType();
}
$property_name = $child_node->children['name'];
assert(is_string($property_name), 'Property name must be a string. ' . 'Got ' . print_r($property_name, true) . ' at ' . $this->context);
$property = new Property(clone $this->context->withLineNumberStart($child_node->lineno ?? 0), is_string($child_node->children['name']) ? $child_node->children['name'] : '_error_', $union_type, $node->flags ?? 0);
$property->setFQSEN(FullyQualifiedPropertyName::make($clazz->getFQSEN(), $property->getName()));
// Add the property to the class
$clazz->addProperty($this->code_base, $property);
$property->setSuppressIssueList($comment->getSuppressIssueList());
// Look for any @var declarations
if ($variable = $comment->getVariableList()[$i] ?? null) {
if ((string) $union_type != 'null' && !$union_type->canCastToUnionType($variable->getUnionType())) {
$this->emitIssue(Issue::TypeMismatchProperty, $child_node->lineno ?? 0, (string) $union_type, (string) $property->getFQSEN(), (string) $variable->getUnionType());
}
// Set the declared type to the doc-comment type and add
// |null if the default value is null
$property->getUnionType()->addUnionType($variable->getUnionType());
}
// Wait until after we've added the (at)var type
// before setting the future so that calling
// $property->getUnionType() doesn't force the
// future to be reified.
if (!empty($future_union_type)) {
$property->setFutureUnionType($future_union_type);
}
}
return $this->context;
}