本文整理汇总了PHP中Phan\Language\Context类的典型用法代码示例。如果您正苦于以下问题:PHP Context类的具体用法?PHP Context怎么用?PHP Context使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @param Context $context
* The context in which the structural element lives
*
* @param string $name,
* The name of the typed structural element
*
* @param UnionType $type,
* A '|' delimited set of types satisfyped by this
* typed structural element.
*
* @param int $flags,
* The flags property contains node specific flags. It is
* always defined, but for most nodes it is always zero.
* ast\kind_uses_flags() can be used to determine whether
* a certain kind has a meaningful flags value.
*/
public function __construct(Context $context, string $name, UnionType $type, int $flags)
{
$this->context = $context;
$this->name = $name;
$this->type = $type;
$this->flags = $flags;
$this->setIsInternal($context->isInternal());
}
示例2: testSimple
public function testSimple()
{
$context = new Context();
$context_namespace = $context->withNamespace('\\A');
$context_class = $context_namespace->withScope(new ClassScope($context_namespace->getScope(), FullyQualifiedClassName::fromFullyQualifiedString('\\A\\B')));
$context_method = $context_namespace->withScope(new FunctionLikeScope($context_namespace->getScope(), FullyQualifiedMethodName::fromFullyQualifiedString('\\A\\b::c')));
$this->assertTrue(!empty($context));
$this->assertTrue(!empty($context_namespace));
$this->assertTrue(!empty($context_class));
$this->assertTrue(!empty($context_method));
}
示例3: testSimple
public function testSimple()
{
$context = new Context();
$context_namespace = $context->withNamespace('\\A');
$context_class = $context_namespace->withClassFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\A\\B'));
$context_method = $context_namespace->withMethodFQSEN(FullyQualifiedMethodName::fromFullyQualifiedString('\\A\\b::c'));
$this->assertTrue(!empty($context));
$this->assertTrue(!empty($context_namespace));
$this->assertTrue(!empty($context_class));
$this->assertTrue(!empty($context_method));
}
示例4: visitVar
/**
* @param Node $node
* A node to parse
*
* @return Context
* A new or an unchanged context resulting from
* parsing the node
*/
public function visitVar(Node $node) : Context
{
$variable_name = AST::variableName($node);
// Check to see if the variable already exists
if ($this->context->getScope()->hasVariableWithName($variable_name)) {
$variable = $this->context->getScope()->getVariableWithName($variable_name);
// If we're assigning to an array element then we don't
// know what the constitutation of the parameter is
// outside of the scope of this assignment, so we add to
// its union type rather than replace it.
if ($this->is_dim_assignment) {
$variable->getUnionType()->addUnionType($this->right_type);
} else {
// If the variable isn't a pass-by-reference paramter
// we clone it so as to not disturb its previous types
// as we replace it.
if (!($variable instanceof Parameter && $variable->isPassByReference())) {
$variable = clone $variable;
}
$variable->setUnionType($this->right_type);
}
$this->context->addScopeVariable($variable);
return $this->context;
}
$variable = Variable::fromNodeInContext($this->assignment_node, $this->context, $this->code_base);
// Set that type on the variable
$variable->getUnionType()->addUnionType($this->right_type);
// Note that we're not creating a new scope, just
// adding variables to the existing scope
$this->context->addScopeVariable($variable);
return $this->context;
}
示例5: visitProp
/**
* @param Node $node
* A node of the type indicated by the method name that we'd
* like to figure out the type that it produces.
*
* @return string
* The class name represented by the given call
*/
public function visitProp(Node $node) : string
{
if (!($node->children['expr']->kind == \ast\AST_VAR && !$node->children['expr']->children['name'] instanceof Node)) {
return '';
}
// $var->prop->method()
$var = $node->children['expr'];
$class = null;
if ($var->children['name'] == 'this') {
// If we're not in a class scope, 'this' won't work
if (!$this->context->isInClassScope()) {
Log::err(Log::ESTATIC, 'Using $this when not in object context', $this->context->getFile(), $node->lineno);
return '';
}
// $this->$node->method()
if ($node->children['prop'] instanceof Node) {
// Too hard. Giving up.
return '';
}
$class = $this->context->getClassInScope($this->code_base);
} else {
// Get the list of viable class types for the
// variable
$union_type = AST::varUnionType($this->context, $var)->nonNativeTypes()->nonGenericArrayTypes();
if ($union_type->isEmpty()) {
return '';
}
$class_fqsen = $union_type->head()->asFQSEN();
if (!$this->code_base->hasClassWithFQSEN($class_fqsen)) {
return '';
}
$class = $this->code_base->getClassByFQSEN($class_fqsen);
}
$property_name = $node->children['prop'];
if (!$class->hasPropertyWithName($this->code_base, $property_name)) {
// If we can't find the property, there's
// no type. Thie issue should be caught
// elsewhere.
return '';
}
try {
$property = $class->getPropertyByNameInContext($this->code_base, $property_name, $this->context);
} catch (AccessException $exception) {
Log::err(Log::EACCESS, $exception->getMessage(), $this->context->getFile(), $node->lineno);
return '';
}
$union_type = $property->getUnionType()->nonNativeTypes();
if ($union_type->isEmpty()) {
// If we don't have a type on the property we
// can't figure out the class type.
return '';
} else {
// Return the first type on the property
// that could be a reference to a class
return (string) $union_type->head()->asFQSEN();
}
// No such property was found, or none were classes
// that could be found
return '';
}
示例6: analyzeBackwardCompatibility
/**
* Perform some backwards compatibility checks on a node
*
* @return void
*/
public function analyzeBackwardCompatibility()
{
if (!Config::get()->backward_compatibility_checks) {
return;
}
if (empty($this->node->children['expr'])) {
return;
}
if ($this->node->kind === \ast\AST_STATIC_CALL || $this->node->kind === \ast\AST_METHOD_CALL) {
return;
}
$llnode = $this->node;
if ($this->node->kind !== \ast\AST_DIM) {
if (!$this->node->children['expr'] instanceof Node) {
return;
}
if ($this->node->children['expr']->kind !== \ast\AST_DIM) {
(new ContextNode($this->code_base, $this->context, $this->node->children['expr']))->analyzeBackwardCompatibility();
return;
}
$temp = $this->node->children['expr']->children['expr'];
$llnode = $this->node->children['expr'];
$lnode = $temp;
} else {
$temp = $this->node->children['expr'];
$lnode = $temp;
}
if (!($temp->kind == \ast\AST_PROP || $temp->kind == \ast\AST_STATIC_PROP)) {
return;
}
while ($temp instanceof Node && ($temp->kind == \ast\AST_PROP || $temp->kind == \ast\AST_STATIC_PROP)) {
$llnode = $lnode;
$lnode = $temp;
// Lets just hope the 0th is the expression
// we want
$temp = array_values($temp->children)[0];
}
if (!$temp instanceof Node) {
return;
}
// Foo::$bar['baz'](); is a problem
// Foo::$bar['baz'] is not
if ($lnode->kind === \ast\AST_STATIC_PROP && $this->node->kind !== \ast\AST_CALL) {
return;
}
// $this->$bar['baz']; is a problem
// $this->bar['baz'] is not
if ($lnode->kind === \ast\AST_PROP && !$lnode->children['prop'] instanceof Node && !$llnode->children['prop'] instanceof Node) {
return;
}
if (($lnode->children['prop'] instanceof Node && $lnode->children['prop']->kind == \ast\AST_VAR || !empty($lnode->children['class']) && $lnode->children['class'] instanceof Node && ($lnode->children['class']->kind == \ast\AST_VAR || $lnode->children['class']->kind == \ast\AST_NAME) || !empty($lnode->children['expr']) && $lnode->children['expr'] instanceof Node && ($lnode->children['expr']->kind == \ast\AST_VAR || $lnode->children['expr']->kind == \ast\AST_NAME)) && ($temp->kind == \ast\AST_VAR || $temp->kind == \ast\AST_NAME)) {
$ftemp = new \SplFileObject($this->context->getFile());
$ftemp->seek($this->node->lineno - 1);
$line = $ftemp->current();
unset($ftemp);
if (strpos($line, '}[') === false || strpos($line, ']}') === false || strpos($line, '>{') === false) {
Issue::maybeEmit($this->code_base, $this->context, Issue::CompatiblePHP7, $this->node->lineno ?? 0);
}
}
}
示例7: fromStringInContext
/**
* @param Context $context
* The context in which the FQSEN string was found
*
* @param $fqsen_string
* An FQSEN string like '\Namespace\Class'
*/
public static function fromStringInContext(string $fqsen_string, Context $context) : FullyQualifiedGlobalStructuralElement
{
// Check to see if we're fully qualified
if (0 === strpos($fqsen_string, '\\')) {
return static::fromFullyQualifiedString($fqsen_string);
}
// Split off the alternate ID
$parts = explode(',', $fqsen_string);
$fqsen_string = $parts[0];
$alternate_id = (int) ($parts[1] ?? 0);
assert(is_int($alternate_id), "Alternate must be an integer in {$fqsen_string}");
$parts = explode('\\', $fqsen_string);
$name = array_pop($parts);
assert(!empty($name), "The name cannot be empty in {$fqsen_string}");
// Check for a name map
if ($context->hasNamespaceMapFor(static::getNamespaceMapType(), $name)) {
return $context->getNamespaceMapFor(static::getNamespaceMapType(), $name);
}
$namespace = implode('\\', array_filter($parts));
// n.b.: Functions must override this method because
// they don't prefix the namespace for naked
// calls
if (empty($namespace)) {
$namespace = $context->getNamespace();
}
return static::make($namespace, $name, $alternate_id);
}
示例8: visitMethodCall
/**
* Visit a node with kind `\ast\AST_METHOD_CALL`
*
* @param Node $node
* A node of the type indicated by the method name that we'd
* like to figure out the type that it produces.
*
* @return UnionType
* The set of types that are possibly produced by the
* given node
*/
public function visitMethodCall(Node $node) : UnionType
{
$class_name = AST::classNameFromNode($this->context, $this->code_base, $node);
if (empty($class_name)) {
return new UnionType();
}
$class_fqsen = FullyQualifiedClassName::fromstringInContext($class_name, $this->context);
assert($this->code_base->hasClassWithFQSEN($class_fqsen), "Class {$class_fqsen} must exist");
$clazz = $this->code_base->getClassByFQSEN($class_fqsen);
$method_name = $node->children['method'];
// Give up on any complicated nonsense where the
// method name is a variable such as in
// `$variable->$function_name()`.
if ($method_name instanceof Node) {
return new UnionType();
}
// Method names can some times turn up being
// other method calls.
assert(is_string($method_name), "Method name must be a string. Something else given.");
if (!$clazz->hasMethodWithName($this->code_base, $method_name)) {
Log::err(Log::EUNDEF, "call to undeclared method {$class_fqsen}->{$method_name}()", $this->context->getFile(), $node->lineno);
return new UnionType();
}
$method = $clazz->getMethodByNameInContext($this->code_base, $method_name, $this->context);
return $method->getUnionType();
}
示例9: visitClosure
/**
* @param Node $node
* A node to parse
*
* @return void
*/
public function visitClosure(Decl $node)
{
try {
$method = (new ContextNode($this->code_base, $this->context->withLineNumberStart($node->lineno ?? 0), $node))->getClosure();
$method->addReference($this->context);
} catch (\Exception $exception) {
// Swallow it
}
}
示例10: classExistsOrIsNative
/**
* @return bool
* False if the class name doesn't point to a known class
*/
private function classExistsOrIsNative(Node $node) : bool
{
if ($this->classExists()) {
return true;
}
$type = UnionType::fromStringInContext($this->class_name, $this->context);
if ($type->isNativeType()) {
return true;
}
Log::err(Log::EUNDEF, "reference to undeclared class {$this->class_fqsen}", $this->context->getFile(), $node->lineno);
return false;
}
示例11: fromStringInContext
/**
* @param Context $context
* The context in which the FQSEN string was found
*
* @param $fqsen_string
* An FQSEN string like '\Namespace\Class::methodName'
*
* @return FullyQualifiedMethodName
*/
public static function fromStringInContext(string $fqsen_string, Context $context)
{
// Test to see if we have a class defined
if (false === strpos($fqsen_string, '::')) {
$fully_qualified_class_name = $context->getClassFQSEN();
} else {
assert(false !== strpos($fqsen_string, '::'), "Fully qualified class element lacks '::' delimiter in {$fqsen_string}.");
list($class_name_string, $fqsen_string) = explode('::', $fqsen_string);
$fully_qualified_class_name = FullyQualifiedClassName::fromStringInContext($class_name_string, $context);
}
// Split off the alternate ID
$parts = explode(',', $fqsen_string);
$name = $parts[0];
$alternate_id = (int) ($parts[1] ?? 0);
assert(is_int($alternate_id), "Alternate must be an integer in {$fqsen_string}");
return static::make($fully_qualified_class_name, $name, $alternate_id);
}
示例12: visitVar
/**
* @param Node $node
* A node to parse
*
* @return Context
* A new or an unchanged context resulting from
* parsing the node
*/
public function visitVar(Node $node) : Context
{
$variable_name = AST::variableName($node);
// Check to see if the variable already exists
if ($this->context->getScope()->hasVariableWithName($variable_name)) {
$variable = $this->context->getScope()->getVariableWithName($variable_name);
$variable->setUnionType($this->right_type);
$this->context->addScopeVariable($variable);
return $this->context;
}
$variable = Variable::fromNodeInContext($this->assignment_node, $this->context, $this->code_base);
// Set that type on the variable
$variable->getUnionType()->addUnionType($this->right_type);
// Note that we're not creating a new scope, just
// adding variables to the existing scope
$this->context->addScopeVariable($variable);
return $this->context;
}
示例13: visitClassNode
private function visitClassNode(Node $node) : UnionType
{
// Things of the form `new $class_name();`
if ($node->kind == \ast\AST_VAR) {
return new UnionType();
}
// Anonymous class of form `new class { ... }`
if ($node->kind == \ast\AST_CLASS && $node->flags & \ast\flags\CLASS_ANONYMOUS) {
// Generate a stable name for the anonymous class
$anonymous_class_name = (new ContextNode($this->code_base, $this->context, $node))->getUnqualifiedNameForAnonymousClass();
// Turn that into a fully qualified name
$fqsen = FullyQualifiedClassName::fromStringInContext($anonymous_class_name, $this->context);
// Turn that into a union type
return Type::fromFullyQualifiedString((string) $fqsen)->asUnionType();
}
// Things of the form `new $method->name()`
if ($node->kind !== \ast\AST_NAME) {
return new UnionType();
}
// Get the name of the class
$class_name = $node->children['name'];
// If this is a straight-forward class name, recurse into the
// class node and get its type
if (!Type::isSelfTypeString($class_name)) {
// TODO: does anyone else call this method?
return self::unionTypeFromClassNode($this->code_base, $this->context, $node);
}
// This is a self-referential node
if (!$this->context->isInClassScope()) {
Log::err(Log::ESTATIC, "Cannot access {$class_name} when not in a class scope", $this->context->getFile(), $node->lineno);
return new UnionType();
}
// Reference to a parent class
if ($class_name === 'parent') {
$class = $this->context->getClassInScope($this->code_base);
if (!$class->hasParentClassFQSEN()) {
Log::err(Log::ESTATIC, "Reference to parent of parentless class {$class->getFQSEN()}", $this->context->getFile(), $node->lineno);
return new UnionType();
}
return Type::fromFullyQualifiedString((string) $class->getParentClassFQSEN())->asUnionType();
}
return Type::fromFullyQualifiedString((string) $this->context->getClassFQSEN())->asUnionType();
}
示例14: visitNew
/**
* @param Node $node
* A node of the type indicated by the method name that we'd
* like to figure out the type that it produces.
*
* @return string
* The class name represented by the given call
*/
public function visitNew(Node $node) : string
{
// Things of the form `new $class_name();`
if ($node->children['class']->kind == \ast\AST_VAR) {
return '';
}
// Anonymous class
// $v = new class { ... }
if ($node->children['class']->kind == \ast\AST_CLASS && $node->children['class']->flags & \ast\flags\CLASS_ANONYMOUS) {
return (new ContextNode($this->code_base, $this->context, $node->children['class']))->getUnqualifiedNameForAnonymousClass();
}
// Things of the form `new $method->name()`
if ($node->children['class']->kind !== \ast\AST_NAME) {
return '';
}
$class_name = $node->children['class']->children['name'];
if (!in_array($class_name, ['self', 'static', 'parent'])) {
return (string) UnionTypeVisitor::unionTypeFromClassNode($this->code_base, $this->context, $node->children['class']);
}
if (!$this->context->isInClassScope()) {
Log::err(Log::ESTATIC, "Cannot access {$class_name}:: when no class scope is active", $this->context->getFile(), $node->lineno);
return '';
}
if ($class_name == 'static') {
return (string) $this->context->getClassFQSEN();
}
if ($class_name == 'self') {
if ($this->context->isGlobalScope()) {
assert(false, "Unimplemented branch is required for {$this->context}");
} else {
return (string) $this->context->getClassFQSEN();
}
}
if ($class_name == 'parent') {
$clazz = $this->context->getClassInScope($this->code_base);
if (!$clazz->hasParentClassFQSEN()) {
return '';
}
return (string) $clazz->getParentClassFQSEN();
}
return '';
}
示例15: visitInstanceof
/**
* @param Node $node
* A node to parse
*
* @return Context
* A new or an unchanged context resulting from
* parsing the node
*/
public function visitInstanceof(Node $node) : Context
{
// Only look at things of the form
// `$variable instanceof ClassName`
if ($node->children['expr']->kind !== \ast\AST_VAR) {
return $this->context;
}
try {
// Get the variable we're operating on
$variable = (new ContextNode($this->code_base, $this->context, $node->children['expr']))->getVariable();
// Get the type that we're checking it against
$type = UnionType::fromNode($this->context, $this->code_base, $node->children['class']);
// Make a copy of the variable
$variable = clone $variable;
// Add the type to the variable
$variable->getUnionType()->addUnionType($type);
// Overwrite the variable with its new type
$this->context->addScopeVariable($variable);
} catch (\Exception $exception) {
// Swallow it
}
return $this->context;
}