/** * @return string|null */ public function getName() { if (empty($this->parameter)) { return null; } return $this->parameter->getName(); }
/** * Helper method to get the type of a reflection paramter * @param \ReflectionParameter $parameter * @return NULL|\ReflectionType|string */ protected function getReflectionParameterName(\ReflectionParameter $parameter) { // parameter is a class if ($class = $parameter->getClass()) { return $class->getName() . ' \\$' . $parameter->getName(); } return $parameter->getName(); }
/** * Generate key for parameter * * @param \ReflectionParameter $parameter * @param \ReflectionFunctionAbstract $method * * @return string */ public static function generateForParameter(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $method) { if ($method instanceof \ReflectionMethod) { $key = $method->getDeclaringClass()->getName() . '::' . $method->getName() . ':' . $parameter->getName(); } else { $key = 'function::' . $method->getName() . ':' . $parameter->getName(); } return $key; }
/** * @param \ReflectionParameter $class * @return mixed|object * @throws \Exception */ public function injectByClass($class) { $reflection = $class->getClass(); if (is_null($reflection)) { return HttpParameter::getInstance()->getOrError($class->getName()); } else { if ($reflection->getName() === Option::class) { return HttpParameter::getInstance()->get($class->getName()); } } return $this->create($reflection); }
public function exportCode() { $default_value = null; if ($this->_parameter->isDefaultValueAvailable()) { $default_value = $this->_parameter->getDefaultValue(); if (is_scalar($default_value) && !is_numeric($default_value)) { $default_value = "'{$default_value}'"; } } elseif ($this->_parameter->isOptional()) { $default_value = 'NULL'; } return sprintf('%s%s$%s%s', $this->_parameter->getClass() ? "{$this->_parameter->getClass()->getName()} " : '', $this->_parameter->isPassedByReference() ? '&' : '', $this->_parameter->getName(), $default_value ? " = {$default_value}" : ''); }
/** * @return self */ public static function from(\ReflectionParameter $from) { $param = new static(); $param->name = $from->getName(); $param->reference = $from->isPassedByReference(); if ($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; $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 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(); }
/** * @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); } }
/** * @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); } }
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; }
/** * @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; }
/** * @param \ReflectionParameter $parameter * @return mixed */ private function getArgumentMock(\ReflectionParameter $parameter) { if ($parameter->getClass() !== null) { return $this->testCase->getMockBuilder($parameter->getClass()->getName())->disableOriginalConstructor()->getMock(); } throw new \Mocktainer\UnmockableMethodArgumentException($parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $parameter->getName()); }
/** * Returns this parameters's name * @return string */ public function getName() { if ($this->parameter != null) { return $this->parameter->getName(); } else { return parent::getName(); } }
/** * {@inheritdoc} */ public function getParameterValue(\ReflectionParameter $parameter) { $name = $parameter->getName(); if (isset($this->values[$name])) { return $this->values[$name]; } return $this->fallback->getParameterValue($parameter); }
/** * @param \ReflectionParameter $parameter * @return mixed */ private function loadParameter(\ReflectionParameter $parameter) { $class = $parameter->getClass(); if (is_null($class)) { throw new \Exception('Constructor parameter must be autoloaded but missing parameter type for parameter ' . $parameter->getName()); } return $this->getSkeleton()->get($class->getName()); }
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; }
/** * @load * @param ReflectionParameter $reflection */ private function _createFromReflection($reflection) { $this->_name = $reflection->getName(); if ($reflection->isDefaultValueAvailable()) { $this->_default = true; $this->_value = $reflection->getDefaultValue(); } }
protected function getResolverFor($annotations, \ReflectionParameter $parameter) { foreach ($annotations as $annotation) { if ($annotation instanceof Governor\Resolve && ($annotation->parameter = $parameter->getName())) { return $annotation->resolver; } } return null; }
/** * @param Horde_Injector $injector * @param ReflectionParameter $method * * @return mixed * @throws Horde_Injector_Exception */ public function getParameterDependency(Horde_Injector $injector, ReflectionParameter $parameter) { if ($parameter->getClass()) { return $injector->getInstance($parameter->getClass()->getName()); } elseif ($parameter->isOptional()) { return $parameter->getDefaultValue(); } throw new Horde_Injector_Exception("Untyped parameter \$" . $parameter->getName() . "can't be fulfilled"); }
protected static function getParameterTypeFromDoc(\ReflectionParameter $parameter) { $constructor = $parameter->getDeclaringFunction(); $docComment = $constructor->getDocComment(); if (!preg_match('#@inject(?<target>.+)\\$' . $parameter->getName() . '#', $docComment, $match)) { return null; } return str_replace(' ', '', $match['target']); }
private function getParameterInfo(\ReflectionParameter $parameter) { $paramName = '$' . $parameter->getName() . '=null'; if ($parameter->isPassedByReference()) { $paramName = '&' . $paramName; } $paramType = $parameter->getClass() !== null ? $parameter->getClass()->getName() : ''; return "{$paramType} {$paramName}"; }
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; }
/** * {@inheritdoc} */ public function getParameterValue(\ReflectionParameter $parameter) { $class = $parameter->getClass(); if ($class && $class->getName() == Request::class) { return $this->request; } $name = $parameter->getName(); if (isset($this->parameters[$name])) { return $this->parameters[$name]; } return $this->fallbackResolver->getParameterValue($parameter); }
public function testWrappedMethods() { $php_parameter = new \ReflectionParameter([$this, 'method'], 'param'); $our_parameter = new ReflectionParameter($php_parameter); $this->assertSame($php_parameter->getName(), $our_parameter->getName()); $this->assertSame($php_parameter->allowsNull(), $our_parameter->allowsNull()); $this->assertSame($php_parameter->isOptional(), $our_parameter->isOptional()); $this->assertSame($php_parameter->isDefaultValueAvailable(), $our_parameter->isDefaultValueAvailable()); $this->assertSame($php_parameter->isVariadic(), $our_parameter->isVariadic()); $this->assertSame($php_parameter->isPassedByReference(), $our_parameter->isPassedByReference()); $this->assertSame($php_parameter->getDefaultValue(), $our_parameter->getDefaultValue()); }
/** * 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; }
protected function getParam(\ReflectionParameter $param) { $name = $param->getName(); if (!isset($this->args['params'][$name]) && !$param->isOptional()) { $message = sprintf('You must define param "%s" for "%s"', $name, $this->args['class']); throw new \OutOfBoundsException($message); } if (!isset($this->args['params'][$name]) && $param->isOptional()) { return $param->getDefaultValue(); } $value = $this->args['params'][$name]; $param = is_string($value) && defined($value) ? constant($value) : $value; return $param; }
/** * @param \ReflectionParameter $reflectionParameter * @param array $args * @param bool $singleton * @return mixed * @throws \Exception */ public function getReflectionValue(\ReflectionParameter $reflectionParameter, array $args = array(), $singleton = true) { if (isset($args[$reflectionParameter->getName()])) { return $args[$reflectionParameter->getName()]; } elseif ($reflectionParameter->isOptional()) { return $reflectionParameter->getDefaultValue(); } else { if (!is_null($reflectionParameter->getClass())) { $className = $reflectionParameter->getClass()->getName(); if ($singleton === true) { return $this->dic->getSingleton($className); } else { return $this->dic->getInstance($className); } } else { if ($singleton === true) { return $this->dic->getSingleton($reflectionParameter->getName()); } else { return $this->dic->getInstance($reflectionParameter->getName()); } } } }
/** * @param \ReflectionParameter $parameter * @param string $description */ public function addParameter(\ReflectionParameter $parameter, $description) { $name = $parameter->getName(); if (!$parameter->isOptional()) { $this->addArgument($name, InputArgument::REQUIRED, $description); return; } if ($this->isFlagOption($parameter)) { $this->addOption($name, null, InputOption::VALUE_NONE, $description); return; } $default = $parameter->getDefaultValue(); $this->addArgument($name, InputArgument::OPTIONAL, $description, $default); }
/** * @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; }