Phan\AST\ContextNode::getMethod PHP Method

getMethod() public method

public getMethod ( ast\Node | string $method_name, boolean $is_static ) : Method
$method_name ast\Node | string Either then name of the method or a node that produces the name of the method.
$is_static boolean Set to true if this is a static method call
return Phan\Language\Element\Method A method with the given name on the class referenced from the given node
    public function getMethod($method_name, bool $is_static) : Method
    {
        if ($method_name instanceof Node) {
            // The method_name turned out to be a variable.
            // There isn't much we can do to figure out what
            // it's referring to.
            throw new NodeException($method_name, "Unexpected method node");
        }
        assert(is_string($method_name), "Method name must be a string. Found non-string in context.");
        try {
            $class_list = (new ContextNode($this->code_base, $this->context, $this->node->children['expr'] ?? $this->node->children['class']))->getClassList();
        } catch (CodeBaseException $exception) {
            throw new IssueException(Issue::fromType(Issue::UndeclaredClassMethod)($this->context->getFile(), $this->node->lineno ?? 0, [$method_name, (string) $exception->getFQSEN()]));
        }
        // If there were no classes on the left-type, figure
        // out what we were trying to call the method on
        // and send out an error.
        if (empty($class_list)) {
            $union_type = UnionTypeVisitor::unionTypeFromClassNode($this->code_base, $this->context, $this->node->children['expr'] ?? $this->node->children['class']);
            if (!$union_type->isEmpty() && $union_type->isNativeType() && !$union_type->hasAnyType([MixedType::instance(), ObjectType::instance(), StringType::instance()]) && !(Config::get()->null_casts_as_any_type && $union_type->hasType(NullType::instance()))) {
                throw new IssueException(Issue::fromType(Issue::NonClassMethodCall)($this->context->getFile(), $this->node->lineno ?? 0, [$method_name, (string) $union_type]));
            }
            throw new NodeException($this->node, "Can't figure out method call for {$method_name}");
        }
        // Hunt to see if any of them have the method we're
        // looking for
        foreach ($class_list as $i => $class) {
            if ($class->hasMethodWithName($this->code_base, $method_name)) {
                return $class->getMethodByNameInContext($this->code_base, $method_name, $this->context);
            } else {
                if (!$is_static && $class->hasCallMethod($this->code_base)) {
                    return $class->getCallMethod($this->code_base);
                } else {
                    if ($is_static && $class->hasCallStaticMethod($this->code_base)) {
                        return $class->getCallStaticMethod($this->code_base);
                    }
                }
            }
        }
        // Figure out an FQSEN for the method we couldn't find
        $method_fqsen = FullyQualifiedMethodName::make($class_list[0]->getFQSEN(), $method_name);
        if ($is_static) {
            throw new IssueException(Issue::fromType(Issue::UndeclaredStaticMethod)($this->context->getFile(), $this->node->lineno ?? 0, [(string) $method_fqsen]));
        }
        throw new IssueException(Issue::fromType(Issue::UndeclaredMethod)($this->context->getFile(), $this->node->lineno ?? 0, [(string) $method_fqsen]));
    }

Usage Example

コード例 #1
0
 /**
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitNew(Node $node) : Context
 {
     try {
         $context_node = new ContextNode($this->code_base, $this->context, $node);
         $method = $context_node->getMethod('__construct', false);
         // Get the class and increase its reference
         // count
         $class = $context_node->getClass();
         $class->addReference($this->context);
         $this->analyzeCallToMethod($this->code_base, $method, $node);
     } catch (CodeBaseException $exception) {
         Log::err(Log::EUNDEF, $exception->getMessage(), $this->context->getFile(), $node->lineno);
         return $this->context;
     } catch (\Exception $exception) {
         // If we can't figure out what kind of a call
         // this is, don't worry about it
         return $this->context;
     }
     return $this->context;
 }
All Usage Examples Of Phan\AST\ContextNode::getMethod