Example #1
0
 /**
  * Builds a PC_Obj_Method from the given row
  *
  * @param array $row the row from db
  * @return PC_Obj_Method the method
  */
 private function build_func($row)
 {
     $c = new PC_Obj_Method($row['file'], $row['line'], $row['class'] == 0, $row['id'], $row['class']);
     $c->set_name($row['name']);
     $c->set_visibility($row['visibility']);
     $c->set_abstract($row['abstract']);
     $c->set_static($row['static']);
     $c->set_anonymous($row['anonymous']);
     $c->set_final($row['final']);
     $c->get_version()->set(unserialize($row['min_version']), unserialize($row['max_version']));
     foreach (unserialize($row['params']) as $param) {
         $c->put_param($param);
     }
     list($hasretdoc, $rettype) = unserialize($row['return_type']);
     $throws = unserialize($row['throws']);
     if (is_array($throws)) {
         foreach ($throws as $class => $type) {
             $c->add_throw($class, $type);
         }
     }
     $c->set_has_return_doc($hasretdoc);
     $c->set_return_type($rettype);
     return $c;
 }
Example #2
0
 /**
  * Creates a method.
  *
  * @param string $name the name
  * @param array $modifiers the modifiers
  * @param array $params an array of PC_Obj_Parameter's
  * @param PC_Obj_MultiType $return the return type
  * @return PC_Obj_Method the method
  */
 public function create_method($name, $modifiers, $params, $return)
 {
     $m = new PC_Obj_Method($this->get_file(), $this->get_last_function_line(), false);
     $m->set_name($name);
     $m->set_static(in_array('static', $modifiers));
     $m->set_abstract(in_array('abstract', $modifiers));
     $m->set_final(in_array('final', $modifiers));
     if (in_array('private', $modifiers)) {
         $m->set_visibility(PC_Obj_Visible::V_PRIVATE);
     } else {
         if (in_array('protected', $modifiers)) {
             $m->set_visibility(PC_Obj_Visible::V_PROTECTED);
         } else {
             $m->set_visibility(PC_Obj_Visible::V_PUBLIC);
         }
     }
     foreach ($params as $param) {
         $m->put_param($param);
     }
     if ($return !== null) {
         $m->set_return_type($return);
     }
     $this->parse_method_doc($m);
     return $m;
 }
Example #3
0
 /**
  * Parses a method-description into a PC_Obj_Method
  * 
  * @param string $file the filename
  * @param string $desc the description
  * @return array an array of the class-name and the PC_Obj_Method
  * @throws PC_PHPRef_Exception if it failed
  */
 public static function parse_method_desc($file, $desc)
 {
     $classname = '';
     // find link to method
     if (preg_match('/<a href="(.*?)" class="methodname">/', $desc, $m)) {
         $file = dirname($file) . '/' . $m[1];
     }
     // prepare description
     $desc = trim(strip_tags($desc));
     $desc = FWS_StringHelper::htmlspecialchars_back($desc);
     // filter out modifier, return-type, name and params
     $match = array();
     $res = preg_match('/^(?:(abstract|static|final|public|protected|private)\\s*)?' . '(?:(abstract|static|final|public|protected|private)\\s*)?' . '(?:(abstract|static|final|public|protected|private)\\s*)?' . '(?:(\\S+)\\s+)?' . '([a-zA-Z0-9_:\\-\\>]+)\\s*\\((.*?)\\)$/s', $desc, $match);
     if (!$res) {
         throw new PC_PHPRef_Exception('Unable to parse "' . $desc . '"');
     }
     list(, $modifier1, $modifier2, $modifier3, $return, $name, $params) = $match;
     // detect class-names
     if (($pos = strpos($name, '::')) !== false || ($pos = strpos($name, '->')) !== false) {
         $classname = substr($name, 0, $pos);
         $name = substr($name, $pos + 2);
     }
     // build basic method
     $method = new PC_Obj_Method($file, 0, $classname != '');
     if ($modifier1 == 'static' || $modifier2 == 'static' || $modifier3 == 'static') {
         $method->set_static(true);
     }
     if ($modifier1 == 'final' || $modifier2 == 'final' || $modifier3 == 'final') {
         $method->set_final(true);
     }
     if ($modifier1 == 'abstract' || $modifier2 == 'abstract' || $modifier3 == 'abstract') {
         $method->set_abstract(true);
     }
     if (in_array($modifier1, array('private', 'protected'))) {
         $method->set_visibility($modifier1);
     } else {
         if (in_array($modifier2, array('private', 'protected'))) {
             $method->set_visibility($modifier2);
         } else {
             if (in_array($modifier3, array('private', 'protected'))) {
                 $method->set_visibility($modifier3);
             }
         }
     }
     if ($return) {
         $method->set_return_type(PC_Obj_MultiType::get_type_by_name($return));
         // set this always for builtin types since it makes no sense to report errors for
         // inherited classes or similar
         $method->set_has_return_doc(true);
     }
     $method->set_name($name);
     // check what kind of params we have
     $optional = '';
     $firstopt = strpos($params, '[');
     if ($firstopt !== false) {
         $required = substr($params, 0, $firstopt);
         $optional = substr($params, $firstopt + 1);
         $optional = str_replace(array('[', ']'), '', $optional);
     } else {
         $required = $params;
     }
     // add required ones
     $required = trim($required);
     if ($required && $required != 'void') {
         $reqparts = explode(', ', $required);
         foreach ($reqparts as $part) {
             list($type, $name) = explode(' ', trim($part));
             $param = new PC_Obj_Parameter();
             $param->set_name(trim($name));
             $param->set_mtype(self::get_param_type($type));
             $param->set_has_doc(true);
             $method->put_param($param);
         }
     }
     // add optional ones
     $optional = trim($optional);
     if ($optional) {
         $optparts = explode(', ', $optional);
         foreach ($optparts as $part) {
             $part = trim($part);
             if ($part == '') {
                 continue;
             }
             $default = null;
             $param = new PC_Obj_Parameter();
             $param->set_optional(true);
             // has it a known default-value?
             if (($pos = strpos($part, '=')) !== false) {
                 $nametype = trim(substr($part, 0, $pos));
                 $default = trim(substr($part, $pos + 1));
                 $parts = preg_split('/\\s+/', $nametype);
                 if (count($parts) != 2) {
                     throw new PC_PHPRef_Exception('Parameter description has not 2 parts: "' . $nametype . '"');
                 }
                 list($type, $name) = $parts;
             } else {
                 // detect variable arguments
                 if (strpos($part, '...') !== false) {
                     $param->set_first_vararg(true);
                     // sometimes there is a space bewteen $ and ...
                     $part = preg_replace('/\\$\\s+\\.\\.\\./', '$...', $part);
                 }
                 $parts = preg_split('/\\s+/', $part);
                 if (count($parts) != 2) {
                     throw new PC_PHPRef_Exception('Parameter description has not 2 parts: "' . $part . '"');
                 }
                 list($type, $name) = $parts;
             }
             // detect references
             if (substr($name, 0, 1) == '&') {
                 $param->set_reference(true);
                 $name = substr($name, 1);
             }
             $param->set_name(trim($name));
             $param->set_mtype(self::get_param_type($type, $default));
             $param->set_has_doc(true);
             $method->put_param($param);
         }
     }
     return array('func', $classname, $method);
 }