/**
  * @return self
  */
 public static function from(\ReflectionParameter $from)
 {
     $param = new static();
     $param->name = $from->getName();
     $param->reference = $from->isPassedByReference();
     if ($from->isArray()) {
         $param->typeHint = 'array';
     } elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
         $param->typeHint = '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 = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable();
     $param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
     $namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL;
     $namespace = $namespace ? "\\{$namespace}\\" : '\\';
     if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {
         $param->typeHint = substr($param->typeHint, strlen($namespace));
     }
     return $param;
 }
示例#2
0
 /**
  * @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()) {
         $param->typeHint = 'array';
     } elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
         $param->typeHint = '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 = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable();
     $param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
     return $param;
 }
 public function getType() : Type
 {
     if ($this->type === null) {
         $phpDocType = $this->phpDocType;
         if ($phpDocType !== null && $this->reflection->isDefaultValueAvailable() && $this->reflection->getDefaultValue() === null) {
             $phpDocType = $phpDocType->makeNullable();
         }
         $this->type = TypehintHelper::decideTypeFromReflection($this->reflection->getType(), $phpDocType, $this->reflection->getDeclaringClass() !== null ? $this->reflection->getDeclaringClass()->getName() : null, $this->reflection->isVariadic());
     }
     return $this->type;
 }
 public function exportCode()
 {
     $default_value = null;
     if ($this->_parameter->isDefaultValueAvailable()) {
         $default_value = $this->_parameter->getDefaultValue();
         if (is_scalar($default_value) && !is_numeric($default_value)) {
             $default_value = "'{$default_value}'";
         }
     } elseif ($this->_parameter->isOptional()) {
         $default_value = 'NULL';
     }
     return sprintf('%s%s$%s%s', $this->_parameter->getClass() ? "{$this->_parameter->getClass()->getName()} " : '', $this->_parameter->isPassedByReference() ? '&' : '', $this->_parameter->getName(), $default_value ? " = {$default_value}" : '');
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function getParameterValue(\ReflectionParameter $parameter)
 {
     if ($parameter->isDefaultValueAvailable()) {
         return $parameter->getDefaultValue();
     }
     throw UnresolvedValueException::unresolvedParameter($parameter);
 }
 public function testGeneralInfoGetters()
 {
     $allNameGetters = ['isArray', 'isCallable', 'isOptional', 'isPassedByReference', 'isDefaultValueAvailable', 'getPosition', 'canBePassedByValue', 'allowsNull', 'getDefaultValue', 'getDefaultValueConstantName', 'isDefaultValueConstant', '__toString'];
     $onlyWithDefaultValues = array_flip(['getDefaultValue', 'getDefaultValueConstantName', 'isDefaultValueConstant']);
     if (PHP_VERSION_ID >= 50600) {
         $allNameGetters[] = 'isVariadic';
     }
     if (PHP_VERSION_ID >= 70000) {
         $allNameGetters[] = 'hasType';
     }
     foreach ($this->parsedRefFile->getFileNamespaces() as $fileNamespace) {
         foreach ($fileNamespace->getFunctions() as $refFunction) {
             $functionName = $refFunction->getName();
             foreach ($refFunction->getParameters() as $refParameter) {
                 $parameterName = $refParameter->getName();
                 $originalRefParameter = new \ReflectionParameter($functionName, $parameterName);
                 foreach ($allNameGetters as $getterName) {
                     // skip some methods if there is no default value
                     $isDefaultValueAvailable = $originalRefParameter->isDefaultValueAvailable();
                     if (isset($onlyWithDefaultValues[$getterName]) && !$isDefaultValueAvailable) {
                         continue;
                     }
                     $expectedValue = $originalRefParameter->{$getterName}();
                     $actualValue = $refParameter->{$getterName}();
                     $this->assertSame($expectedValue, $actualValue, "{$getterName}() for parameter {$functionName}:{$parameterName} should be equal");
                 }
             }
         }
     }
 }
 /**
  * Creates a PHP parameter from reflection
  * 
  * @param \ReflectionParameter $ref
  * @return PhpParameter
  */
 public static function fromReflection(\ReflectionParameter $ref)
 {
     $parameter = new static();
     $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference());
     if ($ref->isDefaultValueAvailable()) {
         $parameter->setDefaultValue($ref->getDefaultValue());
     }
     // find type and description in docblock
     $docblock = new Docblock($ref->getDeclaringFunction());
     $params = $docblock->getTags('param');
     $tag = $params->find($ref->name, function (ParamTag $t, $name) {
         return $t->getVariable() == '$' . $name;
     });
     if ($tag !== null) {
         $parameter->setType($tag->getType(), $tag->getDescription());
     }
     // set type if not found in comment
     if ($parameter->getType() === null) {
         if ($ref->isArray()) {
             $parameter->setType('array');
         } elseif ($class = $ref->getClass()) {
             $parameter->setType($class->getName());
         } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) {
             $parameter->setType('callable');
         }
     }
     return $parameter;
 }
