コード例 #1
0
ファイル: Method.php プロジェクト: jnvsor/kint
 public function __construct(ReflectionFunctionAbstract $method)
 {
     $this->name = $method->getShortName();
     $this->filename = $method->getFilename();
     $this->startline = $method->getStartLine();
     $this->endline = $method->getEndLine();
     $this->docstring = $method->getDocComment();
     $this->operator = $this->static ? Kint_Object::OPERATOR_STATIC : Kint_Object::OPERATOR_OBJECT;
     $this->access = Kint_Object::ACCESS_PUBLIC;
     if ($method instanceof ReflectionMethod) {
         $this->static = $method->isStatic();
         $this->abstract = $method->isAbstract();
         $this->final = $method->isFinal();
         $this->owner_class = $method->getDeclaringClass()->name;
         if ($method->isProtected()) {
             $this->access = Kint_Object::ACCESS_PROTECTED;
         } elseif ($method->isPrivate()) {
             $this->access = Kint_Object::ACCESS_PRIVATE;
         }
     }
     foreach ($method->getParameters() as $param) {
         $this->parameters[] = new Kint_Object_Parameter($param);
     }
     if ($this->docstring) {
         if (preg_match('/@return\\s+(.*)\\r?\\n/m', $this->docstring, $matches)) {
             if (!empty($matches[1])) {
                 $this->returntype = $matches[1];
             }
         }
     }
     $docstring = new Kint_Object_Representation_Docstring($this->docstring, $this->filename, $this->startline);
     $docstring->implicit_label = true;
     $this->addRepresentation($docstring);
 }
コード例 #2
0
 /**
  * @param CollaboratorManager $collaborators
  * @param \ReflectionFunctionAbstract $function
  */
 private function generateCollaborators(CollaboratorManager $collaborators, \ReflectionFunctionAbstract $function)
 {
     if ($comment = $function->getDocComment()) {
         $comment = str_replace("\r\n", "\n", $comment);
         foreach (explode("\n", trim($comment)) as $line) {
             if (preg_match(self::$docex, $line, $match)) {
                 $collaborator = $this->getOrCreateCollaborator($collaborators, $match[2]);
                 $collaborator->beADoubleOf($match[1]);
             }
         }
     }
     foreach ($function->getParameters() as $parameter) {
         $collaborator = $this->getOrCreateCollaborator($collaborators, $parameter->getName());
         try {
             if (null !== ($class = $parameter->getClass())) {
                 $collaborator->beADoubleOf($class->getName());
             }
         } catch (ReflectionException $e) {
             throw new CollaboratorNotFoundException(sprintf('Collaborator does not exist '), 0, $e, $parameter);
         }
     }
 }
コード例 #3
0
 /**
  * @param \ReflectionFunctionAbstract $function
  *
  * @return ReturnComment|false
  */
 public function parseDocComment(\ReflectionFunctionAbstract $function)
 {
     $docComment = $function->getDocComment();
     if (!$docComment) {
         return false;
     }
     if (preg_match('/\\*\\s*@return\\s(.*)/i', $docComment, $matches)) {
         return $this->parseReturnString($matches[1], $function);
     } elseif ($function instanceof \ReflectionMethod && false !== strpos($docComment, '{@inheritdoc}')) {
         /** @var \ReflectionMethod $function */
         $class = $function->getDeclaringClass();
         foreach ($class->getInterfaces() as $interface) {
             $result = $this->parseClassMethod($interface, $function->getName());
             if (false !== $result) {
                 return $result;
             }
         }
         if ($parent = $class->getParentClass()) {
             return $this->parseClassMethod($parent, $function->getName());
         }
     }
     return false;
 }
コード例 #4
0
 /**
  * @param CollaboratorManager         $collaborators
  * @param \ReflectionFunctionAbstract $function
  */
 private function generateCollaborators(CollaboratorManager $collaborators, \ReflectionFunctionAbstract $function)
 {
     if ($comment = $function->getDocComment()) {
         $comment = str_replace("\r\n", "\n", $comment);
         foreach (explode("\n", trim($comment)) as $line) {
             if (preg_match(self::$docex, $line, $match)) {
                 $collaborator = $this->getOrCreateCollaborator($collaborators, $match[2]);
                 $collaborator->beADoubleOf($match[1]);
             }
         }
     }
     foreach ($function->getParameters() as $parameter) {
         $collaborator = $this->getOrCreateCollaborator($collaborators, $parameter->getName());
         if (null !== ($class = $parameter->getClass())) {
             $collaborator->beADoubleOf($class->getName());
         }
     }
 }
