Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReflectionHydrator is hard to extend due to the use of self::. #113

Open
ianef opened this issue Sep 27, 2023 · 0 comments
Open

ReflectionHydrator is hard to extend due to the use of self::. #113

ianef opened this issue Sep 27, 2023 · 0 comments
Labels
Bug Something isn't working

Comments

@ianef
Copy link

ianef commented Sep 27, 2023

Bug Report

I have written a hydrator that hydrates Doctrine entities from arrays and the Request object. It also handles associations within the entity, and nested associations within that. It uses Reflection to determine the target type from property types, type hints, and @var annotations.

When hydrating associations, Doctrine returns a Proxy class which inherits from the entity class we are working with. The problem is that the ReflectionHydrator uses ReflectionClass to obtain the properties of the object, but this does not return the properties of the base entity class which are marked as private, just the immediate properties of the Proxy class.

I have tried to extend ReflectionHydrator and override getReflProperties() so that I can use the parent class reflection for proxy entities, but that does not work as calls to getReflProperties are made using the self operator.

I am proposing replacing the use of self:: with static:: in method calls so that extended methods will use the overridden getReflProperties method I need to customise.

Q A
Version(s) 4.5.0 - 4.14.0

Summary

Extending ReflectionHydrator to override getReflProperties for custom ReflectionClass handling does not work as expected because calls to this method within ReflectionHydrator are made using self::.

Current behavior

Code in the extended ReflectionHydrator ::getReflProperties method is not called from the hydrate() or extract() methods.

Expected behavior

Code in the extended ReflectionHydrator ::getReflProperties method should be called from the hydrate() and extract() methods.

Suggested fix

    /**
     * Extract values from an object
     *
     * {@inheritDoc}
     */
    public function extract(object $object): array
    {
        $result = [];
        foreach (static::getReflProperties($object) as $property) {
            $propertyName = $this->extractName($property->getName(), $object);
            if (! $this->getCompositeFilter()->filter($propertyName)) {
                continue;
            }

            $value                 = $property->getValue($object);
            $result[$propertyName] = $this->extractValue($propertyName, $value, $object);
        }

        return $result;
    }

    /**
     * Hydrate $object with the provided $data.
     *
     * {@inheritDoc}
     */
    public function hydrate(array $data, object $object)
    {
        $reflProperties = static::getReflProperties($object);
        foreach ($data as $key => $value) {
            $name = $this->hydrateName($key, $data);
            if (isset($reflProperties[$name])) {
                $reflProperties[$name]->setValue($object, $this->hydrateValue($name, $value, $data));
            }
        }
        return $object;
    }
@ianef ianef added the Bug Something isn't working label Sep 27, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 1, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 1, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 1, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 1, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 3, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 3, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 3, 2023
Bug fix for issue laminas#113 - Use static instead of self.
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 10, 2023
ianef added a commit to ianef/laminas-hydrator that referenced this issue Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant