Pelzini

This is the code documentation for the Pelzini project

source of /processor/parser_function.php

Contains the ParserFunction class
  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. /**
  22.  * Contains the {@link ParserFunction} class
  23.  *
  24.  * @package Parser model
  25.  * @author Josh Heidenreich
  26.  * @since 0.1
  27.  **/
  28.  
  29. /**
  30.  * Represents a function
  31.  **/
  32. class ParserFunction extends CodeParserItem {
  33. public $name;
  34. public $args;
  35. public $throws;
  36. public $visibility;
  37. public $abstract;
  38. public $description;
  39. public $returns;
  40. public $static;
  41. public $final;
  42. public $has_return_stmt;
  43.  
  44.  
  45. public function __construct()
  46. {
  47. parent::__construct();
  48.  
  49. $this->args = array();
  50. $this->throws = array();
  51. $this->returns = array();
  52. $this->visibility = 'public';
  53. $this->static = false;
  54. $this->final = false;
  55. $this->has_return_stmt = false;
  56. }
  57.  
  58.  
  59. /**
  60.   * Processes Javadoc tags that are specific to this PaserItem
  61.   **/
  62. protected function processSpecificDocblockTags($docblock_tags)
  63. {
  64. $this->description = htmlify_text(@$docblock_tags['@summary']);
  65.  
  66. if (count($this->returns) == 0 and !$this->has_return_stmt) {
  67. $void_return = new ParserReturn();
  68. $void_return->type = 'void';
  69. $this->returns[] = $void_return;
  70. }
  71. }
  72.  
  73.  
  74. /**
  75.   * Does post-pasing processing of this ParserFunction.
  76.   * Specifically, loads types for the function arguments
  77.   **/
  78. public function post_load()
  79. {
  80. $args = array();
  81. foreach ($this->args as $arg) {
  82. $args[$arg->name] = $arg;
  83. }
  84.  
  85. // Do arguments
  86. $params = @$this->docblock_tags['@param'];
  87. if ($params != null) {
  88. foreach ($params as $idx => $param_tag) {
  89. $parts = preg_split('/\s+/', $param_tag, 3);
  90.  
  91. if (isset($args[$parts[0]])) {
  92. // name type desc
  93. $arg = $args[$parts[0]];
  94. if (!$arg->type) $arg->type = $parts[1];
  95. unset($parts[0], $parts[1]);
  96.  
  97. } else if (isset($args[$parts[1]])) {
  98. // type name desc
  99. $arg = $args[$parts[1]];
  100. if (!$arg->type) $arg->type = $parts[0];
  101. unset($parts[0], $parts[1]);
  102.  
  103. } else {
  104. // type desc
  105. $arg = @$this->args[$idx];
  106. if (!$arg) continue;
  107. if (!$arg->type) $arg->type = $parts[0];
  108. unset($parts[0]);
  109. }
  110.  
  111. $arg->description = htmlify_text(implode(' ', $parts));
  112. }
  113. }
  114.  
  115. // Combine @throw and @throws docblock tags
  116. $throws = array();
  117. if (isset($this->docblock_tags['@throw'])) {
  118. $throws = $this->docblock_tags['@throw'];
  119. }
  120. if (isset($this->docblock_tags['@throws'])) {
  121. $throws = array_merge($throws, $this->docblock_tags['@throws']);
  122. }
  123.  
  124. // Process throws
  125. foreach ($throws as $throws_tag) {
  126. if ($throws_tag == '') $throws_tag = 'Exception';
  127. $parts = preg_split('/\s+/', $throws_tag, 2);
  128. $throw = new ParserThrow();
  129. $throw->exception = $parts[0];
  130. $throw->description = htmlify_text(@$parts[1]);
  131. $this->throws[] = $throw;
  132. }
  133.  
  134. // Do return value
  135. if (isset($this->docblock_tags['@return'])) {
  136. $this->returns = array();
  137. foreach ($this->docblock_tags['@return'] as $return_tag) {
  138. $parts = preg_split('/\s+/', $return_tag, 2);
  139. $types = explode('|', $parts[0]);
  140. foreach ($types as $t) {
  141. $return = new ParserReturn();
  142. $return->type = trim($t);
  143. $return->description = htmlify_text(@$parts[1]);
  144. $this->returns[] = $return;
  145. }
  146. }
  147. }
  148.  
  149. // If there is only one return tag, and the type ends in ?
  150. // Nuke the ? and create a new return tag called "null"
  151. if (count($this->returns) == 1) {
  152. if (substr($this->returns[0]->type, -1) == '?') {
  153. $this->returns[0]->type = substr($this->returns[0]->type, 0, -1);
  154. $null_return = new ParserReturn();
  155. $null_return->type = 'null';
  156. $this->returns[] = $null_return;
  157. }
  158. }
  159. }
  160.  
  161.  
  162. /**
  163.   * Debugging use only
  164.   **/
  165. public function dump()
  166. {
  167. echo '<div style="border: 1px red solid;">';
  168. echo $this->visibility . ' ';
  169. echo $this->name;
  170. if ($this->abstract) echo '<br>abstract';
  171. if ($this->static) echo '<br>static';
  172. if ($this->final) echo '<br>static';
  173. echo '<br>' . $this->description;
  174.  
  175. foreach ($this->args as $a) $a->dump();
  176. foreach ($this->throws as $t) $t->dump();
  177. foreach ($this->returns as $r) $r->dump();
  178.  
  179. parent::dump();
  180. echo '</div>';
  181. }
  182.  
  183.  
  184. }
  185.  
  186.  
  187. ?>
  188.