/** * @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; }
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; }
/** * @return string|NULL */ public static function getParameterType(\ReflectionParameter $param) { if ($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; } } }
/** * @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; }
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); } }
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(); }
/** * Creates a PHP parameter from reflection * * @param \ReflectionParameter $ref * @return PhpParameter */ public static function fromReflection(\ReflectionParameter $ref) { $parameter = new static(); $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference()); if ($ref->isDefaultValueAvailable()) { $parameter->setDefaultValue($ref->getDefaultValue()); } // find type and description in docblock $docblock = new Docblock($ref->getDeclaringFunction()); $params = $docblock->getTags('param'); $tag = $params->find($ref->name, function (ParamTag $t, $name) { return $t->getVariable() == '$' . $name; }); if ($tag !== null) { $parameter->setType($tag->getType(), $tag->getDescription()); } // set type if not found in comment if ($parameter->getType() === null) { if ($ref->isArray()) { $parameter->setType('array'); } elseif ($class = $ref->getClass()) { $parameter->setType($class->getName()); } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) { $parameter->setType('callable'); } } return $parameter; }
public function __construct(\ReflectionParameter $parameter) { if (!$parameter->isArray()) { throw new \InvalidArgumentException('Provided parameter should have an array type hint'); } parent::__construct($parameter); }
/** * Returns whether parameter MUST be an array * @return boolean * @since PHP 5.1.0 */ public function isArray() { if ($this->parameter != null) { return $this->parameter->isArray(); } else { return parent::isArray(); } }
public function validate(\ReflectionParameter $parameter, $argument) { if ($parameter->isArray()) { $this->validateArrayArgument($argument); } elseif ($parameter->getClass()) { $this->validateObjectArgument($parameter->getClass()->getName(), $argument, $parameter->allowsNull()); } // other arguments don't need to be or can't be validated }
/** * @param \ReflectionParameter $reflectionParameter * * @return null|string */ protected function getTypeByReflectionParameter(\ReflectionParameter $reflectionParameter) { if ($reflectionParameter->getClass()) { return $reflectionParameter->getClass()->getName(); } if ($reflectionParameter->isArray()) { return 'array'; } return null; }
protected function getType(\ReflectionParameter $param) { if ($param->isArray()) { return "array"; } if ($cls = $param->getClass()) { return $cls->getName(); } return ""; }
/** * Denormalize a method parameter value. * * @param \ReflectionParameter $reflectionParameter * @param mixed $value Parameter value to denormalize * @return mixed */ protected function denormalizeParameter(\ReflectionParameter $reflectionParameter, $value) { if ($reflectionParameter->getClass() === null && !$reflectionParameter->isArray()) { return $value; } if (!$this->serializer instanceof DenormalizerInterface) { throw new LogicException('Cannot denormalize because injected serializer is not a denormalizer'); } if ($reflectionParameter->getClass() !== null) { return $this->serializer->denormalize($value, $reflectionParameter->getClass()->name); } if ($reflectionParameter->isArray()) { $className = $reflectionParameter->getDeclaringClass()->getName(); $methodName = $reflectionParameter->getDeclaringFunction()->getName(); $parameterName = $reflectionParameter->getName(); return $this->serializer->denormalize($value, $className . '::' . $methodName . '(' . $parameterName . ')'); } return $value; }
/** * Helper class for getParameterName(). * * @param \ReflectionParameter $parameter * @return string */ private static function getTypeHint(\ReflectionParameter $parameter) { if ($parameter->isArray()) { return 'array '; } $class = $parameter->getClass(); if ($class) { return $class->getName() . ' '; } return ''; }
/** * @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; } } }
/** * Returns parameter type. * * @param \ReflectionParameter $param ReflectionParameter object. * @return array|null Array if parameter has its type, null otherwise. */ protected function getParameterType(\ReflectionParameter $param) { if ($param->isArray()) { return array('name' => 'array'); } $classType = $param->getClass(); if (null !== $classType) { return $this->getInheritanceInspection($classType); } return null; }
/** * @param \ReflectionParameter $param * * @return string|null */ protected function _getHintName(\ReflectionParameter $param) { if (version_compare(phpversion(), '5.4', '>=') && $param->isCallable()) { return 'callable'; } if ($param->isArray()) { return 'array'; } if ($class = $param->getClass()) { return $class->getName(); } }
/** * Checks if the value matches the parameter type. * * @param \ReflectionParameter $parameter * @param mixed $value * * @return bool */ protected static function matchType(\ReflectionParameter $parameter, $value) { if ($class = $parameter->getClass()) { return is_object($value) && $class->isInstance($value); } if ($parameter->isArray()) { return is_array($value); } if ($parameter->isCallable()) { return is_callable($value); } return true; }
private function buildParameter(\ReflectionParameter $parameter) { switch (true) { case $parameter->getClass() !== null: return new ClassArgument($parameter); case $parameter->isArray(): return new ArrayArgument($parameter); case $parameter->isCallable(): return new CallableArgument($parameter); default: return new MixedArgument($parameter); } }
/** * @param \ReflectionParameter $parameter * @return Parameter */ public function parameter(\ReflectionParameter $parameter) { $phpyParam = new Parameter($parameter->getName()); if ($parameter->isArray()) { $phpyParam->setTypeHint('array'); } elseif ($class = $parameter->getClass()) { $phpyParam->setTypeHint('\\' . $class->getName()); } if ($parameter->isDefaultValueAvailable()) { $phpyParam->setDefaultValue($parameter->getDefaultValue()); } $phpyParam->setByRef($parameter->isPassedByReference()); return $phpyParam; }
public static function fromReflection(\ReflectionParameter $ref) { $parameter = new static(); $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference()); if ($ref->isDefaultValueAvailable()) { $parameter->setDefaultValue($ref->getDefaultValue()); } if ($ref->isArray()) { $parameter->setType('array'); } elseif ($class = $ref->getClass()) { $parameter->setType($class->getName()); } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) { $parameter->setType('callable'); } return $parameter; }
private function generateTypeHint(\ReflectionParameter $param) { if ($param->isArray()) { return 'array '; } else { if ($param->isCallable()) { return 'callable '; } else { if ($param->getClass()) { return $param->getClass()->getName() . ' '; } else { return ''; } } } }
/** * 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; }
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; }
private static function buildField(\ReflectionParameter $parameter, $data) { // If parameter turns out to be an Object then recurse if (!empty($parameter->getClass())) { return self::build($parameter->getClass()->getName(), $data); } // If parameter is type of array, then check whether it is an array of Object if ($parameter->isArray()) { $possibleObjectClassName = $parameter->getDeclaringClass()->getName() . '_' . $parameter->getName() . '_item'; if (class_exists($possibleObjectClassName)) { return array_map(function ($values) use($possibleObjectClassName) { return self::build($possibleObjectClassName, $values); }, $data); } } return $data; }
public static function create_from_reflection_parameter(\ReflectionParameter $rp) { $arg = new Argument($rp->getName()); if ($rp->allowsNull()) { $arg->set_null_allowed(true); } if ($rp->isDefaultValueAvailable()) { $arg->set_default($rp->getDefaultValue()); } if ($rp->isArray()) { $arg->set_array(true); } elseif ($type = $rp->getClass()) { $arg->set_type($type->getName()); } if ($rp->isPassedByReference()) { $arg->set_reference(true); } return $arg; }
/** * 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; }
protected function generateParameterDeclaration(\ReflectionParameter $param) { $output = ''; if ($param->isArray()) { $output .= 'array '; } elseif (is_callable(array($param, 'isCallable')) && $param->isCallable()) { $output .= 'callable '; } elseif ($param->getClass()) { $output .= '\\' . $param->getClass()->getName() . ' '; } if ($param->isPassedByReference()) { $output .= '&'; } $output .= '$' . $param->getName(); if ($param->isDefaultValueAvailable()) { $output .= ' = ' . $param->getDefaultValue(); } return $output; }
/** * @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; } } }
/** * Collect method parameter info * * @param ReflectionParameter $parameter * @return array */ protected function _getMethodParameterInfo(ReflectionParameter $parameter) { $parameterInfo = array('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'] = $this->_escapeDefaultValue($parameter->getDefaultValue()); } elseif ($defaultValue === null) { $parameterInfo['defaultValue'] = $this->_getNullDefaultValue(); } else { $parameterInfo['defaultValue'] = $defaultValue; } } return $parameterInfo; }