コード例 #5
0
 /**
  * @param CollaboratorManager         $collaborators
  * @param \ReflectionFunctionAbstract $function
  * @param \ReflectionClass            $classRefl
  */
 private function generateCollaborators(CollaboratorManager $collaborators, \ReflectionFunctionAbstract $function, \ReflectionClass $classRefl)
 {
     if ($comment = $function->getDocComment()) {
         $comment = str_replace("\r\n", "\n", $comment);
         foreach (explode("\n", trim($comment)) as $line) {
             if (preg_match(self::$docex, $line, $match)) {
                 $collaborator = $this->getOrCreateCollaborator($collaborators, $match[2]);
                 $collaborator->beADoubleOf($match[1]);
             }
         }
     }
     foreach ($function->getParameters() as $parameter) {
         $collaborator = $this->getOrCreateCollaborator($collaborators, $parameter->getName());
         try {
             if ($this->isUnsupportedTypeHinting($parameter)) {
                 throw new InvalidCollaboratorTypeException($parameter, $function);
             }
             if (($indexedClass = $this->getParameterTypeFromIndex($classRefl, $parameter)) || ($indexedClass = $this->getParameterTypeFromReflection($parameter))) {
                 $collaborator->beADoubleOf($indexedClass);
             }
         } catch (ClassNotFoundException $e) {
             $this->throwCollaboratorNotFound($e, null, $e->getClassname());
         } catch (DisallowedScalarTypehintException $e) {
             throw new InvalidCollaboratorTypeException($parameter, $function);
         }
     }
 }
コード例 #6
0
ファイル: EntityFile.php プロジェクト: sergeyk-jomedia/koda
 /**
  *
  * @param EntityFunction $callable
  * @param \ReflectionFunctionAbstract $reflection
  * @return $this
  */
 public static function parseCallable(EntityFunction $callable, \ReflectionFunctionAbstract $reflection)
 {
     $doc = $reflection->getDocComment();
     $params = [];
     if ($doc) {
         $info = ToolKit::parseDoc($doc);
         $callable->setDescription($info['desc']);
         $callable->setReturnInfo($info['return']['type'], $reflection->returnsReference(), $info['return']['desc']);
         $callable->setOptions($info['options']);
         $params = $info["params"];
     } else {
         $info = [];
     }
     /* @var \ReflectionParameter[] $params */
     foreach ($reflection->getParameters() as $param) {
         $argument = new EntityArgument($param->name);
         if (isset($params[$param->name]["desc"])) {
             $argument->description = $params[$param->name]["desc"];
         }
         if ($callable->getLine()) {
             $argument->setLine($callable->getLine());
         }
         $argument->setOptional($param->isOptional());
         $argument->setNullAllowed($param->allowsNull());
         $argument->setValue($param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->isPassedByReference());
         $argument->setByRef($param->isPassedByReference());
         /** @var \ReflectionParameter $param */
         if ($param->isArray()) {
             $argument->setCast(Types::ARR);
         }
         if ($c = $param->getClass()) {
             $argument->setCast(Types::OBJECT, $c->name);
         } elseif (isset($info['params'][$param->name])) {
             $_type = $info['params'][$param->name]["type"];
             if (strpos($_type, "|") || $_type === "mixed") {
                 // multiple types or mixed
                 $argument->setCast(Types::MIXED);
             } else {
                 if (strpos($_type, "[]")) {
                     $argument->setCast(Types::ARR, rtrim($_type, '[]'));
                 }
                 if (isset(Types::$codes[$_type])) {
                     $argument->setCast(Types::getType($_type));
                 } else {
                     $argument->setCast(Types::OBJECT, ltrim($_type, '\\'));
                 }
             }
         } else {
             $argument->warning("not documented");
         }
         $callable->pushArgument($argument);
     }
 }
コード例 #7
0
ファイル: upgradelib.php プロジェクト: dg711/moodle
/**
 * Given some sort of reflection function/method object, return an array of docblock lines, where each line is an array of
 * keywords/descriptions
 *
 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
 *
 * @return array docblock converted in to an array
 */
function admin_mnet_method_get_docblock(ReflectionFunctionAbstract $function)
{
    return array_map(function ($line) {
        $text = trim($line, " \t\n\r\v*/");
        if (strpos($text, '@param') === 0) {
            return preg_split('/\\s+/', $text, 4);
        }
        if (strpos($text, '@return') === 0) {
            return preg_split('/\\s+/', $text, 3);
        }
        return array($text);
    }, explode("\n", $function->getDocComment()));
}
コード例 #8
0
 /**
  * @param \ReflectionFunctionAbstract $reflection
  */
 public function setDocBlockFromReflection(\ReflectionFunctionAbstract $reflection)
 {
     $docBlock = $reflection->getDocComment();
     if ($docBlock) {
         $docBlock = preg_replace('/([\\n\\r])(' . self::$_indentation . ')+/', '$1', $docBlock);
         $this->setDocBlock($docBlock);
     }
 }
