Failed Conditions
Push — master ( b07393...21bc80 )
by Guilherme
09:49
created

PropertyMetadataBuilder::__construct()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 7
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping\Builder;
6
7
use Doctrine\ORM\Annotation;
8
use Doctrine\ORM\Mapping;
9
use function assert;
10
11
class PropertyMetadataBuilder
12
{
13
    /** @var Mapping\ClassMetadataBuildingContext */
14
    private $metadataBuildingContext;
15
16
    /** @var FieldMetadataBuilder */
17
    private $fieldMetadataBuilder;
18
19
    /** @var OneToOneAssociationMetadataBuilder */
20
    private $oneToOneAssociationMetadataBuilder;
21
22
    /** @var ManyToOneAssociationMetadataBuilder */
23
    private $manyToOneAssociationMetadataBuilder;
24
25
    /** @var OneToManyAssociationMetadataBuilder */
26
    private $oneToManyAssociationMetadataBuilder;
27
28
    /** @var ManyToManyAssociationMetadataBuilder */
29
    private $manyToManyAssociationMetadataBuilder;
30
31
    /** @var TransientMetadataBuilder */
32
    private $transientMetadataBuilder;
33
34
    /** @var Mapping\ClassMetadata */
35
    private $componentMetadata;
36
37
    /** @var string */
38
    private $fieldName;
39
40
    /** @var Annotation\Id|null */
41
    private $idAnnotation;
42
43
    /** @var Annotation\Version|null */
44
    private $versionAnnotation;
45
46
    /** @var Annotation\Cache|null */
47
    private $cacheAnnotation;
48
49
    /** @var Annotation\Column|null */
50
    private $columnAnnotation;
51
52
    /** @var Annotation\Embedded|null */
53
    private $embeddedAnnotation;
54
55
    /** @var Annotation\OneToOne|null */
56
    private $oneToOneAnnotation;
57
58
    /** @var Annotation\ManyToOne|null */
59
    private $manyToOneAnnotation;
60
61
    /** @var Annotation\OneToMany|null */
62
    private $oneToManyAnnotation;
63
64
    /** @var Annotation\ManyToMany|null */
65
    private $manyToManyAnnotation;
66
67
    /** @var Annotation\JoinTable|null */
68
    private $joinTableAnnotation;
69
70
    /** @var Annotation\JoinColumns|null */
71
    private $joinColumnsAnnotation;
72
73
    /** @var Annotation\JoinColumn|null */
74
    private $joinColumnAnnotation;
75
76
    /** @var Annotation\OrderBy|null */
77
    private $orderByAnnotation;
78
79
    /** @var Annotation\GeneratedValue|null */
80
    private $generatedValueAnnotation;
81
82
    /** @var Annotation\SequenceGenerator|null */
83
    private $sequenceGeneratorAnnotation;
84
85
    /** @var Annotation\CustomIdGenerator|null */
86
    private $customIdGeneratorAnnotation;
87
88 407
    public function __construct(
89
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext,
90
        ?FieldMetadataBuilder $fieldMetadataBuilder = null,
91
        ?OneToOneAssociationMetadataBuilder $oneToOneAssociationMetadataBuilder = null,
92
        ?ManyToOneAssociationMetadataBuilder $manyToOneAssociationMetadataBuilder = null,
93
        ?OneToManyAssociationMetadataBuilder $oneToManyAssociationMetadataBuilder = null,
94
        ?ManyToManyAssociationMetadataBuilder $manyToManyAssociationMetadataBuilder = null,
95
        ?TransientMetadataBuilder $transientMetadataBuilder = null
96
    ) {
97 407
        $this->metadataBuildingContext              = $metadataBuildingContext;
98 407
        $this->fieldMetadataBuilder                 = $fieldMetadataBuilder ?: new FieldMetadataBuilder($metadataBuildingContext);
99 407
        $this->oneToOneAssociationMetadataBuilder   = $oneToOneAssociationMetadataBuilder ?: new OneToOneAssociationMetadataBuilder($metadataBuildingContext);
100 407
        $this->manyToOneAssociationMetadataBuilder  = $manyToOneAssociationMetadataBuilder ?: new ManyToOneAssociationMetadataBuilder($metadataBuildingContext);
101 407
        $this->oneToManyAssociationMetadataBuilder  = $oneToManyAssociationMetadataBuilder ?: new OneToManyAssociationMetadataBuilder($metadataBuildingContext);
102 407
        $this->manyToManyAssociationMetadataBuilder = $manyToManyAssociationMetadataBuilder ?: new ManyToManyAssociationMetadataBuilder($metadataBuildingContext);
103 407
        $this->transientMetadataBuilder             = $transientMetadataBuilder ?: new TransientMetadataBuilder($metadataBuildingContext);
104 407
    }
105
106 406
    public function withComponentMetadata(Mapping\ClassMetadata $componentMetadata) : PropertyMetadataBuilder
107
    {
108 406
        $this->componentMetadata = $componentMetadata;
109
110 406
        $this->fieldMetadataBuilder->withComponentMetadata($componentMetadata);
111 406
        $this->oneToOneAssociationMetadataBuilder->withComponentMetadata($componentMetadata);
112 406
        $this->manyToOneAssociationMetadataBuilder->withComponentMetadata($componentMetadata);
113 406
        $this->oneToManyAssociationMetadataBuilder->withComponentMetadata($componentMetadata);
114 406
        $this->manyToManyAssociationMetadataBuilder->withComponentMetadata($componentMetadata);
115 406
        $this->transientMetadataBuilder->withComponentMetadata($componentMetadata);
116
117 406
        return $this;
118
    }
119
120 406
    public function withFieldName(string $fieldName) : PropertyMetadataBuilder
121
    {
122 406
        $this->fieldName = $fieldName;
123
124 406
        $this->fieldMetadataBuilder->withFieldName($fieldName);
125 406
        $this->oneToOneAssociationMetadataBuilder->withFieldName($fieldName);
126 406
        $this->manyToOneAssociationMetadataBuilder->withFieldName($fieldName);
127 406
        $this->oneToManyAssociationMetadataBuilder->withFieldName($fieldName);
128 406
        $this->manyToManyAssociationMetadataBuilder->withFieldName($fieldName);
129 406
        $this->transientMetadataBuilder->withFieldName($fieldName);
130
131 406
        return $this;
132
    }
133
134 406
    public function withIdAnnotation(?Annotation\Id $idAnnotation) : PropertyMetadataBuilder
135
    {
136 406
        $this->idAnnotation = $idAnnotation;
137
138 406
        $this->fieldMetadataBuilder->withIdAnnotation($idAnnotation);
139 406
        $this->oneToOneAssociationMetadataBuilder->withIdAnnotation($idAnnotation);
140 406
        $this->manyToOneAssociationMetadataBuilder->withIdAnnotation($idAnnotation);
141
142 406
        return $this;
143
    }
144
145 389
    public function withCacheAnnotation(?Annotation\Cache $cacheAnnotation) : PropertyMetadataBuilder
146
    {
147 389
        $this->cacheAnnotation = $cacheAnnotation;
148
149 389
        $this->oneToOneAssociationMetadataBuilder->withCacheAnnotation($cacheAnnotation);
150 389
        $this->manyToOneAssociationMetadataBuilder->withCacheAnnotation($cacheAnnotation);
151 389
        $this->oneToManyAssociationMetadataBuilder->withCacheAnnotation($cacheAnnotation);
152 389
        $this->manyToManyAssociationMetadataBuilder->withCacheAnnotation($cacheAnnotation);
153
154 389
        return $this;
155
    }
156
157 402
    public function withColumnAnnotation(?Annotation\Column $columnAnnotation) : PropertyMetadataBuilder
158
    {
159 402
        $this->columnAnnotation = $columnAnnotation;
160
161 402
        if ($columnAnnotation !== null) {
162
            // Make sure all other property type annotations are cleared
163 397
            $this->embeddedAnnotation   = null;
164 397
            $this->oneToOneAnnotation   = null;
165 397
            $this->manyToOneAnnotation  = null;
166 397
            $this->oneToManyAnnotation  = null;
167 397
            $this->manyToManyAnnotation = null;
168
169 397
            $this->fieldMetadataBuilder->withColumnAnnotation($columnAnnotation);
170
        }
171
172 402
        return $this;
173
    }
174
175 372
    public function withEmbeddedAnnotation(?Annotation\Embedded $embeddedAnnotation) : PropertyMetadataBuilder
176
    {
177 372
        $this->embeddedAnnotation = $embeddedAnnotation;
178
179 372
        if ($embeddedAnnotation !== null) {
180
            // Make sure all other property type annotations are cleared
181
            $this->columnAnnotation     = null;
182
            $this->oneToOneAnnotation   = null;
183
            $this->manyToOneAnnotation  = null;
184
            $this->oneToManyAnnotation  = null;
185
            $this->manyToManyAnnotation = null;
186
187
            // $this->embeddedMetadataBuilder->withEmbeddedAnnotation($embeddedAnnotation);
188
        }
189
190 372
        return $this;
191
    }
192
193 379
    public function withOneToOneAnnotation(?Annotation\OneToOne $oneToOneAnnotation) : PropertyMetadataBuilder
194
    {
195 379
        $this->oneToOneAnnotation = $oneToOneAnnotation;
196
197 379
        if ($oneToOneAnnotation !== null) {
198
            // Make sure all other property type annotations are cleared
199 118
            $this->columnAnnotation     = null;
200 118
            $this->embeddedAnnotation   = null;
201 118
            $this->manyToOneAnnotation  = null;
202 118
            $this->oneToManyAnnotation  = null;
203 118
            $this->manyToManyAnnotation = null;
204
205 118
            $this->oneToOneAssociationMetadataBuilder->withOneToOneAnnotation($oneToOneAnnotation);
206
        }
207
208 379
        return $this;
209
    }
210
211 380
    public function withManyToOneAnnotation(?Annotation\ManyToOne $manyToOneAnnotation) : PropertyMetadataBuilder
212
    {
213 380
        $this->manyToOneAnnotation = $manyToOneAnnotation;
214
215 380
        if ($manyToOneAnnotation !== null) {
216
            // Make sure all other property type annotations are cleared
217 149
            $this->columnAnnotation     = null;
218 149
            $this->embeddedAnnotation   = null;
219 149
            $this->oneToOneAnnotation   = null;
220 149
            $this->oneToManyAnnotation  = null;
221 149
            $this->manyToManyAnnotation = null;
222
223 149
            $this->manyToOneAssociationMetadataBuilder->withManyToOneAnnotation($manyToOneAnnotation);
224
        }
225
226 380
        return $this;
227
    }
228
229 381
    public function withOneToManyAnnotation(?Annotation\OneToMany $oneToManyAnnotation) : PropertyMetadataBuilder
230
    {
231 381
        $this->oneToManyAnnotation = $oneToManyAnnotation;
232
233 381
        if ($oneToManyAnnotation !== null) {
234
            // Make sure all other property type annotations are cleared
235 118
            $this->columnAnnotation     = null;
236 118
            $this->embeddedAnnotation   = null;
237 118
            $this->oneToOneAnnotation   = null;
238 118
            $this->manyToOneAnnotation  = null;
239 118
            $this->manyToManyAnnotation = null;
240
241 118
            $this->oneToManyAssociationMetadataBuilder->withOneToManyAnnotation($oneToManyAnnotation);
242
        }
243
244 381
        return $this;
245
    }
246
247 386
    public function withManyToManyAnnotation(?Annotation\ManyToMany $manyToManyAnnotation) : PropertyMetadataBuilder
248
    {
249 386
        $this->manyToManyAnnotation = $manyToManyAnnotation;
250
251 386
        if ($manyToManyAnnotation !== null) {
252
            // Make sure all other property type annotations are cleared
253 103
            $this->columnAnnotation    = null;
254 103
            $this->embeddedAnnotation  = null;
255 103
            $this->oneToOneAnnotation  = null;
256 103
            $this->manyToOneAnnotation = null;
257 103
            $this->oneToManyAnnotation = null;
258
259 103
            $this->manyToManyAssociationMetadataBuilder->withManyToManyAnnotation($manyToManyAnnotation);
260
        }
261
262 386
        return $this;
263
    }
264
265 386
    public function withJoinTableAnnotation(?Annotation\JoinTable $joinTableAnnotation) : PropertyMetadataBuilder
266
    {
267 386
        $this->joinTableAnnotation = $joinTableAnnotation;
268
269 386
        $this->manyToManyAssociationMetadataBuilder->withJoinTableAnnotation($joinTableAnnotation);
270
271 386
        return $this;
272
    }
273
274 385
    public function withJoinColumnsAnnotation(?Annotation\JoinColumns $joinColumnsAnnotation) : PropertyMetadataBuilder
275
    {
276 385
        $this->joinColumnsAnnotation = $joinColumnsAnnotation;
277
278 385
        $this->oneToOneAssociationMetadataBuilder->withJoinColumnsAnnotation($joinColumnsAnnotation);
279 385
        $this->manyToOneAssociationMetadataBuilder->withJoinColumnsAnnotation($joinColumnsAnnotation);
280
281 385
        return $this;
282
    }
283
284 385
    public function withJoinColumnAnnotation(?Annotation\JoinColumn $joinColumnAnnotation) : PropertyMetadataBuilder
285
    {
286 385
        $this->joinColumnAnnotation = $joinColumnAnnotation;
287
288 385
        $this->oneToOneAssociationMetadataBuilder->withJoinColumnAnnotation($joinColumnAnnotation);
289 385
        $this->manyToOneAssociationMetadataBuilder->withJoinColumnAnnotation($joinColumnAnnotation);
290
291 385
        return $this;
292
    }
293
294 387
    public function withOrderByAnnotation(?Annotation\OrderBy $orderByAnnotation) : PropertyMetadataBuilder
295
    {
296 387
        $this->orderByAnnotation = $orderByAnnotation;
297
298 387
        $this->oneToManyAssociationMetadataBuilder->withOrderByAnnotation($orderByAnnotation);
299 387
        $this->manyToManyAssociationMetadataBuilder->withOrderByAnnotation($orderByAnnotation);
300
301 387
        return $this;
302
    }
303
304 406
    public function withVersionAnnotation(?Annotation\Version $versionAnnotation) : PropertyMetadataBuilder
305
    {
306 406
        $this->versionAnnotation = $versionAnnotation;
307
308 406
        $this->fieldMetadataBuilder->withVersionAnnotation($versionAnnotation);
309
310 406
        return $this;
311
    }
312
313 400
    public function withGeneratedValueAnnotation(?Annotation\GeneratedValue $generatedValueAnnotation) : PropertyMetadataBuilder
314
    {
315 400
        $this->generatedValueAnnotation = $generatedValueAnnotation;
316
317 400
        $this->fieldMetadataBuilder->withGeneratedValueAnnotation($this->generatedValueAnnotation);
318
319 400
        return $this;
320
    }
321
322 400
    public function withSequenceGeneratorAnnotation(?Annotation\SequenceGenerator $sequenceGeneratorAnnotation) : PropertyMetadataBuilder
323
    {
324 400
        $this->sequenceGeneratorAnnotation = $sequenceGeneratorAnnotation;
325
326 400
        $this->fieldMetadataBuilder->withSequenceGeneratorAnnotation($this->sequenceGeneratorAnnotation);
327
328 400
        return $this;
329
    }
330
331 400
    public function withCustomIdGeneratorAnnotation(?Annotation\CustomIdGenerator $customIdGeneratorAnnotation) : PropertyMetadataBuilder
332
    {
333 400
        $this->customIdGeneratorAnnotation = $customIdGeneratorAnnotation;
334
335 400
        $this->fieldMetadataBuilder->withCustomIdGeneratorAnnotation($this->customIdGeneratorAnnotation);
336
337 400
        return $this;
338
    }
339
340 406
    public function build() : ?Mapping\Property
341
    {
342
        // Validate required fields
343 406
        assert($this->componentMetadata !== null);
344 406
        assert($this->fieldName !== null);
345
346 406
        $componentClassName = $this->componentMetadata->getClassName();
347
348 406
        switch (true) {
349 406
            case $this->columnAnnotation !== null:
350 397
                $propertyMetadata = $this->fieldMetadataBuilder->build();
351
352
                // Prevent column duplication
353 397
                $columnName = $propertyMetadata->getColumnName();
354
355 397
                if ($this->componentMetadata->checkPropertyDuplication($columnName)) {
0 ignored issues
show
Bug introduced by
It seems like $columnName can also be of type null; however, parameter $columnName of Doctrine\ORM\Mapping\Cla...ckPropertyDuplication() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

355
                if ($this->componentMetadata->checkPropertyDuplication(/** @scrutinizer ignore-type */ $columnName)) {
Loading history...
356
                    throw Mapping\MappingException::duplicateColumnName($componentClassName, $columnName);
357
                }
358
359 397
                return $propertyMetadata;
360 271
            case $this->embeddedAnnotation !== null:
361
                // @todo guilhermeblanco Remove nullable typehint once embeddeds are back
362
                return null;
363 271
            case $this->oneToOneAnnotation !== null:
364 118
                return $this->oneToOneAssociationMetadataBuilder->build();
365
366
                // Prevent column duplication
367
                // @todo guilhermeblanco Open an issue to discuss making this scenario impossible.
368
//                foreach ($propertyMetadata->getJoinColumns() as $joinColumnMetadata) {
369
//                    $columnName = $joinColumnMetadata->getColumnName();
370
//
371
//                    if ($this->componentMetadata->checkPropertyDuplication($columnName)) {
372
//                        throw Mapping\MappingException::duplicateColumnName($componentClassName, $columnName);
373
//                    }
374
//                }
375
//
376
//                return $propertyMetadata;
377 218
            case $this->manyToOneAnnotation !== null:
378 149
                return $this->manyToOneAssociationMetadataBuilder->build();
379
380
                // Prevent column duplication
381
                // @todo guilhermeblanco Open an issue to discuss making this scenario impossible.
382
//                foreach ($propertyMetadata->getJoinColumns() as $joinColumnMetadata) {
383
//                    $columnName = $joinColumnMetadata->getColumnName();
384
//
385
//                    if ($this->componentMetadata->checkPropertyDuplication($columnName)) {
386
//                        throw Mapping\MappingException::duplicateColumnName($componentClassName, $columnName);
387
//                    }
388
//                }
389
//
390
//                return $propertyMetadata;
391 178
            case $this->oneToManyAnnotation !== null:
392 118
                $propertyMetadata = $this->oneToManyAssociationMetadataBuilder->build();
393
394 118
                if ($this->componentMetadata->isMappedSuperclass && ! $propertyMetadata->isOwningSide()) {
395
                    throw Mapping\MappingException::illegalToManyAssociationOnMappedSuperclass(
396
                        $componentClassName,
397
                        $this->fieldName
398
                    );
399
                }
400
401 118
                return $propertyMetadata;
402 118
            case $this->manyToManyAnnotation !== null:
403 103
                $propertyMetadata = $this->manyToManyAssociationMetadataBuilder->build();
404
405 103
                if ($this->componentMetadata->isMappedSuperclass && ! $propertyMetadata->isOwningSide()) {
406 1
                    throw Mapping\MappingException::illegalToManyAssociationOnMappedSuperclass(
407 1
                        $componentClassName,
408 1
                        $this->fieldName
409
                    );
410
                }
411
412 102
                return $propertyMetadata;
413
            default:
414 29
                return $this->transientMetadataBuilder->build();
415
        }
416
    }
417
}
418