IPReflectionCommentParser.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Class for parsing the comment blocks for classes, functions
  4. * methods and properties.
  5. *
  6. * The class parses the commentblock and extracts certain
  7. * documentation tags and the (full/small) description
  8. *
  9. * @author David Kingma
  10. * @version 0.1
  11. */
  12. class IPReflectionCommentParser{
  13. /**
  14. * @var string Contains the full commen text
  15. */
  16. public $comment;
  17. /**
  18. * @var object refence to the IPReflection(Class|Method|Property)
  19. */
  20. public $obj;
  21. /** @var boolean */
  22. public $smallDescriptionDone;
  23. /** @var boolean */
  24. public $fullDescriptionDone;
  25. /**
  26. * Constructor, initiateds the parse function
  27. *
  28. * @param string Commentaar block
  29. * @param string Defines if its the comment for a class, public of function
  30. */
  31. function __construct($comment, $obj) {
  32. $this->comment = $comment;
  33. $this->obj = $obj;
  34. $this->parse();
  35. }
  36. /**
  37. * parses the comment, line for line
  38. *
  39. * Will take the type of comment (class, property or function) as an
  40. * argument and split it up in lines.
  41. * @param string Defines if its the comment for a class, public of function
  42. * @return void
  43. */
  44. function parse() {
  45. //reset object
  46. $descriptionDone = false;
  47. $this->fullDescriptionDone = false;
  48. //split lines
  49. $lines = explode("\n", $this->comment);
  50. //check lines for description or tags
  51. foreach ($lines as $line) {
  52. $pos = strpos($line,"* @");
  53. if (trim($line) == "/**" || trim($line) == "*/") { //skip the start and end line
  54. }elseif (!($pos === false)) { //comment
  55. $this->parseTagLine(substr($line,$pos+3));
  56. $descriptionDone=true;
  57. }elseif(!$descriptionDone){
  58. $this->parseDescription($line);
  59. }
  60. }
  61. //if full description is empty, put small description in full description
  62. if (trim(str_replace(Array("\n","\r"), Array("", ""), $this->obj->fullDescription)) == "")
  63. $this->obj->fullDescription = $this->obj->smallDescription;
  64. }
  65. /**
  66. * Parses the description to the small and full description properties
  67. *
  68. * @param string The description line
  69. * @return void
  70. */
  71. function parseDescription($descriptionLine) {
  72. if(strpos($descriptionLine,"*") <= 2) $descriptionLine = substr($descriptionLine, (strpos($descriptionLine,"*") + 1));
  73. //geen lege comment regel indien al in grote omschrijving
  74. if(trim(str_replace(Array("\n","\r"), Array("", ""), $descriptionLine)) == ""){
  75. if($this->obj->fullDescription == "")
  76. $descriptionLine = "";
  77. $this->smallDescriptionDone = true;
  78. }
  79. if(!$this->smallDescriptionDone)//add to small description
  80. $this->obj->smallDescription.=$descriptionLine;
  81. else{//add to full description
  82. $this->obj->fullDescription.=$descriptionLine;
  83. }
  84. }
  85. /**
  86. * Parses a tag line and extracts the tagname and values
  87. *
  88. * @param string The tagline
  89. * @return void
  90. */
  91. function parseTagLine($tagLine) {
  92. $tagArr = explode(" ", $tagLine);
  93. $tag = $tagArr[0];
  94. switch(strtolower($tag)){
  95. case 'abstract':
  96. $this->obj->abstract = true; break;
  97. case 'access':
  98. $this->obj->isPrivate = (strtolower(trim($tagArr[1]))=="private")?true:false;
  99. break;
  100. case 'author':
  101. unset($tagArr[0]);
  102. $this->obj->author = implode(" ",$tagArr);
  103. break;
  104. case 'copyright':
  105. unset($tagArr[0]);
  106. $this->obj->copyright = implode(" ",$tagArr);
  107. break;
  108. case 'deprecated':
  109. case 'deprec':
  110. $this->obj->deprecated = true;
  111. break;
  112. case 'extends': break;
  113. case 'global':
  114. $this->obj->globals[] = $tagArr[1];
  115. break;
  116. case 'param':
  117. $o = new stdClass();
  118. $o->type = trim($tagArr[1]);
  119. $o->comment = implode(" ",$tagArr);
  120. $this->obj->params[] = $o;
  121. break;
  122. case 'return':
  123. $this->obj->return = trim($tagArr[1]); break;
  124. case 'link':break;
  125. case 'see':break;
  126. case 'since':
  127. $this->obj->since = trim($tagArr[1]); break;
  128. case 'static':
  129. $this->obj->static = true; break;
  130. case 'throws':
  131. unset($tagArr[0]);
  132. $this->obj->throws = implode(" ",$tagArr);
  133. break;
  134. case 'todo':
  135. unset($tagArr[0]);
  136. $this->obj->todo[] = implode(" ",$tagArr);
  137. break;
  138. case 'var':
  139. $this->obj->type = trim($tagArr[1]);
  140. unset($tagArr[0],$tagArr[1]);
  141. $comment=implode(" ",$tagArr);
  142. //check if its an optional property
  143. $this->obj->optional = strpos($comment,"[OPTIONAL]") !== FALSE;
  144. $this->obj->autoincrement = strpos($comment,"[AUTOINCREMENT]") !== FALSE;
  145. $this->obj->description = str_replace("[OPTIONAL]", "", $comment);
  146. break;
  147. case 'version':
  148. $this->obj->version = $tagArr[1];
  149. break;
  150. default:
  151. //echo "\nno valid tag: '".strtolower($tag)."' at tagline: '$tagLine' <br>";
  152. //do nothing
  153. }
  154. }
  155. }
  156. ?>