0 follower

Final Class Yiisoft\Definitions\ParameterDefinition

InheritanceYiisoft\Definitions\ParameterDefinition
ImplementsYiisoft\Definitions\Contract\DefinitionInterface

Parameter definition resolves an object based on information from ReflectionParameter instance.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( ReflectionParameter $parameter )
$parameter ReflectionParameter

                public function __construct(
    private readonly ReflectionParameter $parameter,
) {}

            
getReflection() public method

public ReflectionParameter getReflection ( )

                public function getReflection(): ReflectionParameter
{
    return $this->parameter;
}

            
hasValue() public method

public boolean hasValue ( )

                public function hasValue(): bool
{
    return $this->parameter->isDefaultValueAvailable();
}

            
isOptional() public method

public boolean isOptional ( )

                public function isOptional(): bool
{
    return $this->parameter->isOptional();
}

            
isVariadic() public method

public boolean isVariadic ( )

                public function isVariadic(): bool
{
    return $this->parameter->isVariadic();
}

            
resolve() public method

public mixed resolve ( \Psr\Container\ContainerInterface $container )
$container \Psr\Container\ContainerInterface

                public function resolve(ContainerInterface $container): mixed
{
    $type = $this->parameter->getType();
    if ($type instanceof ReflectionUnionType) {
        return $this->resolveUnionType($type, $container);
    }
    if (!$type instanceof ReflectionNamedType
        || $this->isVariadic()
        || $type->isBuiltin()
    ) {
        return $this->resolveVariadicOrBuiltinOrIntersectionOrNonTyped();
    }
    $typeName = $type->getName();
    /**
     * @psalm-suppress TypeDoesNotContainType
     * @see https://github.com/vimeo/psalm/issues/6756
     */
    if ($typeName === 'self') {
        // If type name is "self", it means that called class and
        // $parameter->getDeclaringClass() returned instance of `ReflectionClass`.
        /** @psalm-suppress PossiblyNullReference */
        $typeName = $this->parameter->getDeclaringClass()->getName();
    }
    try {
        $result = $container->get($typeName);
    } catch (Throwable $t) {
        if (
            $this->parameter->isOptional()
            && (
                $t instanceof CircularReferenceException
                || !$container->has($typeName)
            )
        ) {
            return $this->parameter->getDefaultValue();
        }
        throw $t;
    }
    if (!$result instanceof $typeName) {
        $actualType = get_debug_type($result);
        throw new InvalidConfigException(
            "Container returned incorrect type \"$actualType\" for service \"{$type->getName()}\".",
        );
    }
    return $result;
}