/** * 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; }
public function __construct(\ReflectionParameter $parameter, $message = null, $code = null, \Exception $previous = null) { if (null === $message) { $message = sprintf('Unable to resolve argument $%s (#%d) of %s.', $parameter->name, $parameter->getPosition(), static::getFunctionName($parameter->getDeclaringFunction())); } parent::__construct($message, $code, $previous); }
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; }
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); } }
protected static function createParameterTypeWithRef(ReflectionParameter $ref) { if ($ref->isArray()) { $parameter = new HermitSqlParameterSequence(); $parameter->add($ref->getName(), $ref->getPosition()); return $parameter; } $class = $ref->getClass(); if (is_null($class)) { $parameter = new HermitSqlParameterHash(); $parameter->add($ref->getName(), $ref->getPosition()); return $parameter; } $parameter = new HermitSqlParameterClassHash($class); $parameter->add($ref->getName(), $ref->getPosition()); return $parameter; }
function it_generates_correct_message_based_on_function_and_parameter(\ReflectionParameter $parameter, \ReflectionMethod $function, \ReflectionClass $class) { $parameter->getPosition()->willReturn(2); $function->getDeclaringClass()->willReturn($class); $class->getName()->willReturn('Acme\\Foo'); $function->getName()->willReturn('bar'); $this->getMessage()->shouldStartWith('Collaborator must be an object: argument 2 defined in Acme\\Foo::bar.'); }
/** * Returns whether this parameter is an optional parameter * @return integer * @since PHP 5.2.3 */ public function getPosition() { if ($this->parameter != null) { return $this->parameter->getPosition(); } else { return parent::getPosition(); } }
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 testDebugInfo() { $this->skipIf(php_sapi_name() === 'cli', 'Only for webtest runner'); $functionLists = ['Array' => ['array_change_key_case', 'array_chunk', 'array_combine', 'array_count_values', 'array_diff_assoc', 'array_diff_key', 'array_diff_uassoc', 'array_diff_ukey', 'array_diff', 'array_fill_keys', 'array_fill', 'array_filter', 'array_flip', 'array_intersect_assoc', 'array_intersect_key', 'array_intersect_uassoc', 'array_intersect_ukey', 'array_intersect', 'array_key_exists', 'array_keys', 'array_map', 'array_merge_recursive', 'array_merge', 'array_multisort', 'array_pad', 'array_pop', 'array_product', 'array_push', 'array_rand', 'array_reduce', 'array_replace_recursive', 'array_replace', 'array_reverse', 'array_search', 'array_shift', 'array_slice', 'array_splice', 'array_sum', 'array_udiff_assoc', 'array_udiff_uassoc', 'array_udiff', 'array_uintersect_assoc', 'array_uintersect_uassoc', 'array_uintersect', 'array_unique', 'array_unshift', 'array_values', 'array_walk_recursive', 'array_walk', 'array', 'arsort', 'asort', 'compact', 'count', 'current', 'each', 'end', 'extract', 'in_array', 'key', 'krsort', 'ksort', 'list', 'natcasesort', 'natsort', 'next', 'pos', 'prev', 'range', 'reset', 'rsort', 'shuffle', 'sizeof', 'sort', 'uasort', 'uksort', 'usort'], 'String' => ['addcslashes', 'addslashes', 'bin2hex', 'chop', 'chr', 'chunk_split', 'convert_cyr_string', 'convert_uudecode', 'convert_uuencode', 'count_chars', 'crc32', 'crypt', 'echo', 'explode', 'fprintf', 'get_html_translation_table', 'hebrev', 'hebrevc', 'hex2bin', 'html_entity_decode', 'htmlentities', 'htmlspecialchars_decode', 'htmlspecialchars', 'implode', 'join', 'lcfirst', 'levenshtein', 'localeconv', 'ltrim', 'md5_file', 'md5', 'metaphone', 'money_format', 'nl_langinfo', 'nl2br', 'number_format', 'ord', 'parse_str', 'print', 'printf', 'quoted_printable_decode', 'quoted_printable_encode', 'quotemeta', 'rtrim', 'setlocale', 'sha1_file', 'sha1', 'similar_text', 'soundex', 'sprintf', 'sscanf', 'str_getcsv', 'str_ireplace', 'str_pad', 'str_repeat', 'str_replace', 'str_rot13', 'str_shuffle', 'str_split', 'str_word_count', 'strcasecmp', 'strchr', 'strcmp', 'strcoll', 'strcspn', 'strip_tags', 'stripcslashes', 'stripos', 'stripslashes', 'stristr', 'strlen', 'strnatcasecmp', 'strnatcmp', 'strncasecmp', 'strncmp', 'strpbrk', 'strpos', 'strrchr', 'strrev', 'strripos', 'strrpos', 'strspn', 'strstr', 'strtok', 'strtolower', 'strtoupper', 'strtr', 'substr_compare', 'substr_count', 'substr_replace', 'substr', 'trim', 'ucfirst', 'ucwords', 'vfprintf', 'vprintf', 'vsprintf', 'wordwrap']]; $res = ''; foreach ($functionLists as $type => $functions) { $res .= "{$type} functions:\n"; foreach ($functions as $function) { try { $needle = new ReflectionParameter($function, "needle"); $haystack = new ReflectionParameter($function, "haystack"); $order = $needle->getPosition() < $haystack->getPosition() ? '$needle, $haystack' : '$haystack, $needle'; $res .= sprintf("%20s %s\n", $function, $order); } catch (ReflectionException $e) { continue; } } $res .= "\n"; } $this->debug($res); }
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); } } } } } } }
/** * @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; }
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; }
/** * 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; }
/** * normalize one param of a method * * @since 7-5-11 * @param ReflectionParameter $rparam * @param array $params see {@link normalizeParams()} for description of the $params array * @return mixed the normalized param */ public function normalizeParam(\ReflectionParameter $rparam, array $params = array()) { $ret_param = null; $index = $rparam->getPosition(); // first try and resolve numeric keys, then do string keys... // we use array_key_exists because values could be null if (array_key_exists($index, $params)) { $ret_param = $params[$index]; } else { $field_name = $rparam->getName(); if (array_key_exists($field_name, $params)) { $ret_param = $params[$field_name]; } else { try { $rclass = $rparam->getClass(); } catch (\ReflectionException $e) { throw new \ReflectionException(sprintf('%s which is param %s of method %s::%s()', $e->getMessage(), $index + 1, $rparam->getDeclaringClass()->getName(), $rparam->getDeclaringFunction()->getName())); } //try/catch if ($rclass === null) { if ($this->existsField($field_name)) { $ret_param = $this->getField($field_name); } else { if ($rparam->isDefaultValueAvailable()) { $ret_param = $rparam->getDefaultValue(); } else { $declaring_class = $rparam->getDeclaringClass(); $declaring_func = $rparam->getDeclaringFunction(); throw new \UnexpectedValueException(sprintf('no suitable value could be found for %s::%s() param "%s"', empty($declaring_class) ? 'unknown' : $declaring_class->getName(), empty($declaring_func) ? 'unkown' : $declaring_func->getName(), $field_name)); } } //if/else if/else } else { $class_name = $rclass->getName(); try { $ret_param = $this->getInstance($class_name); } catch (\Exception $e) { if ($rparam->isDefaultValueAvailable()) { $ret_param = $rparam->getDefaultValue(); } else { throw $e; } //if/else } //try/catch } //if/else } //if/else } //if/else return $ret_param; }
/** * Fixes parameters indexed by the parameter name -> reindex by position. * * This is necessary so that merging definitions between sources is possible. * * @param ObjectDefinition $definition * @param string $method * @param array $parameters * @return array */ private function fixParameters(ObjectDefinition $definition, $method, $parameters) { $fixedParameters = array(); foreach ($parameters as $index => $parameter) { // Parameter indexed by the parameter name, we reindex it with its position if (is_string($index)) { $callable = array($definition->getClassName(), $method); $reflectionParameter = new \ReflectionParameter($callable, $index); $index = $reflectionParameter->getPosition(); } $fixedParameters[$index] = $parameter; } return $fixedParameters; }
public function getPosition() { return $this->reflectionParameter->getPosition(); }
/** * 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; }
/** * Fixes parameters indexed by the parameter name -> reindex by position. * * This is necessary so that merging definitions between sources is possible. * * @param ObjectDefinition $definition * @param string $method * @param array $parameters * @throws DefinitionException * @return array */ private function fixParameters(ObjectDefinition $definition, $method, $parameters) { $fixedParameters = []; foreach ($parameters as $index => $parameter) { // Parameter indexed by the parameter name, we reindex it with its position if (is_string($index)) { $callable = [$definition->getClassName(), $method]; try { $reflectionParameter = new \ReflectionParameter($callable, $index); } catch (\ReflectionException $e) { throw DefinitionException::create($definition, sprintf("Parameter with name '%s' could not be found. %s.", $index, $e->getMessage())); } $index = $reflectionParameter->getPosition(); } $fixedParameters[$index] = $parameter; } return $fixedParameters; }
private function hasValue(\ReflectionParameter $param, array $args) { return array_key_exists($param->getName(), $args) || array_key_exists($param->getPosition(), $args); }
public function determineParameterValue(\ReflectionParameter $parameter, Constructor\Parameter\ParameterInterface $configuredParameter = null, array $values = []) { # extract relevant info from the ReflectionParameter. $name = $parameter->getName(); $type = $parameter->hasType() === true ? $parameter->getType()->__toString() : 'string'; $isConstructable = class_exists($type) || interface_exists($type); $default = $parameter->isDefaultValueAvailable() === true ? $parameter->getDefaultValue() : null; $position = $parameter->getPosition(); # if there's a configured parameter, use this first if (is_null($configuredParameter) === false) { return $configuredParameter->getValue($this); } # if a value was passed in by ordinal position in the values array, use this next if (isset($values[$position]) === true) { return $values[$position]; } # if a value was passed in by name in the values array, use this next if (isset($values[$name]) === true) { return $values[$name]; } if ($this->has($name) === true) { return $this->get($name); } # if this is not a constructable value and it's in the container by name, use that value if ($isConstructable === false && $this->has($name) === true) { return $this->get($name); } # if it's constructable, see if we have a constructor defined for it. if ($isConstructable === true) { $constructor = $this->findConstructor($name, $type); # if we still haven't found a way to construct it, but it *is* in theory # constructable, add a new constructor for it and hope everything works out. if ($constructor === false) { #echo("Constructing a new name=$name,type=$type\n"); $constructor = new Constructor\Constructor($type, $type, false); $this->addConstructor($constructor); } $this->_debug("Constructing parameter value {$name}/{$type}."); return $this->construct($name, $type, $constructor); } return $default; }
function argData(ReflectionParameter $arg) { $details = ""; $declaringClass = $arg->getDeclaringClass(); $name = $arg->getName(); $class = $arg->getClass(); $position = $arg->getPosition(); $details .= "{$name} Находится на позиции {$position} <br>"; if (!empty($class)) { $classname = $class->getName(); // возвращает объект класса ReflectionClass если в сигнатуре класса было уточнение $details .= "{$name} должен быть объектом типа {$classname} <br>"; } if ($arg->isPassedByReference()) { // проверяет является ли аргумент ссылкой $details .= "{$name} передан по ссылке <br>"; } if ($arg->isDefaultValueAvailable()) { // проверяет есть ли значение по умолчанию $def = $arg->getDefaultValue(); // получает значение по умолчанию $details = "{$name} по умолчанию равно: {$def} <br>"; } return $details; }
public function __construct(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function) { $message = sprintf('Collaborator must be an object: argument %s defined in %s. ' . 'You can create non-object values manually.', $parameter->getPosition(), $this->fetchFunctionIdentifier($function)); $this->setCause($function); parent::__construct($message); }
private function buildArgFromReflParam(\ReflectionParameter $reflParam) { if (array_key_exists($reflParam->name, $this->paramDefinitions)) { $arg = $this->paramDefinitions[$reflParam->name]; } elseif ($reflParam->isDefaultValueAvailable()) { $arg = $reflParam->getDefaultValue(); } elseif ($reflParam->isOptional()) { // This branch is required to work around PHP bugs where a parameter is optional // but has no default value available through reflection. Specifically, PDO exhibits // this behavior. $arg = null; } else { $reflFunc = $reflParam->getDeclaringFunction(); $classWord = $reflFunc instanceof \ReflectionMethod ? $reflFunc->getDeclaringClass()->name . '::' : ''; $funcWord = $classWord . $reflFunc->name; throw new InjectionException($this->inProgressMakes, sprintf(self::M_UNDEFINED_PARAM, $reflParam->name, $reflParam->getPosition(), $funcWord), self::E_UNDEFINED_PARAM); } return $arg; }
/** * Checks if reflection for a a method with two parameters works. * * @return void */ public function testMethodWithTwoParameters() { $reflectionParameter = new ReflectionParameter(__CLASS__, 'methodWithTwoParameters', 'test1'); $this->assertEquals(1, $reflectionParameter->getPosition()); }