/** * Tests the parseSubElements method * * @covers \phpDocumentor\Reflection\ClassReflector::isFinal * * @return void */ public function testIsFinal() { $node = new NodeStmtMock2(); $class_reflector = new ClassReflector($node, new Context()); $this->assertFalse($class_reflector->isFinal()); $node->type = Class_::MODIFIER_FINAL; $this->assertTrue($class_reflector->isFinal()); }
/** * Exports the given reflection object to the parent XML element. * * This method creates a new child element on the given parent XML element * and takes the properties of the Reflection argument and sets the * elements and attributes on the child. * * If a child DOMElement is provided then the properties and attributes are * set on this but the child element is not appended onto the parent. This * is the responsibility of the invoker. Essentially this means that the * $parent argument is ignored in this case. * * @param \DOMElement $parent The parent element to augment. * @param ClassReflector $class The data source. * @param \DOMElement $child Optional: child element to use instead of * creating a new one on the $parent. * * @return void */ public function export(\DOMElement $parent, $class, \DOMElement $child = null) { if (!$child) { $child = new \DOMElement('class'); $parent->appendChild($child); } $child->setAttribute('final', $class->isFinal() ? 'true' : 'false'); $child->setAttribute('abstract', $class->isAbstract() ? 'true' : 'false'); $child->appendChild(new \DOMElement('extends', $class->getParentClass())); $interfaces = method_exists($class, 'getInterfaces') ? $class->getInterfaces() : $class->getParentInterfaces(); foreach ($interfaces as $interface) { $child->appendChild(new \DOMElement('implements', $interface)); } $object = new InterfaceExporter(); $object->export($child, $class, $child); }
/** * Resolve a class name from self/parent. * * @param string $class The class name. * * @return string The resolved class name. */ protected function _resolveName($class) { if (!$this->called_in_class) { return $class; } switch ($class) { case '$this': case 'self': $class = $this->called_in_class->getShortName(); break; case 'parent': $class = $this->called_in_class->getNode()->extends->toString(); break; } return $class; }
/** * Gets the normalized class name (without trailing backslash). * * @param ClassReflector $classReflector * * @return string */ private function getClassName(ClassReflector $classReflector) { $className = $classReflector->getName(); if ('\\' === $className[0]) { return substr($className, 1); } return $className; }
/** * Creates a Descriptor from the provided data. * * @param ClassReflector $data * * @return ClassDescriptor */ public function create($data) { $classDescriptor = new ClassDescriptor(); $classDescriptor->setFullyQualifiedStructuralElementName($data->getName()); $classDescriptor->setName($data->getShortName()); $classDescriptor->setPackage($this->extractPackageFromDocBlock($data->getDocBlock()) ?: ''); $classDescriptor->setLine($data->getLinenumber()); $classDescriptor->setParent($data->getParentClass()); $classDescriptor->setAbstract($data->isAbstract()); $classDescriptor->setFinal($data->isFinal()); // Reflection library formulates namespace as global but this is not wanted for phpDocumentor itself $classDescriptor->setNamespace('\\' . (strtolower($data->getNamespace()) == 'global' ? '' : $data->getNamespace())); foreach ($data->getInterfaces() as $interfaceClassName) { $classDescriptor->getInterfaces()->set($interfaceClassName, $interfaceClassName); } $fqcn = $classDescriptor->getFullyQualifiedStructuralElementName(); $namespace = substr($fqcn, 0, strrpos($fqcn, '\\')); $classDescriptor->setNamespace($namespace); $this->assembleDocBlock($data->getDocBlock(), $classDescriptor); $this->addConstants($data->getConstants(), $classDescriptor); $this->addProperties($data->getProperties(), $classDescriptor); $this->addMethods($data->getMethods(), $classDescriptor); $this->addUses($data->getTraits(), $classDescriptor); return $classDescriptor; }
public function enterNode(PHPParser_Node $node) { $prettyPrinter = new PHPParser_PrettyPrinter_Zend(); switch (get_class($node)) { case 'PHPParser_Node_Stmt_Use': /** @var PHPParser_Node_Stmt_UseUse $use */ foreach ($node->uses as $use) { $this->context->setNamespaceAlias($use->alias, implode('\\', $use->name->parts)); } break; case 'PHPParser_Node_Stmt_Namespace': $this->context->setNamespace(implode('\\', $node->name->parts)); break; case 'PHPParser_Node_Stmt_Class': $class = new ClassReflector($node, $this->context); $class->parseSubElements(); $this->classes[] = $class; break; case 'PHPParser_Node_Stmt_Trait': $trait = new TraitReflector($node, $this->context); $this->traits[] = $trait; break; case 'PHPParser_Node_Stmt_Interface': $interface = new InterfaceReflector($node, $this->context); $interface->parseSubElements(); $this->interfaces[] = $interface; break; case 'PHPParser_Node_Stmt_Function': $function = new FunctionReflector($node, $this->context); $this->functions[] = $function; break; case 'PHPParser_Node_Stmt_Const': foreach ($node->consts as $constant) { $reflector = new ConstantReflector($node, $this->context, $constant); $this->constants[] = $reflector; } break; case 'PHPParser_Node_Expr_FuncCall': if ($node->name instanceof PHPParser_Node_Name && $node->name == 'define') { $name = trim($prettyPrinter->prettyPrintExpr($node->args[0]->value), '\''); $constant = new PHPParser_Node_Const($name, $node->args[1]->value, $node->getAttributes()); $constant->namespacedName = new PHPParser_Node_Name(($this->current_namespace ? $this->current_namespace . '\\' : '') . $name); $reflector = new ConstantReflector(new \PHPParser_Node_Stmt_Const(array($constant)), $this->context, $constant); $this->constants[] = $reflector; } break; case 'PHPParser_Node_Expr_Include': $include = new IncludeReflector($node, $this->context); $this->includes[] = $include; break; } }
public function leaveNode(PHPParser_Node $node) { $prettyPrinter = new PrettyPrinter(); switch (get_class($node)) { case 'PHPParser_Node_Stmt_Use': /** @var PHPParser_Node_Stmt_UseUse $use */ foreach ($node->uses as $use) { $this->context->setNamespaceAlias($use->alias, implode('\\', $use->name->parts)); } break; case 'PHPParser_Node_Stmt_Namespace': $this->context->setNamespace(isset($node->name) && $node->name ? implode('\\', $node->name->parts) : ''); break; case 'PHPParser_Node_Stmt_Class': $class = new ClassReflector($node, $this->context); $class->parseSubElements(); $this->classes[] = $class; break; case 'PHPParser_Node_Stmt_Trait': $trait = new TraitReflector($node, $this->context); $trait->parseSubElements(); $this->traits[] = $trait; break; case 'PHPParser_Node_Stmt_Interface': $interface = new InterfaceReflector($node, $this->context); $interface->parseSubElements(); $this->interfaces[] = $interface; break; case 'PHPParser_Node_Stmt_Function': $function = new FunctionReflector($node, $this->context); $this->functions[] = $function; break; case 'PHPParser_Node_Stmt_Const': foreach ($node->consts as $constant) { $reflector = new ConstantReflector($node, $this->context, $constant); $this->constants[] = $reflector; } break; case 'PHPParser_Node_Expr_FuncCall': if ($node->name instanceof PHPParser_Node_Name && $node->name == 'define' && isset($node->args[0]) && isset($node->args[1])) { // transform the first argument of the define function call into a constant name $name = str_replace(array('\\\\', '"', "'"), array('\\', '', ''), trim($prettyPrinter->prettyPrintExpr($node->args[0]->value), '\'')); $nameParts = explode('\\', $name); $shortName = end($nameParts); $constant = new PHPParser_Node_Const($shortName, $node->args[1]->value, $node->getAttributes()); $constant->namespacedName = new PHPParser_Node_Name($name); $constant_statement = new \PHPParser_Node_Stmt_Const(array($constant)); $constant_statement->setAttribute('comments', array($node->getDocComment())); $this->constants[] = new ConstantReflector($constant_statement, $this->context, $constant); } break; case 'PHPParser_Node_Expr_Include': $include = new IncludeReflector($node, $this->context); $this->includes[] = $include; break; } }