public function getDeclaration()
 {
     if ($this->reflector->isArray()) {
         $code = 'array ';
     } else {
         $class = $this->reflector->getClass();
         if ($class !== null) {
             $code = $class->name . ' ';
         } else {
             $code = '';
         }
     }
     $code .= '$' . $this->reflector->name;
     if ($this->reflector->isOptional()) {
         $default = $this->reflector->getDefaultValue();
         if (is_null($default)) {
             $default = 'null';
         } elseif (is_bool($default)) {
             $default = $default ? 'true' : 'false';
         } elseif (is_string($default)) {
             $default = "'" . $default . "'";
         } elseif (is_numeric($default)) {
             $default = strval($default);
         } elseif (is_array($default)) {
             $default = 'array()';
         } else {
             echo 'Warning: unknown default type for ' . $this->getMethod()->getFullName() . PHP_EOL;
             var_dump($default);
             $default = 'null';
         }
         $code .= ' = ' . $default;
     }
     return $code;
 }
 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}" : '');
 }
function test($param)
{
    $r = new ReflectionParameter('params', $param);
    var_dump($r->getDefaultValue());
    var_dump($r->getDefaultValueText());
    var_dump($r->getDefaultValueConstantName());
}
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function getParameterValue(\ReflectionParameter $parameter)
 {
     if ($parameter->isDefaultValueAvailable()) {
         return $parameter->getDefaultValue();
     }
     throw UnresolvedValueException::unresolvedParameter($parameter);
 }
示例#6
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;
 }
 /**
  * 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
文件: 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);
     }
 }
示例#9
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;
 }
 /**
  * @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;
 }
示例#11
0
 /**
  * Returns the default value of this parameter or throws an exception
  * @return mixed
  * @since PHP 5.0.3
  */
 public function getDefaultValue()
 {
     if ($this->parameter != null) {
         return $this->parameter->getDefaultValue();
     } else {
         return parent::getDefaultValue();
     }
 }
 private function getDefaultValue(\ReflectionParameter $parameter)
 {
     $defaultValue = null;
     if ($parameter->isDefaultValueAvailable()) {
         $defaultValue = $parameter->getDefaultValue();
     }
     return $defaultValue;
 }
示例#13
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.');
 }
示例#14
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();
     }
 }
示例#15
0
 /**
  * @param Horde_Injector $injector
  * @param ReflectionParameter $method
  *
  * @return mixed
  * @throws Horde_Injector_Exception
  */
 public function getParameterDependency(Horde_Injector $injector, ReflectionParameter $parameter)
 {
     if ($parameter->getClass()) {
         return $injector->getInstance($parameter->getClass()->getName());
     } elseif ($parameter->isOptional()) {
         return $parameter->getDefaultValue();
     }
     throw new Horde_Injector_Exception("Untyped parameter \$" . $parameter->getName() . "can't be fulfilled");
 }
示例#16
0
 protected function __construct(\ReflectionParameter $parameter, $overridenValue)
 {
     if (!$overridenValue instanceof MissingParameter && !$overridenValue instanceof DefaultParameter) {
         $this->setDefaultValue($overridenValue);
     } else {
         if ($parameter && $parameter->isOptional()) {
             $this->setDefaultValue($parameter->getDefaultValue());
         }
     }
 }
示例#17
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;
 }
示例#18
0
 protected function getParameterExtraValue(\ReflectionParameter $parameter)
 {
     if (empty($this->extraValues)) {
         if ($parameter->isOptional()) {
             return $parameter->getDefaultValue();
         } else {
             throw new Exception("");
         }
     } else {
         return array_pop($extras);
     }
 }
 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());
 }
示例#20
0
 private function getArgument(\ReflectionParameter $argument)
 {
     if ($argument->isOptional()) {
         return $argument->getDefaultValue();
     }
     if ($argument->allowsNull()) {
         return null;
     }
     if ($argument->getClass()) {
         return $this->getMockBuilder($argument->getClass()->getName())->disableOriginalConstructor()->getMock();
     }
     return null;
 }
示例#21
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;
 }
示例#22
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);
                         }
                     }
                 }
             }
         }
     }
 }
 protected function getParam(\ReflectionParameter $param)
 {
     $name = $param->getName();
     if (!isset($this->args['params'][$name]) && !$param->isOptional()) {
         $message = sprintf('You must define param "%s" for "%s"', $name, $this->args['class']);
         throw new \OutOfBoundsException($message);
     }
     if (!isset($this->args['params'][$name]) && $param->isOptional()) {
         return $param->getDefaultValue();
     }
     $value = $this->args['params'][$name];
     $param = is_string($value) && defined($value) ? constant($value) : $value;
     return $param;
 }
 private function resolveArgument(\ReflectionParameter $parameter)
 {
     $name = StringUtil::underscore($parameter->name);
     if ('container' === $name) {
         return $this->container;
     }
     if (isset($this->container[$name])) {
         return $this->container[$name];
     }
     if ($parameter->isOptional()) {
         return $parameter->getDefaultValue();
     }
     throw new \RuntimeException(sprintf('Unable to resolve parameter "%s" of class "%s" no service/parameter found with name "%s". ' . 'Consider adding a default value.', $name, $parameter->getDeclaringClass()->name, $name));
 }
示例#25
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;
 }
示例#26
0
 /**
  * Parses the specified arguments.
  *
  * @param  \Interop\Container\ContainerInterface $container
  * @param  \ReflectionParameter                  $parameter
  * @param  array                                 &$arguments
  * @return void
  */
 private function parseParameters(ContainerInterface $container, $parameter, array &$arguments)
 {
     if ($parameter->isOptional()) {
         return array_push($arguments, $parameter->getDefaultValue());
     }
     $class = $parameter->getClass()->getName();
     if ($container->has($class)) {
         return array_push($arguments, $container->get($class));
     }
     // This is where 'the magic happens'. We resolve each of the
     // dependencies, by recursively calling the resolve() method.
     // At one point, we will reach the bottom of the nested
     // dependencies we need in order to instantiate the class.
     array_push($arguments, $this->resolve($container, $class));
 }
 /**
  * Gets the argument data type
  *
  * Returns same data types as PHP's gettype() method:
  *  'boolean', 'integer', 'double', 'string', 'array',
  *  'object', 'resource', 'NULL', 'unknown type'
  *
  * @param    \ReflectionParameter $argument Argument's ReflectionParameter instance
  * @return    string
  */
 private function getArgumentType(\ReflectionParameter $argument)
 {
     if ($argument->isArray()) {
         return 'array';
     }
     // check to see if it's a typehinted class
     $regex = '/^.*\\<\\w+?> ([\\w\\\\]+?) +.*$/';
     preg_match($regex, $argument->__toString(), $matches);
     if (isset($matches[1])) {
         return 'object';
     }
     if ($argument->isOptional()) {
         return gettype($argument->getDefaultValue());
     }
     return null;
 }
 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;
 }
 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;
 }
示例#30
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;
 }