Passed
Push — master ( 064bed...d815ea )
by Gerrit
04:18
created

BlackMagicColumnReflectionPropertyMock::getName()   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 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
25
/** @see BlackMagicDataLoader */
26
final class BlackMagicColumnReflectionPropertyMock extends ReflectionProperty
27
{
28
    private EntityManagerInterface $entityManager;
29
30
    private ClassMetadata $classMetadata;
31
32
    private ReflectionClass $classMetadataReflection;
33
34
    private Column $column;
35
36
    private string $fieldName;
37
38
    private BlackMagicDataLoader $dataLoader;
39
40 1
    public function __construct(
41
        EntityManagerInterface $entityManager,
42
        ClassMetadata $classMetadata,
43
        Column $column,
44
        string $fieldName,
45
        BlackMagicDataLoader $dataLoader
46
    ) {
47 1
        $this->entityManager = $entityManager;
48 1
        $this->classMetadata = $classMetadata;
49 1
        $this->classMetadataReflection = new ReflectionObject($classMetadata);
50 1
        $this->column = $column;
51 1
        $this->fieldName = $fieldName;
52 1
        $this->dataLoader = $dataLoader;
53
    }
54
55
    public function getDeclaringClass(): ReflectionClass
56
    {
57
        Assert::notNull($this->classMetadata->reflClass, sprintf(
58
            'Tried to get declaring class of property "%s" when class-metadata reflection has not been initialized!',
59
            $this->classMetadata->name . '#' . $this->fieldName
60
        ));
61
        
62
        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...
63
    }
64
65
    public function getName(): string
66
    {
67
        return $this->fieldName;
68
    }
69
70
    public function getValue(?object $object = null): mixed
71
    {
72
        return $this->dataLoader->onColumnValueRequestedFromEntity(
73
            $this->entityManager,
74
            $object,
75
            $this->column->getName()
76
        );
77
    }
78
79
    public function setValue($valueOrObject, $value = null): void
80
    {
81
        if (is_null($value)) {
82
            $this->dataLoader->onColumnValueSetOnEntity(
83
                $this->entityManager,
84
                null,
85
                $this->column->getName(),
86
                $valueOrObject
87
            );
88
89
        } else {
90
            $this->dataLoader->onColumnValueSetOnEntity(
91
                $this->entityManager,
92
                $valueOrObject,
93
                $this->column->getName(),
94
                $value
95
            );
96
        }
97
    }
98
99
    public function getDefaultValue(): mixed
100
    {
101
        return null;
102
    }
103
104
    public function getDocComment(): string|false
105
    {
106
        return false;
107
    }
108
109
    public function getModifiers(): int
110
    {
111
        return 0;
112
    }
113
114
    /** 
115
     * @psalm-suppress MissingImmutableAnnotation https://github.com/vimeo/psalm/issues/8604
116
     * @psalm-immutable 
117
     */
118
    public function getType(): ?ReflectionType
119
    {
120
        return null;
121
    }
122
123
    public function hasDefaultValue(): bool
124
    {
125
        return false;
126
    }
127
128
    public function hasType(): bool
129
    {
130
        return false;
131
    }
132
133
    public function isDefault(): bool
134
    {
135
        return false;
136
    }
137
138
    public function isInitialized($object = null): bool
139
    {
140
        return true;
141
    }
142
143
    public function isPrivate(): bool
144
    {
145
        return true;
146
    }
147
148
    public function isProtected(): bool
149
    {
150
        return false;
151
    }
152
153
    public function isPublic(): bool
154
    {
155
        return false;
156
    }
157
158
    public function isReadOnly(): bool
159
    {
160
        return false;
161
    }
162
163
    public function isStatic(): bool
164
    {
165
        return false;
166
    }
167
168
    public function setAccessible($accessible): void
169
    {
170
    }
171
172
    public function __toString(): string
173
    {
174
        return "";
175
    }
176
}
177