本文整理汇总了PHP中ReflectionParameter::getType方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionParameter::getType方法的具体用法?PHP ReflectionParameter::getType怎么用?PHP ReflectionParameter::getType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionParameter
的用法示例。
在下文中一共展示了ReflectionParameter::getType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getType
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;
}
示例2: from
/**
* @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;
}
示例3: from
/**
* @return self
*/
public static function from(\ReflectionParameter $from)
{
$param = new static();
$param->name = $from->getName();
$param->reference = $from->isPassedByReference();
if (PHP_VERSION_ID >= 70000) {
$type = $from->getType();
$param->typeHint = $type ? ($type->isBuiltin() ? '' : '\\') . $type : NULL;
} elseif ($from->isArray() || $from->isCallable()) {
$param->typeHint = $from->isArray() ? 'array' : '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 = $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;
}
示例4: 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;
}
示例5: __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);
}
}
示例6: testGetType
public function testGetType()
{
$p1 = new ReflectionParameter(new \ReflectionParameter([$this, 'method'], 'param'));
$p2 = new ReflectionParameter(new \ReflectionParameter([$this, 'method'], 'param_2'));
$p3 = new ReflectionParameter(new \ReflectionParameter([$this, 'method'], 'param_3'));
$this->assertEquals('array', $p1->getType()->getName());
$this->assertNull($p2->getType());
$this->assertEquals('\\' . \Exception::class, $p3->getType()->getName());
}
示例7: setReflectionMetadata
/**
* Set initial reflection metadata into the parameter definition
*
* @param ParameterDefinition $parameterDefinition
* @param \ReflectionParameter $reflectionParameter
*/
public function setReflectionMetadata(ParameterDefinition $parameterDefinition, \ReflectionParameter $reflectionParameter)
{
// Set parameter metadata
if ($reflectionParameter->isDefaultValueAvailable()) {
$parameterDefinition->setValue($reflectionParameter->getDefaultValue());
}
if ($reflectionParameter->getType()) {
$parameterDefinition->setTypeHint($reflectionParameter->getType());
}
$parameterDefinition->setIsOptional($reflectionParameter->isOptional());
}
示例8: getParameterType
/**
* @return string|NULL
*/
public static function getParameterType(\ReflectionParameter $param)
{
if (PHP_VERSION_ID >= 70000) {
if ($param->hasType()) {
$type = PHP_VERSION_ID >= 70100 ? $param->getType()->getName() : (string) $param->getType();
return strtolower($type) === 'self' ? $param->getDeclaringClass()->getName() : $type;
}
} elseif ($param->isArray() || $param->isCallable()) {
return $param->isArray() ? 'array' : 'callable';
} else {
try {
return ($ref = $param->getClass()) ? $ref->getName() : NULL;
} catch (\ReflectionException $e) {
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
return $m[1];
}
throw $e;
}
}
}
示例9: getType
/**
* @return ReflectionTypeInterface|null
*/
public function getType()
{
if (PHP_MAJOR_VERSION >= 7) {
if ($this->hasType()) {
return new ReflectionType($this->parameter->getType());
} else {
return null;
}
}
$type = null;
preg_match('/\\[\\s<\\w+?>\\s([\\\\\\w]+)/', $this->parameter->__toString(), $matches);
if (isset($matches[1])) {
if (in_array($matches[1], ReflectionTypeInterface::NON_QUALIFIED_TYPES, true)) {
$name = $matches[1];
} else {
$name = '\\' . $matches[1];
}
$type = new ReflectionTypePolyFill($name, $this->parameter->allowsNull());
}
return $type;
}
示例10: getType
/**
* Returns an associated type to the given parameter if available.
*
* @param \ReflectionParameter $parameter
*
* @return null|string
*/
private function getType(\ReflectionParameter $parameter)
{
if (PHP_VERSION_ID >= 70000) {
return $parameter->hasType() ? (string) $parameter->getType() : null;
}
if ($parameter->isArray()) {
return 'array';
}
if ($parameter->isCallable()) {
return 'callable';
}
try {
$refClass = $parameter->getClass();
} catch (\ReflectionException $e) {
// mandatory; extract it from the exception message
return str_replace(array('Class ', ' does not exist'), '', $e->getMessage());
}
return $refClass ? $refClass->getName() : null;
}
示例11: getParameterType
/**
* @return string|NULL
*/
public static function getParameterType(\ReflectionParameter $param)
{
if (PHP_VERSION_ID >= 70000) {
return $param->hasType() ? (string) $param->getType() : NULL;
} elseif ($param->isArray()) {
return 'array';
} elseif (PHP_VERSION_ID >= 50400 && $param->isCallable()) {
return 'callable';
} else {
try {
return ($ref = $param->getClass()) ? $ref->getName() : NULL;
} catch (\ReflectionException $e) {
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
return $m[1];
}
throw $e;
}
}
}
示例12: getType
private function getType(\ReflectionParameter $parameter)
{
if ($this->supportsParameterType) {
if (!($type = $parameter->getType())) {
return;
}
$typeName = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
if ('array' === $typeName && !$type->isBuiltin()) {
return;
}
return $typeName;
}
if (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\\x7F-\\xFF][^ ]++)/', $parameter, $info)) {
return $info[1];
}
}
示例13: getParametersForFunction
/**
* @param $tmpParameter
*
* @return array
*/
protected function getParametersForFunction(\ReflectionParameter $tmpParameter)
{
//Need to do some magic to get type of the parameter :)
if (NULL === $tmpParameter->getClass()) {
if ($tmpParameter->getType() != NULL) {
$par = ['name' => $tmpParameter->getName(), 'type' => $tmpParameter->getType()];
} else {
$par = ['name' => $tmpParameter->getName()];
}
if ($tmpParameter->isDefaultValueAvailable()) {
$par['defaultvalue'] = $tmpParameter->getDefaultValue();
return $par;
}
return $par;
} else {
$par = ['name' => $tmpParameter->getName(), 'type' => $tmpParameter->getClass()->name];
if ($tmpParameter->isDefaultValueAvailable()) {
$par['defaultvalue'] = $tmpParameter->getDefaultValue();
return $par;
}
return $par;
}
}
示例14: parseReflectedParams
protected function parseReflectedParams(\ReflectionParameter $param)
{
$types = [];
if ($this->canInspectReflectionParamType && (bool) ($type = $param->getType())) {
$types[] = (string) $type;
if ($type->allowsNull()) {
$type[] = 'null';
}
}
return ['name' => $param->getName(), 'types' => $types, 'hint' => ''];
}
示例15: getParameterType
/**
* @return [string, bool]
*/
public static function getParameterType(\ReflectionParameter $param)
{
$def = gettype($param->isDefaultValueAvailable() ? $param->getDefaultValue() : NULL);
if (PHP_VERSION_ID >= 70000) {
return array((string) $param->getType() ?: $def, $param->hasType() && !$param->getType()->isBuiltin());
} elseif ($param->isArray()) {
return array('array', FALSE);
} elseif (PHP_VERSION_ID >= 50400 && $param->isCallable()) {
return array('callable', FALSE);
} else {
try {
return ($ref = $param->getClass()) ? array($ref->getName(), TRUE) : array($def, FALSE);
} catch (\ReflectionException $e) {
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
throw new \LogicException(sprintf("Class %s not found. Check type hint of parameter \$%s in %s() or 'use' statements.", $m[1], $param->getName(), $param->getDeclaringFunction()->getDeclaringClass()->getName() . '::' . $param->getDeclaringFunction()->getName()));
}
throw $e;
}
}
}