isDefaultValueConstant()) {
$const = $orig = $param->getDefaultValueConstantName();
$pair = \explode('::', $const);
if (isset($pair[1])) {
$pair[0] = Type::resolve($pair[0], $param);
try {
$rcc = new \ReflectionClassConstant($pair[0], $pair[1]);
} catch (\ReflectionException $e) {
$name = self::toString($param);
throw new \ReflectionException("Unable to resolve constant {$orig} used as default value of {$name}.", 0, $e);
}
return $rcc->getValue();
} elseif (!\defined($const)) {
$const = \substr((string) \strrchr($const, '\\'), 1);
if (!\defined($const)) {
$name = self::toString($param);
throw new \ReflectionException("Unable to resolve constant {$orig} used as default value of {$name}.");
}
}
return \constant($const);
}
return $param->getDefaultValue();
}
/**
* Returns a reflection of a class or trait that contains a declaration of given property. Property can also be declared in the trait.
*/
public static function getPropertyDeclaringClass(\ReflectionProperty $prop) : \ReflectionClass
{
foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
if ($trait->hasProperty($prop->name) && $trait->getProperty($prop->name)->getDocComment() === $prop->getDocComment()) {
return self::getPropertyDeclaringClass($trait->getProperty($prop->name));
}
}
return $prop->getDeclaringClass();
}
/**
* Returns a reflection of a method that contains a declaration of $method.
* Usually, each method is its own declaration, but the body of the method can also be in the trait and under a different name.
*/
public static function getMethodDeclaringMethod(\ReflectionMethod $method) : \ReflectionMethod
{
// file & line guessing as workaround for insufficient PHP reflection
$decl = $method->getDeclaringClass();
if ($decl->getFileName() === $method->getFileName() && $decl->getStartLine() <= $method->getStartLine() && $decl->getEndLine() >= $method->getEndLine()) {
return $method;
}
$hash = [$method->getFileName(), $method->getStartLine(), $method->getEndLine()];
if (($alias = $decl->getTraitAliases()[$method->name] ?? null) && ($m = new \ReflectionMethod(...\explode('::', $alias, 2))) && $hash === [$m->getFileName(), $m->getStartLine(), $m->getEndLine()]) {
return self::getMethodDeclaringMethod($m);
}
foreach ($decl->getTraits() as $trait) {
if ($trait->hasMethod($method->name) && ($m = $trait->getMethod($method->name)) && $hash === [$m->getFileName(), $m->getStartLine(), $m->getEndLine()]) {
return self::getMethodDeclaringMethod($m);
}
}
return $method;
}
/**
* Finds out if reflection has access to PHPdoc comments. Comments may not be available due to the opcode cache.
*/
public static function areCommentsAvailable() : bool
{
static $res;
return $res ?? ($res = (bool) (new \ReflectionMethod(self::class, __FUNCTION__))->getDocComment());
}
public static function toString(\Reflector $ref) : string
{
if ($ref instanceof \ReflectionClass) {
return $ref->name;
} elseif ($ref instanceof \ReflectionMethod) {
return $ref->getDeclaringClass()->name . '::' . $ref->name . '()';
} elseif ($ref instanceof \ReflectionFunction) {
return \PHP_VERSION_ID >= 80200 && $ref->isAnonymous() ? '{closure}()' : $ref->name . '()';
} elseif ($ref instanceof \ReflectionProperty) {
return self::getPropertyDeclaringClass($ref)->name . '::$' . $ref->name;
} elseif ($ref instanceof \ReflectionParameter) {
return '$' . $ref->name . ' in ' . self::toString($ref->getDeclaringFunction());
} else {
throw new Nette\InvalidArgumentException();
}
}
/**
* Expands the name of the class to full name in the given context of given class.
* Thus, it returns how the PHP parser would understand $name if it were written in the body of the class $context.
* @throws Nette\InvalidArgumentException
*/
public static function expandClassName(string $name, \ReflectionClass $context) : string
{
$lower = \strtolower($name);
if (empty($name)) {
throw new Nette\InvalidArgumentException('Class name must not be empty.');
} elseif (Validators::isBuiltinType($lower)) {
return $lower;
} elseif ($lower === 'self' || $lower === 'static') {
return $context->name;
} elseif ($lower === 'parent') {
return $context->getParentClass() ? $context->getParentClass()->name : 'parent';
} elseif ($name[0] === '\\') {
// fully qualified name
return \ltrim($name, '\\');
}
$uses = self::getUseStatements($context);
$parts = \explode('\\', $name, 2);
if (isset($uses[$parts[0]])) {
$parts[0] = $uses[$parts[0]];
return \implode('\\', $parts);
} elseif ($context->inNamespace()) {
return $context->getNamespaceName() . '\\' . $name;
} else {
return $name;
}
}
/** @return array