EasyAdminExtensionBundle icon indicating copy to clipboard operation
EasyAdminExtensionBundle copied to clipboard

[Insight] Object parameters should be type hinted - in src/Helper/EmbeddedListHelper.php, line 134

Open alterphp opened this issue 6 years ago • 1 comments

Violation reported by SensioLabInsight. We call getId method on unknwon object. => method may not exist.

Is it possible to get identifiers without parsing each of related object (from collection ?) ?

@rimi-itk any idea ?

in src/Helper/EmbeddedListHelper.php, line 134

The parameter entity, which is an object, should be typehinted.

                // ManyToMany association
                elseif (isset($assoc['joinTable'])) {
                    $relatedItems = PropertyAccess::createPropertyAccessor()->getValue(
                        $parentEntity, $parentEntityProperty
                    );
                    $itemIds = $relatedItems->map(function ($entity) {
                        return $entity->getId();
                    });

                    return ['entity.id' => $itemIds->toArray()];
                }

Posted from SensioLabsInsight

alterphp avatar Aug 29 '18 13:08 alterphp

@alterphp, I'm not sure if/how we can get ids without getting (full) objects from the collection, but we should definitely clean up the code and not make assumptions on the existence of the getId method:

$fieldName = $assoc['joinTable']['joinColumns'][0]['referencedColumnName'];
$assocFieldPart = 'entity.'.$fieldName;
$accessor = PropertyAccess::createPropertyAccessor();
$relatedItems = $accessor->getValue(
    $parentEntity, $parentEntityProperty
);
$itemIds = $relatedItems->map(function ($entity) use ($accessor, $fieldName) {
    return $accessor->getValue(
        $entity,
        $fieldName
    );
});

return [$assocFieldPart => $itemIds->toArray()];

This matches the code in the OneToMany part which makes perfect sense.

In a very old post on https://groups.google.com/forum/m/#!topic/symfony2/BVgT2vmLbto Christophe Coevoet (stof) claims that ”Getting the identifier will not initialize the proxy anymore in 2.2”, but I have not been able to verify this. I will investigate further.

rimi-itk avatar Aug 30 '18 07:08 rimi-itk