/** * @return array * Get a map from column name to row values for * this instance */ public function toRow() : array { $parent_class_fqsen = $this->clazz->hasParentClassFQSEN() ? (string) $this->clazz->getParentClassFQSEN() : null; $interface_fqsen_list_string = implode('|', array_map(function (FullyQualifiedClassName $fqsen) { return (string) $fqsen; }, $this->clazz->getInterfaceFQSENList())); $trait_fqsen_list_string = implode('|', array_map(function (FullyQualifiedClassName $fqsen) { return (string) $fqsen; }, $this->clazz->getInterfaceFQSENList())); return ['name' => (string) $this->clazz->getName(), 'type' => (string) $this->clazz->getUnionType(), 'flags' => $this->clazz->getFlags(), 'fqsen' => (string) $this->clazz->getFQSEN(), 'context' => base64_encode(serialize($this->clazz->getContext())), 'is_deprecated' => $this->clazz->isDeprecated(), 'parent_class_fqsen' => $parent_class_fqsen, 'interface_fqsen_list' => $interface_fqsen_list_string, 'trait_fqsen_list' => $trait_fqsen_list_string, 'is_parent_constructor_called' => $this->clazz->getIsParentConstructorCalled()]; }
/** * Check to see if the given Clazz is a duplicate * * @return null */ public static function analyzeParentConstructorCalled(CodeBase $code_base, Clazz $clazz) { // Only look at classes configured to require a call // to its parent constructor if (!in_array($clazz->getName(), Config::get()->parent_constructor_required)) { return; } // Don't worry about internal classes if ($clazz->isInternal()) { return; } // Don't worry if there's no parent class if (!$clazz->hasParentClassFQSEN()) { return; } if (!$code_base->hasClassWithFQSEN($clazz->getParentClassFQSEN())) { // This is an error, but its caught elsewhere. We'll // just roll through looking for other errors return; } $parent_clazz = $code_base->getClassByFQSEN($clazz->getParentClassFQSEN()); if (!$parent_clazz->isAbstract() && !$clazz->getIsParentConstructorCalled()) { Issue::emit(Issue::TypeParentConstructorCalled, $clazz->getContext()->getFile(), $clazz->getContext()->getLineNumberStart(), (string) $clazz->getFQSEN(), (string) $parent_clazz->getFQSEN()); } }
/** * @param CodeBase $code_base * The code base in which the class exists * * @param Clazz $class * A class being analyzed * * @return void */ public function analyzeClass(CodeBase $code_base, Clazz $class) { // As an example, we test to see if the name of // the class is `Class`, and emit an issue explain that // the name is not allowed. if ($class->getName() == 'Class') { $this->emitIssue($code_base, $class->getContext(), 'DemoPluginClassName', "Class {$class->getFQSEN()} cannot be called `Class`"); } }
/** * @return Method * A default constructor for the given class */ public static function defaultConstructorForClassInContext(Clazz $clazz, Context $context, CodeBase $code_base) : Method { $method_fqsen = FullyQualifiedMethodName::make($clazz->getFQSEN(), '__construct'); $method = new Method($context, '__construct', $clazz->getUnionType(), 0, $method_fqsen); if ($clazz->hasMethodWithName($code_base, $clazz->getName())) { $old_style_constructor = $clazz->getMethodByName($code_base, $clazz->getName()); $method->setParameterList($old_style_constructor->getParameterList()); $method->setNumberOfRequiredParameters($old_style_constructor->getNumberOfRequiredParameters()); $method->setNumberOfOptionalParameters($old_style_constructor->getNumberOfOptionalParameters()); } return $method; }
/** * @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; }