/** * @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; }
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 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}" : ''); }
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; }
protected function checkArgument(ReflectionParameter $argument, $i) { if ($argument->isOptional() == false && !isset(Request::$get[$i])) { self::jump(400); } return $argument; }
/** * Returns whether this parameter is an optional parameter * @return boolean * @since PHP 5.0.3 */ public function isOptional() { if ($this->parameter != null) { return $this->parameter->isOptional(); } else { return parent::isOptional(); } }
/** * @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"); }
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; }
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()); } } }
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()); }
protected function getParameterExtraValue(\ReflectionParameter $parameter) { if (empty($this->extraValues)) { if ($parameter->isOptional()) { return $parameter->getDefaultValue(); } else { throw new Exception(""); } } else { return array_pop($extras); } }
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; }
/** * @param \ReflectionParameter $parameter * @param string $description */ public function addParameter(\ReflectionParameter $parameter, $description) { $name = $parameter->getName(); if (!$parameter->isOptional()) { $this->addArgument($name, InputArgument::REQUIRED, $description); return; } if ($this->isFlagOption($parameter)) { $this->addOption($name, null, InputOption::VALUE_NONE, $description); return; } $default = $parameter->getDefaultValue(); $this->addArgument($name, InputArgument::OPTIONAL, $description, $default); }
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)); }
/** * 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)); }
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; }
/** * 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; }
/** * @param \ReflectionParameter $param * @param mixed $arg * * @return bool */ static function paramValidateArg(\ReflectionParameter $param, $arg) { if (NULL !== ($expectedClassLike = $param->getClass())) { $expectedClassLikeName = $expectedClassLike->getName(); if ($arg instanceof $expectedClassLikeName) { return TRUE; } } elseif ($param->isArray()) { if (is_array($arg)) { return TRUE; } } else { return TRUE; } if ($param->isOptional()) { if ($arg === $param->getDefaultValue()) { return TRUE; } } return FALSE; }
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 \ReflectionParameter $reflectionParameter * @param array $args * @param bool $singleton * @return mixed * @throws \Exception */ public function getReflectionValue(\ReflectionParameter $reflectionParameter, array $args = array(), $singleton = true) { if (isset($args[$reflectionParameter->getName()])) { return $args[$reflectionParameter->getName()]; } elseif ($reflectionParameter->isOptional()) { return $reflectionParameter->getDefaultValue(); } else { if (!is_null($reflectionParameter->getClass())) { $className = $reflectionParameter->getClass()->getName(); if ($singleton === true) { return $this->dic->getSingleton($className); } else { return $this->dic->getInstance($className); } } else { if ($singleton === true) { return $this->dic->getSingleton($reflectionParameter->getName()); } else { return $this->dic->getInstance($reflectionParameter->getName()); } } } }
/** * 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; }
/** * Format an individual Closure parameter. * * @param \ReflectionParameter $param * * @return string */ protected function formatParam(\ReflectionParameter $param, $color = false) { $ret = '$' . $param->name; if ($color) { $ret = '<strong>' . $ret . '</strong>'; } if ($param->isOptional()) { $ret .= ' = '; if (version_compare(PHP_VERSION, '5.4.3', '>=') && $param->isDefaultValueConstant()) { $name = $param->getDefaultValueConstantName(); if ($color) { $ret .= '<const>' . $name . '</const>'; } else { $ret .= $name; } } elseif ($param->isDefaultValueAvailable()) { $ret .= $this->manager->presentRef($param->getDefaultValue(), $color); } else { if ($color) { $ret .= '<urgent>?</urgent>'; } else { $ret .= '?'; } } } return $ret; }
/** * Retrieve method parameter info * * @param \ReflectionParameter $parameter * @return array */ protected function _getMethodParameterInfo(\ReflectionParameter $parameter) { $parameterInfo = ['name' => $parameter->getName(), 'passedByReference' => $parameter->isPassedByReference()]; if ($parameter->isArray()) { $parameterInfo['type'] = 'array'; } elseif ($parameter->getClass()) { $parameterInfo['type'] = $this->_getFullyQualifiedClassName($parameter->getClass()->getName()); } if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) { $defaultValue = $parameter->getDefaultValue(); if (is_string($defaultValue)) { $parameterInfo['defaultValue'] = $parameter->getDefaultValue(); } elseif ($defaultValue === null) { $parameterInfo['defaultValue'] = $this->_getNullDefaultValue(); } else { $parameterInfo['defaultValue'] = $defaultValue; } } return $parameterInfo; }
/** * @return bool */ public function isOptional() { return $this->reflectionParameter->isOptional(); }
/** * Converts the given parameter reflection into an information array * * @param ReflectionParameter $parameter The parameter to reflect * @return array Parameter information array * @author Robert Lemke <*****@*****.**> * @author Sebastian Kurfürst <*****@*****.**> */ protected function convertParameterReflectionToArray(\ReflectionParameter $parameter, \ReflectionMethod $method = NULL) { $parameterInformation = array('position' => $parameter->getPosition(), 'byReference' => $parameter->isPassedByReference() ? TRUE : FALSE, 'array' => $parameter->isArray() ? TRUE : FALSE, 'optional' => $parameter->isOptional() ? TRUE : FALSE, 'allowsNull' => $parameter->allowsNull() ? TRUE : FALSE); $parameterClass = $parameter->getClass(); $parameterInformation['class'] = $parameterClass !== NULL ? $parameterClass->getName() : NULL; if ($parameter->isDefaultValueAvailable()) { $parameterInformation['defaultValue'] = $parameter->getDefaultValue(); } if ($parameterClass !== NULL) { $parameterInformation['type'] = ltrim($parameterClass->getName(), '\\'); } elseif ($method !== NULL) { $methodTagsAndValues = $this->getMethodTagsValues($method->getDeclaringClass()->getName(), $method->getName()); if (isset($methodTagsAndValues['param']) && isset($methodTagsAndValues['param'][$parameter->getPosition()])) { $explodedParameters = explode(' ', $methodTagsAndValues['param'][$parameter->getPosition()]); if (count($explodedParameters) >= 2) { $parameterInformation['type'] = ltrim($explodedParameters[0], '\\'); } } } return $parameterInformation; }
/** * Import information from reflection * * @param \ReflectionParameter $param * * @return static */ public function import(\ReflectionParameter $param) { $this->name = $param->name; if (isset($this->cb->params[$param->name])) { $doc_info = $this->cb->params[$param->name]; $this->desc = $doc_info["desc"]; $this->filters = $doc_info["filters"]; } else { $doc_info = false; } $this->optional = $param->isOptional(); $this->default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; $this->position = $param->getPosition(); if (isset($this->filters["inject"])) { $this->inject = $this->filters["inject"]["args"] ?: $param->name; unset($this->filters["inject"]); } if ($param->isArray()) { $this->multiple = true; $this->type = null; } if ($c = $param->getClass()) { $this->type = "object"; $this->class = $c->name; } elseif ($doc_info) { $_type = $doc_info["type"]; if (strpos($_type, "|")) { // multitype mark as mixed $this->type = null; return $this; } if (strpos($_type, "[")) { // multiple values $_type = strstr($_type, "[", true); $this->multiple = true; if ($_type === "array") { $this->type = "array"; return $this; } } if ($_type === "array") { $this->multiple = true; $this->type = null; } elseif ($_type == "mixed") { $this->type = null; return $this; } elseif (isset(self::$_scalar[$_type])) { $this->type = $_type; } elseif ($_type == "self" && $this->cb instanceof MethodInfo) { $this->type = "object"; $this->class = $this->cb->class; } else { $_type = ltrim($_type, '\\'); $this->type = "object"; $this->class = $_type; } } else { $this->type = null; } return $this; }
/** * @param \ReflectionParameter $param * * @return string|null */ protected function _getDefaultValue(\ReflectionParameter $param) { if ($param->isOptional()) { return $param->getDefaultValue(); } }
/** * Format an individual Closure parameter. * * @param \ReflectionParameter $param * * @return string */ protected function formatParam(\ReflectionParameter $param) { $ret = $this->formatParamName($param->name); if ($param->isOptional()) { $ret .= ' = '; if (self::isParamDefaultValueConstant($param)) { $name = $param->getDefaultValueConstantName(); $ret .= '<const>' . $name . '</const>'; } elseif ($param->isDefaultValueAvailable()) { $ret .= $this->manager->presentRef($param->getDefaultValue()); } else { $ret .= '<urgent>?</urgent>'; } } return $ret; }
/** * @param \ReflectionParameter $argument * @return object * @throws InjectionException */ private function getInstanceOfArgument(\ReflectionParameter $argument) { if (!$argument->getClass()) { if ($argument->isOptional()) { return $argument->getDefaultValue(); } throw new InjectionException('Invalid argument without class typehint class: [' . $argument->getDeclaringClass()->name . '] method: [' . $argument->getDeclaringFunction()->name . '] argument: [' . $argument->name . ']'); } $argumentClassConfig = $this->config->getClassConfig($argument->getClass()->name); $arguments = $this->getParamsFromTypeHint($argument); $providedInstance = $this->getNamedProvidedInstance($argument->getClass()->name, $argumentClassConfig, $argument->getDeclaringFunction()->getDocComment(), $argument->name, $arguments); if ($providedInstance) { return $providedInstance; } $namedClassName = $this->getNamedClassOfArgument($argument->getClass()->name, $argument->getDeclaringFunction()->getDocComment(), $argument->name); if ($namedClassName) { return $this->getInstanceOfClass($namedClassName, $arguments); } return $this->getInstanceOfClass($argument->getClass()->name, $arguments); }