Failed Conditions
Pull Request — master (#6392)
by Alessandro
19:58
created

HierarchyDiscriminatorResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
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 13
    public static function resolveDiscriminatorsForClass(
22
        ClassMetadata $rootClassMetadata,
23
        EntityManagerInterface $entityManager
24
    ): array {
25 13
        $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 13
        $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 13
        $discriminators = [];
29
30 13
        foreach ($hierarchyClasses as $class) {
31 13
            $currentMetadata = $entityManager->getClassMetadata($class);
32 13
            $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 13
            if (null !== $currentDiscriminator) {
35 13
                $discriminators[$currentDiscriminator] = null;
36
            }
37
        }
38
39 13
        return $discriminators;
40
    }
41
}
42