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

HierarchyDiscriminatorResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 31
ccs 10
cts 12
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolveDiscriminatorsForClass() 0 20 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