Phan\Language\Element\Clazz::addProperty PHP Method

addProperty() public method

Add a property to this class
public addProperty ( CodeBase $code_base, Property $property, Option | Phan\Library\None $type_option ) : void
$code_base Phan\CodeBase A reference to the code base in which the ancestor exists
$property Property The property to copy onto this class
$type_option Option | Phan\Library\None
return void
    public function addProperty(CodeBase $code_base, Property $property, $type_option)
    {
        // Ignore properties we already have
        if ($this->hasPropertyWithName($code_base, $property->getName())) {
            return;
        }
        $property_fqsen = FullyQualifiedPropertyName::make($this->getFQSEN(), $property->getName());
        if ($property->getFQSEN() !== $property_fqsen) {
            $property = clone $property;
            $property->setDefiningFQSEN($property->getFQSEN());
            $property->setFQSEN($property_fqsen);
            try {
                // If we have a parent type defined, map the property's
                // type through it
                if ($type_option->isDefined() && $property->getUnionType()->hasTemplateType()) {
                    $property->setUnionType($property->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base)));
                }
            } catch (IssueException $exception) {
                Issue::maybeEmitInstance($code_base, $property->getContext(), $exception->getIssueInstance());
            }
        }
        $code_base->addProperty($property);
    }

Usage Example

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;
 }
All Usage Examples Of Phan\Language\Element\Clazz::addProperty