示例#8
0
 public function __construct(\ReflectionParameter $parameter)
 {
     $this->name = $parameter->getName();
     $this->position = $parameter->getPosition();
     $this->has_default = $parameter->isDefaultValueAvailable();
     $this->default_value = $this->getHasDefault() ? $parameter->getDefaultValue() : null;
 }
示例#9
0
文件: Parameter.php 项目: jnvsor/kint
 public function __construct(ReflectionParameter $param)
 {
     if (method_exists('ReflectionParameter', 'getType')) {
         if ($type = $param->getType()) {
             $this->type_hint = (string) $type;
         }
     } else {
         if ($param->isArray()) {
             $this->type_hint = 'array';
         } else {
             try {
                 if ($this->type_hint = $param->getClass()) {
                     $this->type_hint = $this->type_hint->name;
                 }
             } catch (ReflectionException $e) {
                 preg_match('/\\[\\s\\<\\w+?>\\s([\\w]+)/s', $param->__toString(), $matches);
                 $this->type_hint = isset($matches[1]) ? $matches[1] : '';
             }
         }
     }
     $this->reference = $param->isPassedByReference();
     $this->position = $param->getPosition();
     $this->name = $param->getName();
     if ($param->isDefaultValueAvailable()) {
         $this->default = var_export($param->getDefaultValue(), true);
     }
 }
示例#10
0
 /**
  * @param ReflectionParameter $parameter
  * @return mixed
  * @throws Exception
  */
 public function resolveNonClass($parameter)
 {
     // 有默认值则返回默认值
     if ($parameter->isDefaultValueAvailable()) {
         return $parameter->getDefaultValue();
     }
     throw new Exception('I have no idea what to do here.');
 }
示例#11
0
 /**
  * Returns whether the default value of this parameter is available
  * @return boolean
  * @since PHP 5.0.3
  */
 public function isDefaultValueAvailable()
 {
     if ($this->parameter != null) {
         return $this->parameter->isDefaultValueAvailable();
     } else {
         return parent::isDefaultValueAvailable();
     }
 }
 private function getDefaultValue(\ReflectionParameter $parameter)
 {
     $defaultValue = null;
     if ($parameter->isDefaultValueAvailable()) {
         $defaultValue = $parameter->getDefaultValue();
     }
     return $defaultValue;
 }
示例#13
0
文件: Argument.php 项目: neel/bong
 /**
  * @load
  * @param ReflectionParameter $reflection
  */
 private function _createFromReflection($reflection)
 {
     $this->_name = $reflection->getName();
     if ($reflection->isDefaultValueAvailable()) {
         $this->_default = true;
         $this->_value = $reflection->getDefaultValue();
     }
 }
示例#14
0
 function __construct(API_Doc_Method $method, ReflectionParameter $parameter)
 {
     $this->name = $parameter->getName();
     $this->is_passed_by_reference = $parameter->isPassedByReference();
     $this->allows_null = $parameter->allowsNull();
     $this->is_optional = $parameter->isOptional();
     $this->is_default_value_available = $parameter->isDefaultValueAvailable();
     $this->position = $parameter->getPosition();
     $this->declaring_method = $method;
 }
 public function testWrappedMethods()
 {
     $php_parameter = new \ReflectionParameter([$this, 'method'], 'param');
     $our_parameter = new ReflectionParameter($php_parameter);
     $this->assertSame($php_parameter->getName(), $our_parameter->getName());
     $this->assertSame($php_parameter->allowsNull(), $our_parameter->allowsNull());
     $this->assertSame($php_parameter->isOptional(), $our_parameter->isOptional());
     $this->assertSame($php_parameter->isDefaultValueAvailable(), $our_parameter->isDefaultValueAvailable());
     $this->assertSame($php_parameter->isVariadic(), $our_parameter->isVariadic());
     $this->assertSame($php_parameter->isPassedByReference(), $our_parameter->isPassedByReference());
     $this->assertSame($php_parameter->getDefaultValue(), $our_parameter->getDefaultValue());
 }