コード例 #9
0
ファイル: Smd.php プロジェクト: xamiro-dev/xamiro
 /**
  * map a class method using reflection doc comment to get all comments to map
  * parameter type and method return value
  *
  * @error 14107
  * @param ReflectionFunctionAbstract $method expects the method to map
  * @return XO
  */
 protected function mapMethod(ReflectionFunctionAbstract $method)
 {
     $obj = new XO();
     $tmp = array();
     if (($comment = $method->getDocComment()) !== false) {
         $lines = preg_split('/\\n/', trim($comment));
         if (sizeof($lines) > 1) {
             $obj->description = array();
             $obj->params = array();
             $i = -1;
             foreach ($lines as $line) {
                 $i++;
                 if ($i === 0 || $i === sizeof($lines) - 1) {
                     continue;
                 }
                 if (stripos($line, '@') === false || stripos($line, '@desc') !== false) {
                     $line = str_replace(array("\n", "\r\n", "\t"), '', trim($line));
                     $line = preg_replace(array('/\\@desc/i', '/^\\*\\/?\\s*/'), array('', ''), $line);
                     if (!empty($line)) {
                         $tmp[] = $line;
                     }
                 }
                 if (stripos($line, '@param') !== false || stripos($line, '@return') !== false) {
                     $line = str_replace(array("\t", "\n", "\r"), '', trim($line));
                     $line = substr($line, strpos($line, '@'));
                     $line = preg_replace(array('/[ \\s]+/', '/[ \\s]\\|[ \\s]/'), array(' ', '|'), $line);
                     $word = preg_split('/\\s+/i', $line);
                     $word = strtolower($word[1]);
                     if (stripos($word, '|') !== false) {
                         $word = explode('|', $word);
                     }
                     if (stripos($line, '@param') !== false) {
                         if (preg_match('/\\$\\w+\\b(.*)$/isD', $line, $m)) {
                             $obj->params['description'][] = trim($m[1]);
                         } else {
                             $obj->params['description'][] = null;
                         }
                         if (is_string($word) && stripos($word, '$') !== false) {
                             $obj->params['type'][] = 'mixed';
                         } else {
                             $obj->params['type'][] = self::mapType($word);
                         }
                     }
                     if (stripos($line, '@return') !== false) {
                         $obj->returns = self::mapType($word);
                     }
                 }
             }
             if (!empty($tmp)) {
                 $obj->description = implode(" ", $tmp);
             }
         }
     }
     return $obj;
 }
コード例 #10
0
ファイル: CallableInfo.php プロジェクト: bzick/koda
 /**
  * Import callable info from reflection
  *
  * @param \ReflectionFunctionAbstract $method
  *
  * @return static
  */
 protected function _importFromReflection(\ReflectionFunctionAbstract $method)
 {
     $doc = $method->getDocComment();
     $doc_params = [];
     $this->return = new ReturnInfo($this);
     if ($doc) {
         $doc = preg_replace('/^\\s*(\\*\\s*)+/mS', '', trim($doc, "*/ \t\n\r"));
         if (strpos($doc, "@") !== false) {
             $doc = explode("@", $doc, 2);
             if ($doc[0] = trim($doc[0])) {
                 $this->desc = $doc[0];
             }
             if ($doc[1]) {
                 foreach (preg_split('/\\r?\\n@/mS', $doc[1]) as $param) {
                     $param = preg_split('/\\s+/S', $param, 2);
                     if (!isset($param[1])) {
                         $param[1] = "";
                     }
                     switch (strtolower($param[0])) {
                         case 'description':
                             if (empty($this->desc)) {
                                 $this->desc = $param[1];
                             }
                             break;
                         case 'param':
                             if (preg_match('/^(.*?)\\s+\\$(\\w+)\\s*(?:\\(([^\\)]+)\\))?/mS', $param[1], $matches)) {
                                 $this->params[$matches[2]] = ["type" => $matches[1], "desc" => trim(substr($param[1], strlen($matches[0]))), "filters" => isset($matches[3]) ? Filter::parseDoc($matches[3]) : []];
                             }
                             break;
                         case 'return':
                             $return = preg_split('~\\s+~mS', $param[1], 2);
                             if ($return) {
                                 $this->return->type = $return[0];
                                 if (count($return) == 2) {
                                     $this->return->desc = $return[1];
                                 }
                             }
                             break;
                         default:
                             if (isset($this->options[$param[0]])) {
                                 $this->options[$param[0]][] = $param[1];
                             } else {
                                 $this->options[$param[0]] = [$param[1]];
                             }
                     }
                 }
             }
         } else {
             $this->desc = $doc;
         }
     }
     foreach ($method->getParameters() as $param) {
         $this->args[$param->name] = $arg = new ArgumentInfo($this);
         $arg->import($param);
     }
     return $this;
 }