home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 August / PCWorld_2001-08_cd.bin / Komunikace / phptriad / phptriadsetup2-11.exe / php / pear / PHPDoc / parser / PhpdocFunctionParser.php < prev    next >
PHP Script  |  2001-02-18  |  5KB  |  139 lines

  1. <?php
  2. /**
  3. * Looks for documented and undocumented functions within a block of php code.
  4. *
  5. * @version $Id: PhpdocFunctionParser.php,v 1.3 2001/02/18 14:45:27 uw Exp $
  6. */
  7. class PhpdocFunctionParser extends PhpdocVariableParser {
  8.  
  9.     /**
  10.     * Internal structur of a function.
  11.     *
  12.     * @var    array    $emptyFunction
  13.     */
  14.     var $emptyFunction = array(
  15.                                 "name"  => "",
  16.                                 "undoc" => true,
  17.                                 
  18.                                 "args"  => array()
  19.                               );
  20.         
  21.     /**
  22.     * Array of tags that are allowed in front of the function keyword
  23.     *
  24.     * @var    array    $functionTags
  25.     * @see    analyseFunctionParagraph()
  26.     */
  27.     var $functionTags = array(
  28.                                 "parameter"     => true,
  29.                                 "param"         => true,
  30.                                 
  31.                                 "return"        => true,
  32.                                 
  33.                                 "access"        => true,
  34.                                 "abstract"      => true,
  35.                                 "static"        => true,
  36.                                 
  37.                                 "throws"        => true,
  38.                                 
  39.                                 "see"           => true,
  40.                                 "link"          => true,
  41.                                 
  42.                                 "global"        => true,
  43.                                 
  44.                                 "version"       => true,
  45.                                 "since"         => true,
  46.                                 
  47.                                 "deprecated"    => true,
  48.                                 "deprec"        => true,
  49.                                 
  50.                                 "brother"       => true,
  51.                                 "sister"        => true,
  52.                                 
  53.                                 "exclude"       => true,
  54.                                 "magic"         => true,
  55.                                 
  56.                                 "author"        => true,
  57.                                 "copyright"     => true,
  58.                                 
  59.                                 "todo"          => true
  60.                            );
  61.     
  62.     /**
  63.     * Analyses a function doc comment.
  64.     * @param    array
  65.     * @return array
  66.     */
  67.     function analyseFunction($para) {
  68.     
  69.         $function = $this->emptyFunction;
  70.         $function["name"] = $para["name"];
  71.  
  72.         if ("" != $para["doc"]) {
  73.  
  74.             $function = $this->analyseTags($this->getTags($para["doc"]), $function, $this->functionTags);
  75.             
  76.             list($msg, $function) = $this->checkParserErrors($function, "function");
  77.             if (""!=$msg) 
  78.                 $this->warn->addDocWarning($this->currentFile, "function", $function["name"], $msg, "mismatch");
  79.             
  80.             list($function["sdesc"], $function["desc"]) = $this->getDescription($para["doc"]);
  81.             
  82.             $function["undoc"] = false;
  83.             
  84.         } 
  85.  
  86.         $function["args"] = $this->getFunctionArgs($para["head"]);            
  87.         return $function;
  88.     } // end func analyseFunction
  89.     
  90.     /**
  91.     * Analyses a function head and returns an array of arguments.
  92.     *
  93.     * @param    string  PHP code to examine.
  94.     * @return   array   Array of arguments: $args[] = array( optional, default, type, name ).
  95.     * @see      getVariableTypeAndValue()
  96.     */                
  97.     function getFunctionArgs($code) {
  98.  
  99.         $args = array();
  100.         while (preg_match($this->PHP_COMPLEX["argument"], $code, $regs)) {
  101.  
  102.             $type       = "";
  103.             $value      = "";
  104.             $optional   = false;
  105.         
  106.             if (!isset($regs[3])) {
  107.                 
  108.                 $len_of_value = strlen($regs[1]);
  109.                 
  110.             } else if ("=" == $regs[3]) {
  111.         
  112.                 $find     = $regs[1] . $regs[2];
  113.                 $code     = substr($code, strpos($code, $find) + strlen($find) );
  114.                 
  115.                 list ($type, $value, $raw_value) = $this->getVariableTypeAndValue($code);
  116.                 $len_of_value = strlen($raw_value);
  117.                 $optional = true;
  118.             
  119.             } else {
  120.                 
  121.                 $len_of_value = strlen($regs[1] . $regs[2]);
  122.                 
  123.             }
  124.             
  125.             $code = substr($code, $len_of_value);
  126.             $args[] = array(
  127.                             "optional"  => $optional, 
  128.                             "default"   => $value,
  129.                             "type"      => $type,
  130.                             "name"      => $regs[1]
  131.                            );
  132.  
  133.         }
  134.  
  135.         return $args;
  136.     } // end func getFunctionArgs
  137.     
  138. } // end class PhpdocFunctionParser
  139.