示例#16
0
 /**
  * @param \ReflectionParameter $parameter
  * @return mixed|null
  */
 private function extractDefaultValue(\ReflectionParameter $parameter)
 {
     if ($parameter->isDefaultValueAvailable()) {
         $value = $parameter->getDefaultValue();
     } else {
         return null;
     }
     if (empty($value) || !is_array($value)) {
         $value = null;
     }
     return $value;
 }
示例#17
0
 /**
  * Setup arguments for current action
  *
  * @param \ReflectionParameter $Parameter
  *
  * @return array
  */
 public function fillParameter(\ReflectionParameter $Parameter)
 {
     $UrlParts = Application::$i->URL->parts();
     if (!isset($UrlParts[$Parameter->getPosition() + 2])) {
         if ($Parameter->isDefaultValueAvailable() === false) {
             showError('Parameter $' . $Parameter->getName() . ' not found');
         }
         $value = $Parameter->getDefaultValue();
     } else {
         $value = Application::$i->URL->parts($Parameter->getPosition() + 2);
     }
     return $value;
 }
示例#18
0
 /**
  * @return bool
  */
 public function isDefaultValueAvailable()
 {
     if (PHP_VERSION_ID === 50316) {
         // PHP bug #62988
         try {
             $this->getDefaultValue();
             return TRUE;
         } catch (ReflectionException $e) {
             return FALSE;
         }
     }
     return parent::isDefaultValueAvailable();
 }
示例#19
0
 protected function resolveParameter($parameters, \ReflectionParameter $constructorArg)
 {
     $parameterName = $constructorArg->getName();
     $parameterPosition = $constructorArg->getPosition();
     if (isset($parameters[$parameterName])) {
         return $parameters[$parameterName];
     } else {
         if (isset($parameters[$parameterPosition])) {
             return $parameters[$parameterPosition];
         } else {
             if (isset($this->parameters[$parameterName])) {
                 return $this->parameters[$parameterName];
             } else {
                 if (isset($this->parameters[$parameterPosition])) {
                     return $this->parameters[$parameterPosition];
                 } else {
                     if ($constructorArg->getClass() !== null) {
                         try {
                             return $this->factory->get($constructorArg->getClass()->getName());
                         } catch (InstantiationException $e) {
                             if ($constructorArg->isDefaultValueAvailable()) {
                                 return $constructorArg->getDefaultValue();
                             } else {
                                 throw $e;
                             }
                         }
                     } else {
                         if ($constructorArg->isDefaultValueAvailable()) {
                             return $constructorArg->getDefaultValue();
                         } else {
                             throw new ParameterMissingException($parameterName);
                         }
                     }
                 }
             }
         }
     }
 }
