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 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; }
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; }
/** * {@inheritdoc} */ public function getParameterValue(\ReflectionParameter $parameter) { if ($parameter->isDefaultValueAvailable()) { return $parameter->getDefaultValue(); } throw UnresolvedValueException::unresolvedParameter($parameter); }
/** * @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; }
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; }
/** * @return string|null */ public function getName() { if (empty($this->parameter)) { return null; } return $this->parameter->getName(); }
function test($param) { $r = new ReflectionParameter('params', $param); var_dump($r->getDefaultValue()); var_dump($r->getDefaultValueText()); var_dump($r->getDefaultValueConstantName()); }
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); }
/** * @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; }
public function __construct(\ReflectionParameter $parameter) { if (!$parameter->isCallable()) { throw new \InvalidArgumentException('Provided parameter should have a callable type hint'); } parent::__construct($parameter); }
/** * @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); } }
/** * @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); }
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; }
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(); }
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; }
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 ReflectionParameter $parameter * @return mixed * @throws Exception */ public function resolveNonClass($parameter) { // 有默认值则返回默认值 if ($parameter->isDefaultValueAvailable()) { return $parameter->getDefaultValue(); } throw new Exception('I have no idea what to do here.'); }
private function getDefaultValue(\ReflectionParameter $parameter) { $defaultValue = null; if ($parameter->isDefaultValueAvailable()) { $defaultValue = $parameter->getDefaultValue(); } return $defaultValue; }
private function isGoutte1() { $refl = new \ReflectionParameter(array('Goutte\\Client', 'setClient'), 0); if ($refl->getClass() && 'Guzzle\\Http\\ClientInterface' === $refl->getClass()->getName()) { return true; } return false; }
/** * {@inheritdoc} */ public function getParameterValue(\ReflectionParameter $parameter) { $name = $parameter->getName(); if (isset($this->values[$name])) { return $this->values[$name]; } return $this->fallback->getParameterValue($parameter); }
/** * Returns true if the given controller parameter is type-hinted as an instance of ScopeFetcher. * * @param \ReflectionParameter $controllerParam A parameter of the controller action. * * @return bool */ private function isScopeFetcherType(\ReflectionParameter $controllerParam) { $class = $controllerParam->getClass(); if (!$class) { return false; } return $class->implementsInterface(self::SCOPE_FETCHER_INTERFACE); }
/** * 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(); }
/** * @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()); }
function it_resolves_an_argument_if_it_is_an_element_type(Factory $factory, \ReflectionClass $class, \ReflectionMethod $constructor, \ReflectionParameter $anyParameter, \ReflectionParameter $elementParameter, \ReflectionClass $elementParameterClass, MyElement $element) { $constructor->getParameters()->willReturn(array($anyParameter, $elementParameter)); $elementParameter->getClass()->willReturn($elementParameterClass); $elementParameterClass->getName()->willReturn('spec\\SensioLabs\\Behat\\PageObjectExtension\\Context\\Argument\\MyElement'); $factory->instantiate('spec\\SensioLabs\\Behat\\PageObjectExtension\\Context\\Argument\\MyElement')->willReturn($element); $this->resolveArguments($class, array())->shouldReturn(array(1 => $element)); }
/** * @load * @param ReflectionParameter $reflection */ private function _createFromReflection($reflection) { $this->_name = $reflection->getName(); if ($reflection->isDefaultValueAvailable()) { $this->_default = true; $this->_value = $reflection->getDefaultValue(); } }
function it_generates_correct_message_based_on_function_and_parameter(\ReflectionParameter $parameter, \ReflectionMethod $function, \ReflectionClass $class) { $parameter->getPosition()->willReturn(2); $function->getDeclaringClass()->willReturn($class); $class->getName()->willReturn('Acme\\Foo'); $function->getName()->willReturn('bar'); $this->getMessage()->shouldStartWith('Collaborator must be an object: argument 2 defined in Acme\\Foo::bar.'); }
public static function tryCreate(\ReflectionParameter $parameter, $overridenValue) { $parameterClass = $parameter->getClass(); if (!$parameterClass) { return null; } $target = $overridenValue instanceof NParameter ? $overridenValue : '\\' . $parameterClass->getName(); return new TypedParameter($target, $parameter, DirectTransformer::def()); }
public function __construct(\ReflectionParameter $parameter) { if ($parameter->getClass() === null) { throw new \InvalidArgumentException('Provided parameter should have a class type hint'); } parent::__construct($parameter); $this->class_name = $parameter->getClass()->getName(); $this->short_name = $parameter->getClass()->getShortName(); }
/** * 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; }