/** * Check if the target action is declared the given type or sub type (checking for * interfaces is also supported). * * @param string $typeName * @return boolean */ public function isResourceType($typeName) { if ($typeName instanceof \ReflectionClass) { $typeName = $typeName->name; } if (!$this->target instanceof \ReflectionMethod) { return false; } if (class_exists($typeName)) { $lookup = strtolower($typeName); $ref = $this->target->getDeclaringClass(); if (strtolower($ref->name) == $lookup) { return true; } $next = $ref; while ($next = $next->getParentClass()) { if (strtolower($next->name) == $lookup) { return true; } } return false; } if (interface_exists($typeName, false)) { return $this->target->getDeclaringClass()->implementsInterface($typeName); } return false; }
public function __construct(ReflectionFunctionAbstract $method) { $this->name = $method->getShortName(); $this->filename = $method->getFilename(); $this->startline = $method->getStartLine(); $this->endline = $method->getEndLine(); $this->docstring = $method->getDocComment(); $this->operator = $this->static ? Kint_Object::OPERATOR_STATIC : Kint_Object::OPERATOR_OBJECT; $this->access = Kint_Object::ACCESS_PUBLIC; if ($method instanceof ReflectionMethod) { $this->static = $method->isStatic(); $this->abstract = $method->isAbstract(); $this->final = $method->isFinal(); $this->owner_class = $method->getDeclaringClass()->name; if ($method->isProtected()) { $this->access = Kint_Object::ACCESS_PROTECTED; } elseif ($method->isPrivate()) { $this->access = Kint_Object::ACCESS_PRIVATE; } } foreach ($method->getParameters() as $param) { $this->parameters[] = new Kint_Object_Parameter($param); } if ($this->docstring) { if (preg_match('/@return\\s+(.*)\\r?\\n/m', $this->docstring, $matches)) { if (!empty($matches[1])) { $this->returntype = $matches[1]; } } } $docstring = new Kint_Object_Representation_Docstring($this->docstring, $this->filename, $this->startline); $docstring->implicit_label = true; $this->addRepresentation($docstring); }
/** * Helper class for getFunctionName(). * * @param \ReflectionFunctionAbstract $function * @return string */ private static function getClassName(\ReflectionFunctionAbstract $function) { if ($function instanceof \ReflectionMethod) { return $function->getDeclaringClass()->getName() . '::'; } return ''; }
/** * {@inheritDoc} */ public function getDeclaringClass() { if ($this->declaringFunction instanceof \ReflectionMethod) { return $this->declaringFunction->getDeclaringClass(); } return null; }
private function fetchFunctionIdentifier(\ReflectionFunctionAbstract $function) { $functionIdentifier = $function->getName(); if ($function instanceof \ReflectionMethod) { $functionIdentifier = sprintf('%s::%s', $function->getDeclaringClass()->getName(), $function->getName()); } return $functionIdentifier; }
/** * @param \ReflectionFunctionAbstract $reflection * * @return string */ protected static function getFunctionName(\ReflectionFunctionAbstract $reflection) { $name = $reflection->name . '()'; if ($reflection instanceof \ReflectionMethod) { $name = $reflection->getDeclaringClass()->name . '::' . $name; } return $name; }
/** * Helper method to retrieve the name of a ReflectionFunctionAbstract * @param \ReflectionFunctionAbstract $reflection * @return string */ protected function getReflectionFunctionName(\ReflectionFunctionAbstract $reflection) { // Class method if ($reflection instanceof \ReflectionMethod) { return $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName(); } return $reflection->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; }
private function getFunctionName(\ReflectionFunctionAbstract $reflectionFunction) { if ($reflectionFunction->isClosure()) { return sprintf('closure defined in %s at line %d', $reflectionFunction->getFileName(), $reflectionFunction->getStartLine()); } elseif ($reflectionFunction instanceof \ReflectionMethod) { return sprintf('%s::%s', $reflectionFunction->getDeclaringClass()->getName(), $reflectionFunction->getName()); } return $reflectionFunction->getName(); }
/** * Generates list of arguments using autowiring. * @return array */ public static function autowireArguments(\ReflectionFunctionAbstract $method, array $arguments, $container) { $optCount = 0; $num = -1; $res = array(); $methodName = ($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName() . '()'; foreach ($method->getParameters() as $num => $parameter) { if (array_key_exists($num, $arguments)) { $res[$num] = $arguments[$num]; unset($arguments[$num]); $optCount = 0; } elseif (array_key_exists($parameter->getName(), $arguments)) { $res[$num] = $arguments[$parameter->getName()]; unset($arguments[$parameter->getName()]); $optCount = 0; } elseif (($class = PhpReflection::getParameterType($parameter)) && !PhpReflection::isBuiltinType($class)) { $res[$num] = $container->getByType($class, FALSE); if ($res[$num] === NULL) { if ($parameter->allowsNull()) { $optCount++; } elseif (class_exists($class) || interface_exists($class)) { $rc = new \ReflectionClass($class); if ($class !== ($hint = $rc->getName())) { throw new ServiceCreationException("Service of type {$class} needed by {$methodName} not found, did you mean {$hint}?"); } throw new ServiceCreationException("Service of type {$class} needed by {$methodName} not found. Did you register it in configuration file?"); } else { throw new ServiceCreationException("Class {$class} needed by {$methodName} not found. Check type hint and 'use' statements."); } } else { if ($container instanceof ContainerBuilder) { $res[$num] = '@' . $res[$num]; } $optCount = 0; } } elseif ($parameter->isOptional() || $parameter->isDefaultValueAvailable()) { // !optional + defaultAvailable = func($a = NULL, $b) since 5.3.17 & 5.4.7 // optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ... $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL; $optCount++; } else { throw new ServiceCreationException("Parameter \${$parameter->getName()} in {$methodName} has no class type hint or default value, so its value must be specified."); } } // extra parameters while (array_key_exists(++$num, $arguments)) { $res[$num] = $arguments[$num]; unset($arguments[$num]); $optCount = 0; } if ($arguments) { throw new ServiceCreationException("Unable to pass specified arguments to {$methodName}."); } return $optCount ? array_slice($res, 0, -$optCount) : $res; }
/** * Creates a function location instance from the supplied reflection. * * @param \ReflectionFunctionAbstract $reflection * * @return self */ public static function fromReflection(\ReflectionFunctionAbstract $reflection) { if ($reflection instanceof \ReflectionFunction) { $namespace = $reflection->getNamespaceName(); } elseif ($reflection instanceof \ReflectionMethod) { $namespace = $reflection->getDeclaringClass()->getNamespaceName(); } else { $namespace = null; } return new self($reflection->getFileName(), $namespace, $reflection->getStartLine(), $reflection->getEndLine()); }
/** * @return string|NULL */ public static function getReturnType(\ReflectionFunctionAbstract $func) { if (PHP_VERSION_ID >= 70000 && $func->hasReturnType()) { $type = PHP_VERSION_ID >= 70100 ? $func->getReturnType()->getName() : (string) $func->getReturnType(); return strtolower($type) === 'self' ? $func->getDeclaringClass()->getName() : $type; } $type = preg_replace('#[|\\s].*#', '', (string) self::parseAnnotation($func, 'return')); if ($type) { return $func instanceof \ReflectionMethod ? self::expandClassName($type, $func->getDeclaringClass()) : ltrim($type, '\\'); } }
/** * @param \ReflectionFunctionAbstract $reflection * * @return string */ protected static function getFunctionName(\ReflectionFunctionAbstract $reflection) { if (!$reflection instanceof \ReflectionMethod) { return $reflection->name; } $class = $reflection->getDeclaringClass()->name; // see https://github.com/facebook/hhvm/issues/3874 if (0 === strpos($class, 'Closure')) { $class = 'Closure'; } return $class . '::' . $reflection->name; }
/** * Creates a function scope instance from the supplied reflection and callable. * * @param \ReflectionFunctionAbstract $reflection * @param callable $callable * * @return self */ public static function fromReflection(\ReflectionFunctionAbstract $reflection, callable $callable) { if (is_array($callable)) { /** @var $reflection \ReflectionMethod */ $thisObject = is_object($callable[0]) ? $callable[0] : null; $scopeType = $reflection->getDeclaringClass()->getName(); } elseif (is_object($callable) && !$callable instanceof \Closure) { /** @var $reflection \ReflectionMethod */ $thisObject = $callable; $scopeType = $reflection->getDeclaringClass()->getName(); } elseif ($reflection->isClosure()) { $thisObject = $reflection->getClosureThis(); $scopeClass = $reflection->getClosureScopeClass(); $scopeType = $scopeClass === null ? null : $scopeClass->getName(); } else { $thisObject = null; $scopeType = null; } $variableTable = $reflection->getStaticVariables(); return new self($thisObject, $scopeType, $variableTable); }
/** * @param \ReflectionFunctionAbstract $functionOrMethod */ private function processFunctionOrMethod(\ReflectionFunctionAbstract $functionOrMethod) { if ($functionOrMethod->isInternal()) { return; } $name = $functionOrMethod->getName(); if ($functionOrMethod instanceof \ReflectionMethod) { $name = $functionOrMethod->getDeclaringClass()->getName() . '::' . $name; } if (!isset($this->lookupTable[$functionOrMethod->getFileName()])) { $this->lookupTable[$functionOrMethod->getFileName()] = []; } foreach (range($functionOrMethod->getStartLine(), $functionOrMethod->getEndLine()) as $line) { $this->lookupTable[$functionOrMethod->getFileName()][$line] = $name; } }
/** * @param ReflectionInvocation $invocation * @param \ReflectionFunctionAbstract $function * @return bool */ protected function isTargeted(ReflectionInvocation $invocation, \ReflectionFunctionAbstract $function) { if ($function instanceof \ReflectionFunction) { return !$invocation->isMethod(); } if (empty($class = $this->classReflection($invocation->getClass()))) { //Unable to get reflection return false; } /** * @var \ReflectionMethod $function */ $target = $function->getDeclaringClass(); if ($target->isTrait()) { //Let's compare traits return in_array($target->getName(), $this->getTraits($invocation->getClass())); } return $class->getName() == $target->getName() || $class->isSubclassOf($target); }
/** * @return array */ public static function combineArgs(\ReflectionFunctionAbstract $method, $args) { $res = array(); $i = 0; foreach ($method->getParameters() as $param) { $name = $param->getName(); if (isset($args[$name])) { // NULLs are ignored $res[$i++] = $args[$name]; $type = $param->isArray() ? 'array' : ($param->isDefaultValueAvailable() ? gettype($param->getDefaultValue()) : 'NULL'); if (!self::convertType($res[$i - 1], $type)) { $mName = $method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' . $method->getName() : $method->getName(); throw new BadRequestException("Invalid value for parameter '{$name}' in method {$mName}(), expected " . ($type === 'NULL' ? 'scalar' : $type) . "."); } } else { $res[$i++] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : ($param->isArray() ? array() : null); } } return $res; }
/** * @param \ReflectionFunctionAbstract $method * @param array $args * @return array * @throws BadRequestException */ protected function combineArgs(\ReflectionFunctionAbstract $method, array $args) { $res = array(); $i = 0; foreach ($method->getParameters() as $param) { $name = $param->getName(); if (isset($args[$name])) { $res[$i++] = $args[$name]; } elseif (isset($args[$i])) { $value = $args[$i]; $res[$i++] = $value; } else { $res[$i++] = $param->isDefaultValueAvailable() && $param->isOptional() ? $param->getDefaultValue() : ($param->isArray() ? array() : NULL); } $type = $param->isArray() ? 'array' : ($param->isDefaultValueAvailable() && $param->isOptional() ? gettype($param->getDefaultValue()) : 'NULL'); if (!$this->convertType($res[$i - 1], $type)) { $mName = $method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' . $method->getName() : $method->getName(); throw new BadRequestException("Invalid value for parameter '{$name}' in method {$mName}(), expected " . ($type === 'NULL' ? 'scalar' : $type) . "."); } } return $res; }
protected function getCallbackSignature(\ReflectionFunctionAbstract $ref) { if ($ref instanceof \ReflectionMethod) { return $ref->getDeclaringClass()->name . '->' . $ref->name . '()'; } if ($ref->isClosure()) { return '*closure*'; } return $ref->name . '()'; }
/** * Get called method from abstract reflection function * * @param \ReflectionFunctionAbstract $method * @param bool $closureInfo * * @return string */ public static function getCalledMethod(\ReflectionFunctionAbstract $method, $closureInfo = true) { if ($method->isClosure()) { if ($closureInfo) { return sprintf('Closure [%s:%d]', $method->getFileName(), $method->getStartLine()); } return 'Closure'; } if ($method instanceof \ReflectionMethod) { return sprintf('%s::%s', $method->getDeclaringClass()->getName(), $method->getName()); } return $method->getName(); }
/** * Invoke. * * @acccess protected * @param \Hoa\Core\Consistency\Xcallable &$reflection Callable. * @param \ReflectionFunctionAbstract &$reflection Reflection. * @param array &$arguments Arguments. * @param bool $isConstructor Whether * it is a * constructor. * @return mixed * @throws \Exception */ protected function invoke(Core\Consistency\Xcallable &$callable, \ReflectionFunctionAbstract &$reflection, array &$arguments, $isConstructor) { if ($reflection instanceof \ReflectionFunction) { return $reflection->invokeArgs($arguments); } if (false === $isConstructor) { $_callback = $callable->getValidCallback(); $_object = $_callback[0]; return $reflection->invokeArgs($_object, $arguments); } $class = $reflection->getDeclaringClass(); $instance = $class->newInstanceArgs($arguments); $callable = xcallable($instance, '__construct'); $reflection = $callable->getReflection(); return void; }
/** * @return array */ public static function combineArgs(\ReflectionFunctionAbstract $method, $args) { $res = array(); foreach ($method->getParameters() as $i => $param) { $name = $param->getName(); list($type, $isClass) = self::getParameterType($param); if (isset($args[$name])) { $res[$i] = $args[$name]; if (!self::convertType($res[$i], $type, $isClass)) { throw new BadRequestException(sprintf('Argument $%s passed to %s() must be %s, %s given.', $name, ($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(), $type === 'NULL' ? 'scalar' : $type, is_object($args[$name]) ? get_class($args[$name]) : gettype($args[$name]))); } } elseif ($param->isDefaultValueAvailable()) { $res[$i] = $param->getDefaultValue(); } elseif ($type === 'array') { $res[$i] = array(); } elseif ($type === 'NULL' || $isClass) { $res[$i] = NULL; } else { throw new BadRequestException(sprintf('Missing parameter $%s required by %s()', $name, ($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName())); } } return $res; }
/** * @return array */ public static function combineArgs(\ReflectionFunctionAbstract $method, $args) { $res = []; $i = 0; foreach ($method->getParameters() as $param) { $name = $param->getName(); if (!isset($args[$name]) && $param->isDefaultValueAvailable()) { $res[$i++] = $param->getDefaultValue(); } else { $res[$i++] = $arg = isset($args[$name]) ? $args[$name] : NULL; list($type, $isClass) = self::getParameterType($param); if (!self::convertType($arg, $type, $isClass)) { throw new BadRequestException(sprintf('Argument $%s passed to %s() must be %s, %s given.', $name, ($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(), $type === 'NULL' ? 'scalar' : $type, is_object($arg) ? get_class($arg) : gettype($arg))); } } } return $res; }