fromObject() public static method

public static fromObject ( $object ) : Type
return Type Get a type for the given object
Example #1
0
 /**
  * @param CodeBase $code_base
  * A reference to the entire code base in which this
  * context exists
  *
  * @param ReflectionClass $class
  * A reflection class representing a builtin class.
  *
  * @return Clazz
  * A Class structural element representing the given named
  * builtin.
  */
 public static function fromReflectionClass(CodeBase $code_base, \ReflectionClass $class) : Clazz
 {
     // Build a set of flags based on the constitution
     // of the built-in class
     $flags = 0;
     if ($class->isFinal()) {
         $flags = \ast\flags\CLASS_FINAL;
     } else {
         if ($class->isInterface()) {
             $flags = \ast\flags\CLASS_INTERFACE;
         } else {
             if ($class->isTrait()) {
                 $flags = \ast\flags\CLASS_TRAIT;
             }
         }
     }
     if ($class->isAbstract()) {
         $flags |= \ast\flags\CLASS_ABSTRACT;
     }
     $context = new Context();
     // Build a base class element
     $clazz = new Clazz($context, $class->getName(), UnionType::fromStringInContext($class->getName(), $context), $flags);
     // If this class has a parent class, add it to the
     // class info
     if ($parent_class = $class->getParentClass()) {
         $parent_class_fqsen = FullyQualifiedClassName::fromFullyQualifiedString('\\' . $parent_class->getName());
         $clazz->setParentClassFQSEN($parent_class_fqsen);
     }
     foreach ($class->getDefaultProperties() as $name => $value) {
         // TODO: whats going on here?
         $reflection_property = new \ReflectionProperty($class->getName(), $name);
         $property = new Property($context->withClassFQSEN($clazz->getFQSEN()), $name, Type::fromObject($value)->asUnionType(), 0);
         $clazz->addProperty($code_base, $property);
     }
     foreach ($class->getInterfaceNames() as $name) {
         $clazz->addInterfaceClassFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getTraitNames() as $name) {
         $clazz->addTraitFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getConstants() as $name => $value) {
         $clazz->addConstant($code_base, new Constant($context, $name, Type::fromObject($value)->asUnionType(), 0));
     }
     foreach ($class->getMethods() as $reflection_method) {
         $method_list = Method::methodListFromReflectionClassAndMethod($context->withClassFQSEN($clazz->getFQSEN()), $code_base, $class, $reflection_method);
         foreach ($method_list as $method) {
             $clazz->addMethod($code_base, $method);
         }
     }
     return $clazz;
 }
Example #2
0
 /**
  * @param Context $context
  * @param CodeBase $code_base
  * @param Node|string|null $node
  *
  * @return UnionType
  *
  * @see \Phan\Deprecated\Pass2::node_type
  * Formerly 'function node_type'
  */
 public static function fromNode(Context $context, CodeBase $code_base, $node) : UnionType
 {
     if (!$node instanceof Node) {
         if ($node === null) {
             return new UnionType();
         }
         return Type::fromObject($node)->asUnionType();
     }
     return (new Element($node))->acceptKindVisitor(new UnionTypeVisitor($context, $code_base));
 }
Example #3
0
 /**
  * Visit a node with kind `\ast\AST_UNARY_MINUS`
  *
  * @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 visitUnaryMinus(Node $node) : UnionType
 {
     return Type::fromObject($node->children['expr'])->asUnionType();
 }
Example #4
0
File: Clazz.php Project: etsy/phan
 /**
  * @param CodeBase $code_base
  * A reference to the entire code base in which this
  * context exists
  *
  * @param ReflectionClass $class
  * A reflection class representing a builtin class.
  *
  * @return Clazz
  * A Class structural element representing the given named
  * builtin.
  */
 public static function fromReflectionClass(CodeBase $code_base, \ReflectionClass $class) : Clazz
 {
     // Build a set of flags based on the constitution
     // of the built-in class
     $flags = 0;
     if ($class->isFinal()) {
         $flags = \ast\flags\CLASS_FINAL;
     } elseif ($class->isInterface()) {
         $flags = \ast\flags\CLASS_INTERFACE;
     } elseif ($class->isTrait()) {
         $flags = \ast\flags\CLASS_TRAIT;
     }
     if ($class->isAbstract()) {
         $flags |= \ast\flags\CLASS_ABSTRACT;
     }
     $context = new Context();
     $class_fqsen = FullyQualifiedClassName::fromStringInContext($class->getName(), $context);
     // Build a base class element
     $clazz = new Clazz($context, $class->getName(), UnionType::fromStringInContext($class->getName(), $context), $flags, $class_fqsen);
     // If this class has a parent class, add it to the
     // class info
     if ($parent_class = $class->getParentClass()) {
         $parent_class_fqsen = FullyQualifiedClassName::fromFullyQualifiedString('\\' . $parent_class->getName());
         $parent_type = $parent_class_fqsen->asType();
         $clazz->setParentType($parent_type);
     }
     // n.b.: public properties on internal classes don't get
     //       listed via reflection until they're set unless
     //       they have a default value. Therefore, we don't
     //       bother iterating over `$class->getProperties()`
     //       `$class->getStaticProperties()`.
     foreach ($class->getDefaultProperties() as $name => $value) {
         $property_context = $context->withScope(new ClassScope(new GlobalScope(), $clazz->getFQSEN()));
         $property_fqsen = FullyQualifiedPropertyName::make($clazz->getFQSEN(), $name);
         $property = new Property($property_context, $name, Type::fromObject($value)->asUnionType(), 0, $property_fqsen);
         $clazz->addProperty($code_base, $property, new None());
     }
     foreach (UnionType::internalPropertyMapForClassName($clazz->getName()) as $property_name => $property_type_string) {
         $property_context = $context->withScope(new ClassScope(new GlobalScope(), $clazz->getFQSEN()));
         $property_type = UnionType::fromStringInContext($property_type_string, new Context());
         $property_fqsen = FullyQualifiedPropertyName::make($clazz->getFQSEN(), $property_name);
         $property = new Property($property_context, $property_name, $property_type, 0, $property_fqsen);
         $clazz->addProperty($code_base, $property, new None());
     }
     foreach ($class->getInterfaceNames() as $name) {
         $clazz->addInterfaceClassFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getTraitNames() as $name) {
         $clazz->addTraitFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getConstants() as $name => $value) {
         $constant_fqsen = FullyQualifiedClassConstantName::make($clazz->getFQSEN(), $name);
         $constant = new ClassConstant($context, $name, Type::fromObject($value)->asUnionType(), 0, $constant_fqsen);
         $clazz->addConstant($code_base, $constant);
     }
     foreach ($class->getMethods() as $reflection_method) {
         $method_context = $context->withScope(new ClassScope(new GlobalScope(), $clazz->getFQSEN()));
         $method_list = FunctionFactory::methodListFromReflectionClassAndMethod($method_context, $code_base, $class, $reflection_method);
         foreach ($method_list as $method) {
             $clazz->addMethod($code_base, $method, new None());
         }
     }
     return $clazz;
 }