Failed Conditions
Pull Request — master (#6392)
by Alessandro
21:55 queued 10:50
created

resolveDiscriminatorsForClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 13
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Doctrine\ORM\Utility;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Doctrine\ORM\EntityManagerInterface;
7
8
/**
9
 * @internal This class exists only to avoid code duplication, do not reuse it externally
10
 */
11
final class HierarchyDiscriminatorResolver
12
{
13
    private function __construct()
14
    {
15
    }
16
17
    /**
18
     * This method is needed to make INSTANCEOF work correctly with inheritance: if the class at hand has inheritance,
19
     * it extracts all the discriminators from the child classes and returns them
20
     */
21 14
    public static function resolveDiscriminatorsForClass(
22
        ClassMetadata $rootClassMetadata,
23
        EntityManagerInterface $entityManager
24
    ): array {
25 14
        $hierarchyClasses = $rootClassMetadata->subClasses;
0 ignored issues
show
Bug introduced by
Accessing subClasses 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...
26 14
        $hierarchyClasses[] = $rootClassMetadata->name;
0 ignored issues
show
Bug introduced by
Accessing name 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...
27
28 14
        $discriminators = [];
29
30 14
        foreach ($hierarchyClasses as $class) {
31 14
            $currentMetadata = $entityManager->getClassMetadata($class);
32 14
            $currentDiscriminator = $currentMetadata->discriminatorValue;
0 ignored issues
show
Bug introduced by
Accessing discriminatorValue 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...
33
34 14
            if (null !== $currentDiscriminator) {
35 14
                $discriminators[$currentDiscriminator] = null;
36
            }
37
        }
38
39 14
        return $discriminators;
40
    }
41
}
42