本文整理汇总了PHP中ReflectionParameter类的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionParameter类的具体用法?PHP ReflectionParameter怎么用?PHP ReflectionParameter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ReflectionParameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
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);
}
}
示例2: getParamTypeHint
public function getParamTypeHint(\ReflectionFunctionAbstract $function, \ReflectionParameter $param)
{
$lowParam = strtolower($param->name);
if ($function instanceof \ReflectionMethod) {
$lowClass = strtolower($function->class);
$lowMethod = strtolower($function->name);
$paramCacheKey = self::CACHE_KEY_CLASSES . "{$lowClass}.{$lowMethod}.param-{$lowParam}";
} else {
$lowFunc = strtolower($function->name);
$paramCacheKey = $lowFunc !== '{closure}' ? self::CACHE_KEY_FUNCS . ".{$lowFunc}.param-{$lowParam}" : null;
}
$typeHint = $paramCacheKey === null ? false : $this->cache->fetch($paramCacheKey);
if (false !== $typeHint) {
return $typeHint;
}
if ($reflectionClass = $param->getClass()) {
$typeHint = $reflectionClass->getName();
$classCacheKey = self::CACHE_KEY_CLASSES . strtolower($typeHint);
$this->cache->store($classCacheKey, $reflectionClass);
} else {
$typeHint = null;
}
$this->cache->store($paramCacheKey, $typeHint);
return $typeHint;
}
示例3: getParameterValue
/**
* {@inheritdoc}
*/
public function getParameterValue(\ReflectionParameter $parameter)
{
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
throw UnresolvedValueException::unresolvedParameter($parameter);
}
示例4: getParamDocBlockHint
/**
* @param array $docBlockParams
* @param \ReflectionParameter $param
* @param array $arguments
*
* @return bool|string
*/
public function getParamDocBlockHint(array $docBlockParams, \ReflectionParameter $param, array $arguments = [])
{
$typeHint = false;
/** @var DocBlock\Tag\ParamTag $docBlockParam */
foreach ($docBlockParams as $docBlockParam) {
if (!$docBlockParam instanceof DocBlock\Tag\ParamTag) {
continue;
}
$type = $docBlockParam->getType();
$docBlockParamName = $docBlockParam->getVariableName();
if ($param->getName() === ltrim($docBlockParamName, '$') && !empty($type)) {
$definitions = explode('|', $type);
foreach ($arguments as $key => $argument) {
foreach ($definitions as $definition) {
if (is_object($argument) && in_array(ltrim($definition, '\\'), $this->getImplemented(get_class($argument))) && (is_numeric($key) || ltrim($docBlockParamName, '$') === $key)) {
$typeHint = $definition;
// no need to loop again, since we found a match already!
continue 3;
}
}
}
if ($typeHint === false) {
// use first definition, there is no way to know which instance of the hinted doc block definitions is actually required
// because there were either no arguments given or no argument match was found
list($firstDefinition, ) = $definitions;
if (!in_array(strtolower($firstDefinition), ['int', 'float', 'bool', 'string', 'array'])) {
$typeHint = $firstDefinition;
}
}
}
}
return $typeHint;
}
示例5: loadArray
private static function loadArray($object, &$result, $prefix = "")
{
$reflect = new ReflectionClass($object);
$typeName = $reflect->getName();
$properties = ObjectParser::getProperties($object);
if (is_array($properties)) {
foreach ($properties as $key => $prop) {
$propertyName = $prop->getName();
$propNameCamelCase = ucfirst($propertyName);
$getMethodName = "get{$propNameCamelCase}";
$setMethodName = "set{$propNameCamelCase}";
$setIdMethodName = "{$setMethodName}_id";
$setMethodParameter = new ReflectionParameter(array($typeName, $setMethodName), 0);
if ($reflect->getMethod($setMethodName) && $setMethodParameter->getClass() && method_exists($object, $setIdMethodName)) {
continue;
}
$value = $object->{$getMethodName}();
$name = $prefix . $propertyName;
if (is_object($value)) {
ObjectParser::loadArray($value, $result, "{$name}.");
} else {
$result[$name] = $value;
}
}
return true;
}
return false;
}
示例6: test
function test($param)
{
$r = new ReflectionParameter('params', $param);
var_dump($r->getDefaultValue());
var_dump($r->getDefaultValueText());
var_dump($r->getDefaultValueConstantName());
}
示例7: __construct
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);
}
示例8: canInject
/**
* @param ReflectionParameter $parameter
* @param CollectionInterface $properties
*
* @return bool
*/
private function canInject(\ReflectionParameter $parameter, CollectionInterface $properties) : bool
{
if (!$parameter->allowsNull() && !$properties->hasKey($parameter->name)) {
return false;
} else {
if ($parameter->allowsNull() && !$properties->hasKey($parameter->name)) {
return false;
}
}
$property = $properties[$parameter->name];
if ($parameter->hasType()) {
$type = $parameter->getType();
if ($type->isBuiltin()) {
return (string) $type === gettype($property);
} else {
if (!is_object($property)) {
return false;
}
}
$refl = new \ReflectionObject($property);
$wishedClass = (string) $type;
return get_class($property) === $wishedClass || $refl->isSubClassOf($wishedClass);
}
return true;
}
示例9: __construct
public function __construct(\ReflectionParameter $parameter)
{
if (!$parameter->isCallable()) {
throw new \InvalidArgumentException('Provided parameter should have a callable type hint');
}
parent::__construct($parameter);
}
示例10: getArgConfig
/**
* @param Zend_Config $testConfig
* @param ReflectionParameter $arg
* @throws Exception
* @throws KalturaTestException
* @return Ambigous
*/
protected function getArgConfig(Zend_Config $testConfig, ReflectionParameter $arg)
{
$argName = $arg->getName();
$argConfig = $testConfig->get($argName);
KalturaLog::debug("Tests data [{$argName}] config [" . print_r($argConfig, true) . "]");
if (!$argConfig) {
if (!$arg->allowsNull()) {
throw new Exception("Argument [{$argName}] can't be null for test [" . $this->getName() . "]");
}
return null;
}
if (is_string($argConfig)) {
return $argConfig;
}
switch ($argConfig->objectType) {
case 'dependency':
throw new KalturaTestException("Argument [{$argName}] taken from dependency");
case 'array':
return $this->populateArray($argConfig);
case 'native':
return $argConfig->value;
case 'file':
return $argConfig->path;
default:
return $this->populateObject($argConfig);
}
}
示例11: getArgumentMock
/**
* @param string $className
* @param string $argumentName
* @param \ReflectionParameter $parameter
* @return mixed
*/
private function getArgumentMock(string $className, string $argumentName, \ReflectionParameter $parameter)
{
if ($parameter->getClass() !== null) {
return $this->testCase->getMockBuilder($parameter->getClass()->name)->disableOriginalConstructor()->getMock();
}
throw new \Mocktainer\UnmockableConstructorArgumentException($className, $argumentName);
}
示例12: getParamTypeHint
public function getParamTypeHint(\ReflectionFunctionAbstract $function, \ReflectionParameter $param, array $arguments = [])
{
$lowParam = strtolower($param->name);
if ($function instanceof \ReflectionMethod) {
$lowClass = strtolower($function->class);
$lowMethod = strtolower($function->name);
$paramCacheKey = self::CACHE_KEY_CLASSES . "{$lowClass}.{$lowMethod}.param-{$lowParam}";
} else {
$lowFunc = strtolower($function->name);
$paramCacheKey = $lowFunc !== '{closure}' ? self::CACHE_KEY_FUNCS . ".{$lowFunc}.param-{$lowParam}" : null;
}
$typeHint = $paramCacheKey === null ? false : $this->cache->fetch($paramCacheKey);
if (false !== $typeHint) {
return $typeHint;
}
if ($reflectionClass = $param->getClass()) {
$typeHint = $reflectionClass->getName();
$classCacheKey = self::CACHE_KEY_CLASSES . strtolower($typeHint);
$this->cache->store($classCacheKey, $this->getClass($param->getClass()->getName()));
} elseif ($function instanceof \ReflectionMethod && ($docBlockParams = $this->getDocBlock($function)->getTagsByName('param')) && !empty($docBlockParams)) {
$typeHint = $this->getParamDocBlockHint($docBlockParams, $param, $arguments);
// store the ExtendedReflectionClass in the cache
if ($typeHint !== false) {
$classCacheKey = self::CACHE_KEY_CLASSES . strtolower($typeHint);
$this->cache->store($classCacheKey, $this->getClass($typeHint));
}
} else {
$typeHint = null;
}
$this->cache->store($paramCacheKey, $typeHint);
return $typeHint;
}
示例13: getArgument
private static function getArgument(ReflectionParameter $parameter)
{
if (isset($_REQUEST[$parameter->name])) {
if ($parameter->isArray()) {
return $_REQUEST[$parameter->name];
}
$type = self::getArgumentType($parameter);
if (!class_exists($type)) {
throw new Exception("Type not found for attribute [{$parameter->name}]");
}
$object = new $type();
if (is_array($_REQUEST[$parameter->name])) {
foreach ($_REQUEST[$parameter->name] as $attribute => $value) {
$object->{$attribute} = $value;
}
}
return $object;
}
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
if ($parameter->allowsNull()) {
return null;
}
if ($parameter->isArray()) {
return array();
}
$type = self::getArgumentType($parameter);
if (!class_exists($type)) {
throw new Exception("Type not found for attribute [{$parameter->name}]");
}
return new $type();
}
示例14: __construct
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;
}
示例15: getParameterAnnotations
public function getParameterAnnotations(\ReflectionParameter $parameter)
{
$class = $parameter->getDeclaringClass() ?: 'Closure';
$method = $parameter->getDeclaringFunction();
if (!$method->isUserDefined()) {
return array();
}
$context = 'parameter ' . ($class === 'Closure' ? $class : $class->getName()) . '::' . $method->getName() . '($' . $parameter->getName() . ')';
if ($class === 'Closure') {
$this->parser->setImports($this->getClosureImports($method));
} else {
$this->parser->setImports($this->getImports($class));
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
}
$lines = file($method->getFileName());
$lines = array_slice($lines, $start = $method->getStartLine() - 1, $method->getEndLine() - $start);
$methodBody = Implode($lines);
$methodBody = str_replace("\n", null, $methodBody);
$signature = preg_split('/\\)\\s*\\{/', $methodBody);
$signature = $signature[0];
$signature = substr($signature, strpos($signature, "function"));
if (preg_match_all('/\\/\\*\\*(.*?)\\*\\/' . '.*?\\$(\\w+)/', $signature, $matches)) {
$docComments = $matches[1];
$names = $matches[2];
for ($i = 0, $len = count($names); $i < $len; ++$i) {
if ($names[$i] === $parameter->name) {
return $this->parser->parse($docComments[$i], $context);
}
}
}
return array();
}