Failed Conditions
Pull Request — develop (#6719)
by Marco
65:21
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
/**
13
 * @since  3.0
14
 * @author Marco Pivetta <[email protected]>
15
 *
16
 * @internal do not use in your own codebase: no BC compliance on this class
17
 */
18
final class NormalizeIdentifier
19
{
20
    /**
21
     * Given a flat identifier, this method will produce another flat identifier, but with all
22
     * association fields that are mapped as identifiers replaced by entity references, recursively.
23
     *
24
     * @throws \Doctrine\ORM\ORMException
25
     */
26
    public function __invoke(
27
        EntityManagerInterface $entityManager,
28
        ClassMetadata $targetClass,
29
        array $flatIdentifier
30
    ) : array {
31
        $normalizedAssociatedId = [];
32
33
        foreach ($targetClass->getDeclaredPropertiesIterator() as $name => $declaredProperty) {
34
            if (! \array_key_exists($name, $flatIdentifier)) {
35
                continue;
36
            }
37
38
            if ($declaredProperty instanceof FieldMetadata) {
39
                $normalizedAssociatedId[$name] = $flatIdentifier[$name];
40
41
                continue;
42
            }
43
44
            if ($declaredProperty instanceof ToOneAssociationMetadata) {
45
                $targetIdMetadata = $entityManager->getClassMetadata($declaredProperty->getTargetEntity());
46
47
                // Note: the ORM prevents using an entity with a composite identifier as an identifier association
48
                //       therefore, reset($targetIdMetadata->identifier) is always correct
49
                $normalizedAssociatedId[$name] = $entityManager->getReference(
50
                    $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...
51
                    $this->__invoke(
52
                        $entityManager,
53
                        $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...
54
                        [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...
55
                    )
56
                );
57
            }
58
        }
59
60
        return $normalizedAssociatedId;
61
    }
62
}
63