/** * Visit a node with kind `\ast\AST_CLASS_CONST_DECL` * * @param Node $node * A node to parse * * @return Context * A new or an unchanged context resulting from * parsing the node */ public function visitClassConstDecl(Node $node) : Context { $clazz = $this->getContextClass(); foreach ($node->children ?? [] as $child_node) { $name = $child_node->children['name']; $fqsen = FullyQualifiedClassConstantName::fromStringInContext($name, $this->context); $constant = new ClassConstant($this->context->withLineNumberStart($child_node->lineno ?? 0)->withLineNumberEnd($child_node->endLineno ?? 0), $name, new UnionType(), $child_node->flags ?? 0); $constant->setFQSEN($fqsen); $constant->setFutureUnionType(new FutureUnionType($this->code_base, $this->context, $child_node->children['value'])); $clazz->addConstant($this->code_base, $constant); } return $this->context; }
/** * Add a class constant * * @return null; */ public function addConstant(CodeBase $code_base, ClassConstant $constant) { $constant_fqsen = FullyQualifiedClassConstantName::make($this->getFQSEN(), $constant->getName()); // Update the FQSEN if its not associated with this // class yet if ($constant->getFQSEN() !== $constant_fqsen) { $constant = clone $constant; $constant->setFQSEN($constant_fqsen); } $code_base->addClassConstant($constant); }
/** * @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(); // Build a base class element $clazz = new Clazz($context, $class->getName(), UnionType::fromStringInContext($class->getName(), $context), $flags); $clazz->setFQSEN(FullyQualifiedClassName::fromStringInContext($class->getName(), $context)); // 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_context = $context->withClassFQSEN($clazz->getFQSEN()); $property = new Property($property_context, $name, Type::fromObject($value)->asUnionType(), 0); $property->setFQSEN(FullyQualifiedPropertyName::make($clazz->getFQSEN(), $name)); $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) { $constant = new ClassConstant($context, $name, Type::fromObject($value)->asUnionType(), 0); $constant->setFQSEN(FullyQualifiedClassConstantName::make($clazz->getFQSEN(), $name)); $clazz->addConstant($code_base, $constant); } foreach ($class->getMethods() as $reflection_method) { $method_list = FunctionFactory::methodListFromReflectionClassAndMethod($context->withClassFQSEN($clazz->getFQSEN()), $code_base, $class, $reflection_method); foreach ($method_list as $method) { $clazz->addMethod($code_base, $method); } } return $clazz; }
/** * @return array * Get a map from column name to row values for * this instance */ public function toRow() : array { $row = ['scope_name' => $this->primaryKeyValue(), 'fqsen' => (string) $this->constant->getFQSEN(), 'name' => (string) $this->constant->getName(), 'type' => (string) $this->constant->getUnionType(), 'flags' => $this->constant->getFlags(), 'context' => base64_encode(serialize($this->constant->getContext())), 'is_deprecated' => $this->constant->isDeprecated()]; return $row; }
/** * @return void */ public function addClassConstant(ClassConstant $constant) { $this->class_constant_map[$constant->getFQSEN()->getNameWithAlternateId()] = $constant; }
/** * @param ClassConstant $constant * Any constant * * @param FullyQualifiedClassName $fqsen * The FQSEN to index the constant by * * @return null */ public function addConstantInScope(ClassConstant $constant, FullyQualifiedClassName $fqsen) { $name = $constant->getFQSEN()->getNameWithAlternateId(); $this->constant_map[(string) $fqsen][$name] = $constant; }