Nette\PhpGenerator\Parameter::from PHP Method

from() public static method

public static from ( ReflectionParameter $from ) : self
$from ReflectionParameter
return self
    public static function from(\ReflectionParameter $from)
    {
        $param = new static($from->getName());
        $param->reference = $from->isPassedByReference();
        if (PHP_VERSION_ID >= 70000) {
            $param->typeHint = $from->hasType() ? (string) $from->getType() : NULL;
        } elseif ($from->isArray() || $from->isCallable()) {
            $param->typeHint = $from->isArray() ? 'array' : 'callable';
        } else {
            try {
                $param->typeHint = $from->getClass() ? $from->getClass()->getName() : NULL;
            } catch (\ReflectionException $e) {
                if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
                    $param->typeHint = $m[1];
                } else {
                    throw $e;
                }
            }
        }
        $param->optional = $from->isDefaultValueAvailable();
        $param->defaultValue = $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
        return $param;
    }

Usage Example

Example #1
0
 /**
  * @return self
  */
 public static function from($from) : self
 {
     if (is_string($from) && strpos($from, '::')) {
         $from = new \ReflectionMethod($from);
     } elseif (is_array($from)) {
         $from = new \ReflectionMethod($from[0], $from[1]);
     } elseif (!$from instanceof \ReflectionFunctionAbstract) {
         $from = new \ReflectionFunction($from);
     }
     $method = new static();
     $method->name = $from->isClosure() ? NULL : $from->getName();
     foreach ($from->getParameters() as $param) {
         $method->parameters[$param->getName()] = Parameter::from($param);
     }
     if ($from instanceof \ReflectionMethod) {
         $method->static = $from->isStatic();
         $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL);
         $method->final = $from->isFinal();
         $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface();
         $method->body = $from->isAbstract() ? FALSE : '';
     }
     $method->returnReference = $from->returnsReference();
     $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
     $method->comment = $from->getDocComment() ? preg_replace('#^\\s*\\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
     if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
         $returnType = $from->getReturnType();
         $method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType;
     }
     return $method;
 }
All Usage Examples Of Nette\PhpGenerator\Parameter::from