示例#20
0
 /**
  * @param \ReflectionParameter $parameter
  * @return Parameter
  */
 public function parameter(\ReflectionParameter $parameter)
 {
     $phpyParam = new Parameter($parameter->getName());
     if ($parameter->isArray()) {
         $phpyParam->setTypeHint('array');
     } elseif ($class = $parameter->getClass()) {
         $phpyParam->setTypeHint('\\' . $class->getName());
     }
     if ($parameter->isDefaultValueAvailable()) {
         $phpyParam->setDefaultValue($parameter->getDefaultValue());
     }
     $phpyParam->setByRef($parameter->isPassedByReference());
     return $phpyParam;
 }
 public static function fromReflection(\ReflectionParameter $ref)
 {
     $parameter = new static();
     $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference());
     if ($ref->isDefaultValueAvailable()) {
         $parameter->setDefaultValue($ref->getDefaultValue());
     }
     if ($ref->isArray()) {
         $parameter->setType('array');
     } elseif ($class = $ref->getClass()) {
         $parameter->setType($class->getName());
     } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) {
         $parameter->setType('callable');
     }
     return $parameter;
 }
 private function shouldParameterHaveAnArgument(\ReflectionParameter $parameter)
 {
     if ($parameter->isOptional()) {
         // any last argument with a default value is optional
         return false;
     }
     if ($parameter->isDefaultValueAvailable()) {
         // e.g. $username = '******'
         return false;
     }
     if ($parameter->getClass() && $parameter->allowsNull()) {
         // e.g. LoggerInterface $logger = null
         return false;
     }
     return true;
 }
 protected function generateParameterDeclaration(\ReflectionParameter $param)
 {
     $output = '';
     if ($param->isArray()) {
         $output .= 'array ';
     } elseif (is_callable(array($param, 'isCallable')) && $param->isCallable()) {
         $output .= 'callable ';
     } elseif ($param->getClass()) {
         $output .= '\\' . $param->getClass()->getName() . ' ';
     }
     if ($param->isPassedByReference()) {
         $output .= '&';
     }
     $output .= '$' . $param->getName();
     if ($param->isDefaultValueAvailable()) {
         $output .= ' = ' . $param->getDefaultValue();
     }
     return $output;
 }
示例#24
0
文件: arguments.php 项目: jaz303/phpx
 public static function create_from_reflection_parameter(\ReflectionParameter $rp)
 {
     $arg = new Argument($rp->getName());
     if ($rp->allowsNull()) {
         $arg->set_null_allowed(true);
     }
     if ($rp->isDefaultValueAvailable()) {
         $arg->set_default($rp->getDefaultValue());
     }
     if ($rp->isArray()) {
         $arg->set_array(true);
     } elseif ($type = $rp->getClass()) {
         $arg->set_type($type->getName());
     }
     if ($rp->isPassedByReference()) {
         $arg->set_reference(true);
     }
     return $arg;
 }
