Failed Conditions
Pull Request — develop (#6719)
by Marco
65:26
created

NormalizeIdentifier::__invoke()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 5
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Utility;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\Mapping\FieldMetadata;
10
use Doctrine\ORM\Mapping\ToOneAssociationMetadata;
11
12
final class NormalizeIdentifier
13
{
14
    /**
15
     * Given a flat identifier, this method will produce another flat identifier, but with all
16
     * association fields that are mapped as identifiers replaced by entity references, recursively.
17
     *
18
     * @throws \Doctrine\ORM\ORMException
19
     */
20
    public function __invoke(
21
        EntityManagerInterface $entityManager,
22
        ClassMetadata $targetClass,
23
        array $flatIdentifier
24
    ) : array {
25
        $normalizedAssociatedId = [];
26
27
        foreach ($targetClass->getDeclaredPropertiesIterator() as $name => $declaredProperty) {
28
            if (! \array_key_exists($name, $flatIdentifier)) {
29
                continue;
30
            }
31
32
            if ($declaredProperty instanceof FieldMetadata) {
33
                $normalizedAssociatedId[$name] = $flatIdentifier[$name];
34
35
                continue;
36
            }
37
38
            if ($declaredProperty instanceof ToOneAssociationMetadata) {
39
                $targetIdMetadata = $entityManager->getClassMetadata($declaredProperty->getTargetEntity());
40
41
                // Note: the ORM prevents using an entity with a composite identifier as an identifier association
42
                //       therefore, reset($targetIdMetadata->identifier) is always correct
43
                $normalizedAssociatedId[$name] = $entityManager->getReference(
44
                    $targetIdMetadata->getClassName(),
0 ignored issues
show
Bug introduced by
The method getClassName() does not seem to exist on object<Doctrine\Common\P...\Mapping\ClassMetadata>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
                    $this->__invoke(
46
                        $entityManager,
47
                        $targetIdMetadata,
0 ignored issues
show
Documentation introduced by
$targetIdMetadata is of type object<Doctrine\Common\P...\Mapping\ClassMetadata>, but the function expects a object<Doctrine\ORM\Mapping\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
                        [reset($targetIdMetadata->identifier) => $flatIdentifier[$name]]
0 ignored issues
show
Bug introduced by
Accessing identifier on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49
                    )
50
                );
51
            }
52
        }
53
54
        return $normalizedAssociatedId;
55
    }
56
}
57