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); } }
/** * Returns a string representation * @return string */ public function __toString() { if ($this->parameter) { return $this->parameter->__toString(); } else { return parent::__toString(); } }
/** * Representation of the object * * If an exception is caught, its message is returned instead of the * original result (but it is not thrown ahead). * * @return string */ public function __toString() { try { return parent::__toString() . ' with user value { ' . ReflectionValue::export($this->value, true) . ' }'; } catch (\Exception $e) { return $e->__toString(); } }
/** * Returns the type of the parameter. In case the parameter has no type (e.g. primitive) the * method returns null. * * @param ReflectionParameter $param * @return string */ public static function getParameterType(ReflectionParameter $param) { $matches = array(); preg_match('/\\[\\s\\<\\w+?>\\s([\\w]+)/s', $param->__toString(), $matches); if (isset($matches[1])) { return $matches[1]; } else { return null; } }
/** * @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; }
/** * 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; }
/** * A static version of `ReflectionParameter::getName()` that doesn't require the parameter class to be loaded. * * This prevents fatal errors when a parameter uses a class name for type hinting but the class is not loaded. * * @param \ReflectionParameter $param The parameter reflection object * @return string The name of the parameter's type hinted object, if there is one. */ protected static function getParameterClassName(\ReflectionParameter $param) { preg_match('/\\[\\s\\<\\w+?>\\s([a-zA-Z0-9_\\\\]+)/s', $param->__toString(), $matches); return isset($matches[1]) ? $matches[1] : null; }
public static final function getParameterType(ReflectionParameter $param) { preg_match('/\\[\\s\\<\\w+?>\\s([\\w]+)/s', $param->__toString(), $matches); return isset($matches[1]) ? $matches[1] : null; }
protected function getClassName(ReflectionParameter $param) { preg_match('/\\[\\s\\<\\w+?>\\s([\\w]+)/s', $param->__toString(), $matches); return isset($matches[1]) ? $matches[1] : null; }
protected function getParameterClassName(\ReflectionParameter $param) { preg_match('/> ([^ ]+) /', $param->__toString(), $matches); return !in_array($matches[1], ['$' . $param->getName(), 'array'], true) ? $matches[1] : null; }
private function getParamClassName(\ReflectionParameter $param) { preg_match('/\\[\\s\\<\\w+?>\\s([\\w\\\\]+)/s', $param->__toString(), $matches); return isset($matches[1]) ? $matches[1] : null; }
<?php function foo(array $x = array('a', 'b')) { } $r = new ReflectionParameter('foo', 0); echo $r->__toString();
private static function getClassName(\ReflectionParameter $param) { preg_match('/(\\w+)\\s\\$\\w+\\s\\]$/s', $param->__toString(), $matches); return isset($matches[1]) ? $matches[1] : null; }