Passed
Push — master ( 50749f...7b5f02 )
by Gerrit
12:36
created

BlackMagicReflectionServiceDecorator::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Copyright (C) 2019 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\RDMBundle\DataLoader\BlackMagic;
14
15
use ReflectionProperty;
16
use Doctrine\DBAL\Schema\Column;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicDataLoader;
19
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
20
use ReflectionException;
21
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
22
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicColumnReflectionPropertyMock;
23
use Webmozart\Assert\Assert;
24
use Doctrine\Persistence\Mapping\ReflectionService;
25
26
final class BlackMagicReflectionServiceDecorator implements ReflectionService
27
{
28
29
    /** @var MappingDriverInterface */
30
    private $mappingDriver;
31
32
    /** @var ReflectionService */
33
    private $innerReflectionService;
34
35
    /** @var EntityManagerInterface */
36
    private $entityManager;
37
38
    /** @var BlackMagicDataLoader */
39
    private $dataLoader;
40
41 1
    public function __construct(
42
        ReflectionService $innerReflectionService,
43
        MappingDriverInterface $mappingDriver,
44
        EntityManagerInterface $entityManager,
45
        BlackMagicDataLoader $dataLoader
46
    ) {
47 1
        $this->innerReflectionService = $innerReflectionService;
48 1
        $this->mappingDriver = $mappingDriver;
49 1
        $this->entityManager = $entityManager;
50 1
        $this->dataLoader = $dataLoader;
51 1
    }
52
53
    public function getParentClasses($class)
54
    {
55
        return $this->innerReflectionService->getParentClasses($class);
56
    }
57
58
    public function getClassShortName($class)
59
    {
60
        return $this->innerReflectionService->getClassShortName($class);
61
    }
62
63
    public function getClassNamespace($class)
64
    {
65
        return $this->innerReflectionService->getClassNamespace($class);
66
    }
67
68
    public function getClass($class)
69
    {
70
        return $this->innerReflectionService->getClass($class);
71
    }
72
73
    public function getAccessibleProperty($class, $property)
74
    {
75
        /** @var ReflectionProperty|null $propertyReflection */
76
        $propertyReflection = null;
77
78
        try {
79
            $propertyReflection = $this->innerReflectionService->getAccessibleProperty($class, $property);
80
81
        } catch (ReflectionException $exception) {
82
            Assert::classExists($class);
83
84
            /** @var EntityMappingInterface|null $entityMapping */
85
            $entityMapping = $this->mappingDriver->loadRDMMetadataForClass($class);
86
87
            if ($entityMapping instanceof EntityMappingInterface) {
88
                /** @var array<Column> $columns */
89
                $columns = $entityMapping->collectDBALColumns();
90
91
                /** @var Column $column */
92
                foreach ($columns as $column) {
93
                    /** @var string $fieldName */
94
                    $fieldName = $this->dataLoader->columnToFieldName($column);
95
96
                    if ($property === $fieldName) {
97
                        $propertyReflection = new BlackMagicColumnReflectionPropertyMock(
98
                            $this->entityManager,
99
                            $this->entityManager->getClassMetadata($class),
0 ignored issues
show
Compatibility introduced by
$this->entityManager->getClassMetadata($class) of type object<Doctrine\Persiste...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ORM\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
100
                            $column,
101
                            $fieldName,
102
                            $this->dataLoader
103
                        );
104
                        break;
105
                    }
106
                }
107
            }
108
        }
109
110
        return $propertyReflection;
111
    }
112
113
    public function hasPublicMethod($class, $method)
114
    {
115
        return $this->innerReflectionService->hasPublicMethod($class, $method);
116
    }
117
}
118