示例#25
0
文件: help.php 项目: ralf000/PHP4
function get_params(ReflectionParameter $p)
{
    $description = "";
    //Это объект?
    $c = $p->getClass();
    if (is_object($c)) {
        $description .= $c->getName() . " ";
    }
    $description .= "\$" . $p->getName();
    //Есть значение по умолчанию?
    if ($p->isDefaultValueAvailable()) {
        $val = $p->getDefaultValue();
        //Может быть пустой строкой
        if ($val == "") {
            $val = "\"\"";
        }
        $description .= " = {$val}";
    }
    return $description;
}
 /**
  * Returns the argument value for a parameter.
  *
  * @param \ReflectionParameter $parameter
  *   The parameter of a callable to get the value for.
  * @param \Symfony\Component\Routing\Route $route
  *   The access checked route.
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The current request.
  * @param \Drupal\Core\Session\AccountInterface $account
  *   The current user.
  *
  * @return mixed
  *   The value of the requested parameter value.
  *
  * @throws \RuntimeException
  *   Thrown when there is a missing parameter.
  */
 protected function getArgument(\ReflectionParameter $parameter, Route $route, Request $request, AccountInterface $account)
 {
     $upcasted_route_arguments = $request->attributes->all();
     $raw_route_arguments = isset($upcasted_route_arguments['_raw_variables']) ? $upcasted_route_arguments['_raw_variables']->all() : $upcasted_route_arguments;
     $parameter_type_hint = $parameter->getClass();
     $parameter_name = $parameter->getName();
     // @todo Remove this once AccessManager::checkNamedRoute() is fixed to not
     //   leak _raw_variables from the request being duplicated.
     // @see https://drupal.org/node/2265939
     $raw_route_arguments += $upcasted_route_arguments;
     // If the route argument exists and is NULL, return it, regardless of
     // parameter type hint.
     if (!isset($upcasted_route_arguments[$parameter_name]) && array_key_exists($parameter_name, $upcasted_route_arguments)) {
         return NULL;
     }
     if ($parameter_type_hint) {
         // If the argument exists and complies with the type hint, return it.
         if (isset($upcasted_route_arguments[$parameter_name]) && is_object($upcasted_route_arguments[$parameter_name]) && $parameter_type_hint->isInstance($upcasted_route_arguments[$parameter_name])) {
             return $upcasted_route_arguments[$parameter_name];
         }
         // Otherwise, resolve $request, $route, and $account by type matching
         // only. This way, the callable may rename them in case the route
         // defines other parameters with these names.
         foreach (array($request, $route, $account) as $special_argument) {
             if ($parameter_type_hint->isInstance($special_argument)) {
                 return $special_argument;
             }
         }
     } elseif (isset($raw_route_arguments[$parameter_name])) {
         return $raw_route_arguments[$parameter_name];
     }
     // If the callable provides a default value, use it.
     if ($parameter->isDefaultValueAvailable()) {
         return $parameter->getDefaultValue();
     }
     // Can't resolve it: call a method that throws an exception or can be
     // overridden to do something else.
     return $this->handleUnresolvedArgument($parameter);
 }
 public function __construct($method, $param)
 {
     $param = new ReflectionParameter($method, $param);
     $this->name = $param->name;
     if ($param->isDefaultValueAvailable()) {
         $default = $param->getDefaultValue();
         if ($default === NULL) {
             $this->default .= 'NULL';
         } elseif (is_bool($default)) {
             $this->default .= $default ? 'TRUE' : 'FALSE';
         } elseif (is_string($default)) {
             $this->default .= "'" . $default . "'";
         } else {
             $this->default .= print_r($default, TRUE);
         }
     }
     if ($param->isPassedByReference()) {
         $this->byref = TRUE;
     }
     if ($param->isOptional()) {
         $this->optional = TRUE;
     }
 }
 /**
  * @param array                $data
  * @param \ReflectionParameter $param
  * @param string               $format
  * @param array                $context
  *
  * @return object
  */
 protected function denormalizeParam($data, $param, $format, $context)
 {
     $name = $param->getName();
     $index = $param->getPosition();
     if (array_key_exists($name, $data)) {
         $value = $data[$name];
     } elseif (array_key_exists($index, $data)) {
         $value = $data[$index];
     } elseif ($param->isDefaultValueAvailable()) {
         $value = $param->getDefaultValue();
     } else {
         $message = sprintf('Missing parameter #%s: %s', $index, $name);
         throw new \RuntimeException($message);
     }
     if ($param->getClass()) {
         $class = $param->getClass()->getName();
     } elseif ($this->serializer->supportsDenormalization($value, MixedDenormalizer::TYPE, $format)) {
         $class = MixedDenormalizer::TYPE;
     }
     if (isset($class)) {
         $value = $this->serializer->denormalize($value, $class, $format, $context);
     }
     return $value;
 }
示例#29
0
function argData(ReflectionParameter $arg)
{
    $details = "";
    $declaringclass = $arg->getDeclaringClass();
    $name = $arg->getName();
    $class = $arg->getClass();
    $position = $arg->getPosition();
    $details .= "\${$name} has position {$position}\n";
    if (!empty($class)) {
        $classname = $class->getName();
        $details .= "\${$name} must be a {$classname} object\n";
    }
    if ($arg->isPassedByReference()) {
        $details .= "\${$name} is passed by reference\n";
    }
    if ($arg->isDefaultValueAvailable()) {
        $def = $arg->getDefaultValue();
        $details .= "\${$name} has default: {$def}\n";
    }
    if ($arg->allowsNull()) {
        $details .= "\${$name} can be null\n";
    }
    return $details;
}
示例#30
0
 /**
  * Generates the code for an individual method parameter.
  *
  * @param ReflectionParameter $parameter
  *
  * @return string
  */
 protected function implementParameter(ReflectionParameter $parameter)
 {
     $default = '';
     $type = '';
     if ($parameter->isArray()) {
         $type = 'array ';
     } elseif ($parameter->getClass() !== null) {
         $type = $parameter->getClass()->getName() . ' ';
     }
     if ($parameter->isDefaultValueAvailable()) {
         $default = ' = ' . var_export($parameter->getDefaultValue(), true);
     } elseif ($parameter->isOptional()) {
         $default = ' = null';
     }
     return $type . ($parameter->isPassedByReference() ? '&' : '') . '$parm' . $parameter->getPosition() . $default;
 }