__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 9
c 1
b 0
f 1
nc 2
nop 5
dl 0
loc 19
ccs 6
cts 9
cp 0.6667
crap 2.1481
rs 9.9666
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 ReflectionClass;
16
use ReflectionType;
17
use ReflectionProperty;
18
use Doctrine\ORM\Mapping\ClassMetadata;
19
use Doctrine\DBAL\Schema\Column;
20
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicDataLoader;
21
use Doctrine\ORM\EntityManagerInterface;
22
use ReflectionObject;
23
use Webmozart\Assert\Assert;
24
use ReflectionException;
25
26
/** @see BlackMagicDataLoader */
27
final class BlackMagicColumnReflectionPropertyMock extends ReflectionProperty
28
{
29
    private EntityManagerInterface $entityManager;
30
31
    private ClassMetadata $classMetadata;
32
33
    private ReflectionClass $classMetadataReflection;
34
35
    private Column $column;
36
37
    private string $fieldName;
38
39
    private BlackMagicDataLoader $dataLoader;
40 1
41
    public function __construct(
42
        EntityManagerInterface $entityManager,
43
        ClassMetadata $classMetadata,
44
        Column $column,
45
        string $fieldName,
46
        BlackMagicDataLoader $dataLoader
47 1
    ) {
48 1
        $this->entityManager = $entityManager;
49 1
        $this->classMetadata = $classMetadata;
50 1
        $this->classMetadataReflection = new ReflectionObject($classMetadata);
51 1
        $this->column = $column;
52 1
        $this->fieldName = $fieldName;
53
        $this->dataLoader = $dataLoader;
54
55
        try {
56
            # This might fail if cache was not built yet
57
            parent::__construct($classMetadata->getName(), $fieldName);
58
59
        } catch (ReflectionException $exception) {
60
            # Do nothing
61
        }
62
    }
63
64
    public function getDeclaringClass(): ReflectionClass
65
    {
66
        Assert::notNull($this->classMetadata->reflClass, sprintf(
67
            'Tried to get declaring class of property "%s" when class-metadata reflection has not been initialized!',
68
            $this->classMetadata->name . '#' . $this->fieldName
69
        ));
70
        
71
        return $this->classMetadata->reflClass;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->classMetadata->reflClass could return the type null which is incompatible with the type-hinted return ReflectionClass. Consider adding an additional type-check to rule them out.
Loading history...
72
    }
73
74
    public function getName(): string
75
    {
76
        return $this->fieldName;
77
    }
78
79
    public function getValue(?object $object = null): mixed
80
    {
81
        return $this->dataLoader->onColumnValueRequestedFromEntity(
82
            $this->entityManager,
83
            $object,
84
            $this->column->getName()
85
        );
86
    }
87
88
    public function setValue($valueOrObject, $value = null): void
89
    {
90
        if (is_null($value)) {
91
            $this->dataLoader->onColumnValueSetOnEntity(
92
                $this->entityManager,
93
                null,
94
                $this->column->getName(),
95
                $valueOrObject
96
            );
97
98
        } else {
99
            $this->dataLoader->onColumnValueSetOnEntity(
100
                $this->entityManager,
101
                $valueOrObject,
102
                $this->column->getName(),
103
                $value
104
            );
105
        }
106
    }
107
108
    public function getDefaultValue(): mixed
109
    {
110
        return null;
111
    }
112
113
    public function getDocComment(): string|false
114
    {
115
        return false;
116
    }
117
118
    public function getModifiers(): int
119
    {
120
        return 0;
121
    }
122
123
    /** 
124
     * @psalm-suppress MissingImmutableAnnotation https://github.com/vimeo/psalm/issues/8604
125
     * @psalm-immutable 
126
     */
127
    public function getType(): ?ReflectionType
128
    {
129
        return null;
130
    }
131
132
    public function hasDefaultValue(): bool
133
    {
134
        return false;
135
    }
136
137
    public function hasType(): bool
138
    {
139
        return false;
140
    }
141
142
    public function isDefault(): bool
143
    {
144
        return false;
145
    }
146
147
    public function isInitialized($object = null): bool
148
    {
149
        return true;
150
    }
151
152
    public function isPrivate(): bool
153
    {
154
        return true;
155
    }
156
157
    public function isProtected(): bool
158
    {
159
        return false;
160
    }
161
162
    public function isPublic(): bool
163
    {
164
        return false;
165
    }
166
167
    public function isReadOnly(): bool
168
    {
169
        return false;
170
    }
171
172
    public function isStatic(): bool
173
    {
174
        return false;
175
    }
176
177
    public function setAccessible($accessible): void
178
    {
179
    }
180
181
    public function __toString(): string
182
    {
183
        return "";
184
    }
185
}
186