PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/main/php/text/doclet/ClassDoc.class.php

https://github.com/treuter/xp-framework
PHP | 215 lines | 92 code | 24 blank | 99 comment | 11 complexity | dd4a2e441e1973c754dafb2633132497 MD5 | raw file
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses('text.doclet.AnnotatedDoc', 'text.doclet.ClassIterator');
  7. define('EXCEPTION_CLASS', 'exception');
  8. define('ERROR_CLASS', 'error');
  9. define('INTERFACE_CLASS', 'interface');
  10. define('ORDINARY_CLASS', 'class');
  11. define('ENUM_CLASS', 'enum');
  12. /**
  13. * Represents an XP class or interface and provides access to
  14. * information about it, its comment and tags, and members.
  15. *
  16. * @test xp://net.xp_framework.unittest.text.doclet.ClassDocTest
  17. * @purpose Documents a class
  18. */
  19. class ClassDoc extends AnnotatedDoc {
  20. public
  21. $fields = array(),
  22. $methods = array(),
  23. $constants = array(),
  24. $interfaces = NULL,
  25. $usedClasses = NULL,
  26. $superclass = NULL,
  27. $type = NULL,
  28. $qualifiedName = '',
  29. $modifiers = array();
  30. protected
  31. $loader = NULL;
  32. /**
  33. * Constructor
  34. *
  35. */
  36. public function __construct() {
  37. $this->interfaces= new ClassIterator();
  38. $this->usedClasses= new ClassIterator();
  39. }
  40. /**
  41. * Set rootdoc
  42. *
  43. * @param text.doclet.RootDoc root
  44. */
  45. public function setRoot($root) {
  46. parent::setRoot($root);
  47. $this->interfaces->root= $root;
  48. $this->usedClasses->root= $root;
  49. }
  50. /**
  51. * Set class loader this classdoc was loaded from
  52. *
  53. * @param lang.IClassLoader loader
  54. */
  55. public function setLoader($loader) {
  56. $this->loader= $loader;
  57. }
  58. /**
  59. * Returns the source file name this doc was parsed from.
  60. *
  61. * @return io.File
  62. */
  63. public function sourceFile() {
  64. return $this->loader
  65. ? $this->loader->getResourceAsStream(strtr($this->qualifiedName, '.', '/').xp::CLASS_FILE_EXT)
  66. : NULL
  67. ;
  68. }
  69. /**
  70. * Retrieve class type, which one of the following constants
  71. *
  72. * <ul>
  73. * <li>EXCEPTION_CLASS</li>
  74. * <li>ERROR_CLASS</li>
  75. * <li>INTERFACE_CLASS</li>
  76. * <li>ORDINARY_CLASS</li>
  77. * </ul>
  78. *
  79. * @return string
  80. */
  81. public function classType() {
  82. static $map= array(
  83. 'lang.XPException' => EXCEPTION_CLASS,
  84. 'lang.Error' => ERROR_CLASS,
  85. 'lang.Enum' => ENUM_CLASS
  86. );
  87. if ($this->type) return $this->type; // Already known
  88. $cmp= $this;
  89. do {
  90. if (isset($map[$cmp->qualifiedName])) {
  91. return $this->type= $map[$cmp->qualifiedName];
  92. }
  93. } while ($cmp= $cmp->superclass);
  94. return $this->type= ORDINARY_CLASS;
  95. }
  96. /**
  97. * Returns whether this class is an exception class.
  98. *
  99. * @return bool
  100. */
  101. public function isException() {
  102. return EXCEPTION_CLASS == $this->classType();
  103. }
  104. /**
  105. * Returns whether this class is an error class.
  106. *
  107. * @return bool
  108. */
  109. public function isError() {
  110. return ERROR_CLASS == $this->classType();
  111. }
  112. /**
  113. * Returns whether this class is an interface.
  114. *
  115. * @return bool
  116. */
  117. public function isInterface() {
  118. return INTERFACE_CLASS == $this->classType();
  119. }
  120. /**
  121. * Returns whether this class is an interface.
  122. *
  123. * @return bool
  124. */
  125. public function isEnum() {
  126. return ENUM_CLASS == $this->classType();
  127. }
  128. /**
  129. * Returns whether this class is an ordinary class
  130. *
  131. * @return bool
  132. */
  133. public function isOrdinaryClass() {
  134. return ORDINARY_CLASS == $this->classType();
  135. }
  136. /**
  137. * Returns whether this class is a subclass of a given class.
  138. *
  139. * @return bool
  140. */
  141. public function subclassOf($classdoc) {
  142. $cmp= $this;
  143. do {
  144. if ($cmp->qualifiedName == $classdoc->qualifiedName) return TRUE;
  145. } while ($cmp= $cmp->superclass);
  146. return FALSE;
  147. }
  148. /**
  149. * Get the fully qualified name of this program element. For example,
  150. * for the class util.Date, return "util.Date".
  151. *
  152. * @return string
  153. */
  154. public function qualifiedName() {
  155. return $this->qualifiedName;
  156. }
  157. /**
  158. * Returns the package this class is contained in
  159. *
  160. * @return text.doclet.PackageDoc
  161. */
  162. public function containingPackage() {
  163. return $this->root->packageNamed(substr($this->qualifiedName, 0, strrpos($this->qualifiedName, '.')));
  164. }
  165. /**
  166. * Returns a string representation of this object
  167. *
  168. * @return string
  169. */
  170. public function toString() {
  171. return $this->getClassName().'<'.$this->classType().' '.$this->qualifiedName.'>';
  172. }
  173. /**
  174. * Returns modifiers as a hashmap (modifier names as keys for easy
  175. * O(1) lookup).
  176. *
  177. * @return [:bool]
  178. */
  179. public function getModifiers() {
  180. return $this->modifiers;
  181. }
  182. /**
  183. * Returns a hashcode for this object
  184. *
  185. * @return string
  186. */
  187. public function hashCode() {
  188. return $this->getClassName().$this->qualifiedName;
  189. }
  190. }
  191. ?>