jeudi 18 février 2021

ReflectionParameter::getClass() deprecation

When I do a test with phpunit i get this error: Method ReflectionParameter::getClass() is deprecated, this is after the migration to php8. How to make it work again ?

documentation

the lines concerned :

$parameter->getClass()->getName()
$parameter->getClass()

Full code:

public function register(string $id): self
    {
        $reflectionClass = new ReflectionClass($id);
        if ($reflectionClass->isInterface()) {
            $this->register($this->aliases[$id]);
            $this->definitions[$id] = &$this->definitions[$this->aliases[$id]];
            return $this;
        }
        $dependencies = [];
        if (null !== $reflectionClass->getConstructor()) {
            var_dump($reflectionClass->getConstructor());
            $dependencies = array_map(
                fn (ReflectionParameter $parameter) => $this->getDefinition($parameter->getClass()->getName()),
                array_filter(
                    $reflectionClass->getConstructor()->getParameters(),
                    fn (ReflectionParameter $parameter) => $parameter->getClass()
                )
            );
        }
        $aliases = array_filter($this->aliases, fn (string $alias) => $id === $alias);
        $this->definitions[$id] = new Definition($id, true, $aliases, $dependencies);
        return $this;
    }
    public function make(ContainerInterface $container): object
    {
        $constructor = $this->class->getConstructor();
        if (null === $constructor) {
            return $this->class->newInstance();
        }
        $parameters = $constructor->getParameters();
        return $this->class->newInstanceArgs(
            array_map(function (ReflectionParameter $param) use ($container) {
                if ($param->getClass() === null) {
                    return $container->getParameter($param->getName());
                }
                return $container->get($param->getClass()->getName());
            }, $parameters)
        );
    }

I tried Something like :

$param->getType() 
$parameter->getType()->getName()
$parameter->getType()->name

Aucun commentaire:

Enregistrer un commentaire