Failed Conditions
Push — master ( 4f8027...fa7802 )
by Guilherme
09:14
created

convertReflectionPropertyToFieldMetadata()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 80
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 9.6988

Importance

Changes 0
Metric Value
cc 9
eloc 42
nc 9
nop 4
dl 0
loc 80
ccs 31
cts 39
cp 0.7949
crap 9.6988
rs 7.6924
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php /** @noinspection ALL */
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping\Driver;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\Reader;
9
use Doctrine\DBAL\Types\Type;
10
use Doctrine\ORM\Annotation;
11
use Doctrine\ORM\Cache\Exception\CacheException;
12
use Doctrine\ORM\Events;
13
use Doctrine\ORM\Mapping;
14
use Doctrine\ORM\Mapping\Builder;
15
use FilesystemIterator;
16
use RecursiveDirectoryIterator;
17
use RecursiveIteratorIterator;
18
use RecursiveRegexIterator;
19
use ReflectionClass;
20
use ReflectionException;
21
use ReflectionMethod;
22
use ReflectionProperty;
23
use RegexIterator;
24
use RuntimeException;
25
use UnexpectedValueException;
26
use function array_diff;
27
use function array_intersect;
28
use function array_map;
29
use function array_merge;
30
use function array_unique;
31
use function class_exists;
32
use function constant;
33
use function count;
34
use function defined;
35
use function get_class;
36
use function get_declared_classes;
37
use function in_array;
38
use function is_dir;
39
use function is_numeric;
40
use function preg_match;
41
use function preg_quote;
42
use function realpath;
43
use function sprintf;
44
use function str_replace;
45
use function strpos;
46
use function strtoupper;
47
use function var_export;
48
49
/**
50
 * The AnnotationDriver reads the mapping metadata from docblock annotations.
51
 */
52
class AnnotationDriver implements MappingDriver
53
{
54
    /** @var int[] */
55
    protected $entityAnnotationClasses = [
56
        Annotation\Entity::class           => 1,
57
        Annotation\MappedSuperclass::class => 2,
58
    ];
59
60
    /**
61
     * The AnnotationReader.
62
     *
63
     * @var AnnotationReader
64
     */
65
    protected $reader;
66
67
    /**
68
     * The paths where to look for mapping files.
69
     *
70
     * @var string[]
71
     */
72
    protected $paths = [];
73
74
    /**
75
     * The paths excluded from path where to look for mapping files.
76
     *
77
     * @var string[]
78
     */
79
    protected $excludePaths = [];
80
81
    /**
82
     * The file extension of mapping documents.
83
     *
84
     * @var string
85
     */
86
    protected $fileExtension = '.php';
87
88
    /**
89
     * Cache for AnnotationDriver#getAllClassNames().
90
     *
91
     * @var string[]|null
92
     */
93
    protected $classNames;
94
95
    /**
96
     * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
97
     * docblock annotations.
98
     *
99
     * @param Reader               $reader The AnnotationReader to use, duck-typed.
100
     * @param string|string[]|null $paths  One or multiple paths where mapping classes can be found.
101
     */
102 2297
    public function __construct(Reader $reader, $paths = null)
103
    {
104 2297
        $this->reader = $reader;
0 ignored issues
show
Documentation Bug introduced by
$reader is of type Doctrine\Common\Annotations\Reader, but the property $reader was declared to be of type Doctrine\Common\Annotations\AnnotationReader. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
105
106 2297
        if ($paths) {
107 2211
            $this->addPaths((array) $paths);
108
        }
109 2297
    }
110
111
    /**
112
     * Appends lookup paths to metadata driver.
113
     *
114
     * @param string[] $paths
115
     */
116 2215
    public function addPaths(array $paths)
117
    {
118 2215
        $this->paths = array_unique(array_merge($this->paths, $paths));
119 2215
    }
120
121
    /**
122
     * Retrieves the defined metadata lookup paths.
123
     *
124
     * @return string[]
125
     */
126
    public function getPaths()
127
    {
128
        return $this->paths;
129
    }
130
131
    /**
132
     * Append exclude lookup paths to metadata driver.
133
     *
134
     * @param string[] $paths
135
     */
136
    public function addExcludePaths(array $paths)
137
    {
138
        $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
139
    }
140
141
    /**
142
     * Retrieve the defined metadata lookup exclude paths.
143
     *
144
     * @return string[]
145
     */
146
    public function getExcludePaths()
147
    {
148
        return $this->excludePaths;
149
    }
150
151
    /**
152
     * Retrieve the current annotation reader
153
     *
154
     * @return Reader
155
     */
156 1
    public function getReader()
157
    {
158 1
        return $this->reader;
159
    }
160
161
    /**
162
     * Gets the file extension used to look for mapping files under.
163
     *
164
     * @return string
165
     */
166
    public function getFileExtension()
167
    {
168
        return $this->fileExtension;
169
    }
170
171
    /**
172
     * Sets the file extension used to look for mapping files under.
173
     *
174
     * @param string $fileExtension The file extension to set.
175
     */
176
    public function setFileExtension($fileExtension)
177
    {
178
        $this->fileExtension = $fileExtension;
179
    }
180
181
    /**
182
     * Returns whether the class with the specified name is transient. Only non-transient
183
     * classes, that is entities and mapped superclasses, should have their metadata loaded.
184
     *
185
     * A class is non-transient if it is annotated with an annotation
186
     * from the {@see AnnotationDriver::entityAnnotationClasses}.
187
     *
188
     * @param string $className
189
     *
190
     * @throws ReflectionException
191
     */
192 193
    public function isTransient($className) : bool
193
    {
194 193
        $classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className));
195
196 193
        foreach ($classAnnotations as $annotation) {
197 188
            if (isset($this->entityAnnotationClasses[get_class($annotation)])) {
198 188
                return false;
199
            }
200
        }
201
202 12
        return true;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     *
208
     * @throws ReflectionException
209
     */
210 60
    public function getAllClassNames() : array
211
    {
212 60
        if ($this->classNames !== null) {
213 45
            return $this->classNames;
214
        }
215
216 60
        if (! $this->paths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->paths of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
217
            throw Mapping\MappingException::pathRequired();
218
        }
219
220 60
        $classes       = [];
221 60
        $includedFiles = [];
222
223 60
        foreach ($this->paths as $path) {
224 60
            if (! is_dir($path)) {
225
                throw Mapping\MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
226
            }
227
228 60
            $iterator = new RegexIterator(
229 60
                new RecursiveIteratorIterator(
230 60
                    new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
231 60
                    RecursiveIteratorIterator::LEAVES_ONLY
232
                ),
233 60
                '/^.+' . preg_quote($this->fileExtension) . '$/i',
234 60
                RecursiveRegexIterator::GET_MATCH
235
            );
236
237 60
            foreach ($iterator as $file) {
238 60
                $sourceFile = $file[0];
239
240 60
                if (! preg_match('(^phar:)i', $sourceFile)) {
241 60
                    $sourceFile = realpath($sourceFile);
242
                }
243
244 60
                foreach ($this->excludePaths as $excludePath) {
245
                    $exclude = str_replace('\\', '/', realpath($excludePath));
246
                    $current = str_replace('\\', '/', $sourceFile);
247
248
                    if (strpos($current, $exclude) !== false) {
249
                        continue 2;
250
                    }
251
                }
252
253 60
                require_once $sourceFile;
254
255 60
                $includedFiles[] = $sourceFile;
256
            }
257
        }
258
259 60
        $declared = get_declared_classes();
260
261 60
        foreach ($declared as $className) {
262 60
            $reflectionClass = new ReflectionClass($className);
263 60
            $sourceFile      = $reflectionClass->getFileName();
264
265 60
            if (in_array($sourceFile, $includedFiles, true) && ! $this->isTransient($className)) {
266 60
                $classes[] = $className;
267
            }
268
        }
269
270 60
        $this->classNames = $classes;
271
272 60
        return $classes;
273
    }
274
275
    /**
276
     * {@inheritDoc}
277
     *
278
     * @throws CacheException
279
     * @throws Mapping\MappingException
280
     * @throws ReflectionException
281
     * @throws RuntimeException
282
     * @throws UnexpectedValueException
283
     */
284 379
    public function loadMetadataForClass(
285
        string $className,
286
        ?Mapping\ComponentMetadata $parent,
287
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
288
    ) : Mapping\ComponentMetadata {
289 379
        $reflectionClass  = new ReflectionClass($className);
290 379
        $metadata         = new Mapping\ClassMetadata($className, $parent, $metadataBuildingContext);
291 379
        $classAnnotations = $this->getClassAnnotations($reflectionClass);
292 379
        $classMetadata    = $this->convertClassAnnotationsToClassMetadata(
293 379
            $classAnnotations,
294 379
            $reflectionClass,
295 379
            $metadata,
296 379
            $metadataBuildingContext
297
        );
298
299
        // Evaluate @Cache annotation
300 373
        if (isset($classAnnotations[Annotation\Cache::class])) {
301 18
            $cacheBuilder = new Builder\CacheMetadataBuilder($metadataBuildingContext);
302
303
            $cacheBuilder
304 18
                ->withComponentMetadata($metadata)
305 18
                ->withCacheAnnotation($classAnnotations[Annotation\Cache::class]);
306
307 18
            $metadata->setCache($cacheBuilder->build());
308
        }
309
310
        // Evaluate annotations on properties/fields
311
        /** @var ReflectionProperty $reflProperty */
312 373
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
313 373
            if ($reflectionProperty->getDeclaringClass()->getName() !== $reflectionClass->getName()) {
314 74
                continue;
315
            }
316
317 373
            $propertyAnnotations = $this->getPropertyAnnotations($reflectionProperty);
318 372
            $property            = $this->convertPropertyAnnotationsToProperty(
319 372
                $propertyAnnotations,
320 372
                $reflectionProperty,
321 372
                $classMetadata,
322 372
                $metadataBuildingContext
323
            );
324
325 372
            if ($classMetadata->isMappedSuperclass &&
326 372
                $property instanceof Mapping\ToManyAssociationMetadata &&
327 372
                ! $property->isOwningSide()) {
328 1
                throw Mapping\MappingException::illegalToManyAssociationOnMappedSuperclass(
329 1
                    $classMetadata->getClassName(),
330 1
                    $property->getName()
331
                );
332
            }
333
334 371
            $metadata->addProperty($property);
335
        }
336
337 370
        $this->attachPropertyOverrides($classAnnotations, $reflectionClass, $metadata, $metadataBuildingContext);
338
339 370
        return $classMetadata;
340
    }
341
342
    /**
343
     * @param Annotation\Annotation[] $classAnnotations
344
     *
345
     * @throws Mapping\MappingException
346
     * @throws UnexpectedValueException
347
     * @throws ReflectionException
348
     */
349 379
    private function convertClassAnnotationsToClassMetadata(
350
        array $classAnnotations,
351
        ReflectionClass $reflectionClass,
352
        Mapping\ClassMetadata $metadata,
353
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
354
    ) : Mapping\ClassMetadata {
355
        switch (true) {
356 379
            case isset($classAnnotations[Annotation\Entity::class]):
357 372
                return $this->convertClassAnnotationsToEntityClassMetadata(
358 372
                    $classAnnotations,
359 372
                    $reflectionClass,
360 372
                    $metadata,
361 372
                    $metadataBuildingContext
362
                );
363
364
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
365
366 29
            case isset($classAnnotations[Annotation\MappedSuperclass::class]):
367 23
                return $this->convertClassAnnotationsToMappedSuperClassMetadata(
368 23
                    $classAnnotations,
369 23
                    $reflectionClass,
370 23
                    $metadata
371
                );
372 6
            case isset($classAnnotations[Annotation\Embeddable::class]):
373
                return $this->convertClassAnnotationsToEmbeddableClassMetadata(
374
                    $classAnnotations,
375
                    $reflectionClass,
376
                    $metadata
377
                );
378
            default:
379 6
                throw Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass($reflectionClass->getName());
380
        }
381
    }
382
383
    /**
384
     * @param Annotation\Annotation[] $classAnnotations
385
     *
386
     * @return Mapping\ClassMetadata
387
     *
388
     * @throws Mapping\MappingException
389
     * @throws ReflectionException
390
     * @throws UnexpectedValueException
391
     */
392 372
    private function convertClassAnnotationsToEntityClassMetadata(
393
        array $classAnnotations,
394
        ReflectionClass $reflectionClass,
395
        Mapping\ClassMetadata $metadata,
396
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
397
    ) {
398
        /** @var Annotation\Entity $entityAnnot */
399 372
        $entityAnnot = $classAnnotations[Annotation\Entity::class];
400
401 372
        if ($entityAnnot->repositoryClass !== null) {
402 3
            $metadata->setCustomRepositoryClassName($entityAnnot->repositoryClass);
403
        }
404
405 372
        if ($entityAnnot->readOnly) {
406 1
            $metadata->asReadOnly();
407
        }
408
409 372
        $metadata->isMappedSuperclass = false;
410 372
        $metadata->isEmbeddedClass    = false;
411
412
        // Process table information
413 372
        $parent = $metadata->getParent();
414
415 372
        if ($parent && $parent->inheritanceType === Mapping\InheritanceType::SINGLE_TABLE) {
416
            // Handle the case where a middle mapped super class inherits from a single table inheritance tree.
417
            do {
418 29
                if (! $parent->isMappedSuperclass) {
419 29
                    $metadata->setTable($parent->table);
420
421 29
                    break;
422
                }
423
424 4
                $parent = $parent->getParent();
425 29
            } while ($parent !== null);
426
        } else {
427 372
            $tableBuilder = new Builder\TableMetadataBuilder($metadataBuildingContext);
428
429
            $tableBuilder
430 372
                ->withEntityClassMetadata($metadata)
431 372
                ->withTableAnnotation($classAnnotations[Annotation\Table::class] ?? null);
432
433 372
            $metadata->setTable($tableBuilder->build());
434
        }
435
436
        // Evaluate @ChangeTrackingPolicy annotation
437 372
        if (isset($classAnnotations[Annotation\ChangeTrackingPolicy::class])) {
438 6
            $changeTrackingAnnot = $classAnnotations[Annotation\ChangeTrackingPolicy::class];
439
440 6
            $metadata->setChangeTrackingPolicy(
441 6
                constant(sprintf('%s::%s', Mapping\ChangeTrackingPolicy::class, $changeTrackingAnnot->value))
0 ignored issues
show
Bug introduced by
Accessing value on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
442
            );
443
        }
444
445
        // Evaluate @InheritanceType annotation
446 372
        if (isset($classAnnotations[Annotation\InheritanceType::class])) {
447 80
            $inheritanceTypeAnnot = $classAnnotations[Annotation\InheritanceType::class];
448
449 80
            $metadata->setInheritanceType(
450 80
                constant(sprintf('%s::%s', Mapping\InheritanceType::class, $inheritanceTypeAnnot->value))
451
            );
452
453 80
            if ($metadata->inheritanceType !== Mapping\InheritanceType::NONE) {
454 80
                $discriminatorColumnBuilder = new Builder\DiscriminatorColumnMetadataBuilder($metadataBuildingContext);
455
456
                $discriminatorColumnBuilder
457 80
                    ->withComponentMetadata($metadata)
458 80
                    ->withDiscriminatorColumnAnnotation($classAnnotations[Annotation\DiscriminatorColumn::class] ?? null);
459
460 80
                $metadata->setDiscriminatorColumn($discriminatorColumnBuilder->build());
461
462
                // Evaluate DiscriminatorMap annotation
463 80
                if (isset($classAnnotations[Annotation\DiscriminatorMap::class])) {
464 77
                    $discriminatorMapAnnotation = $classAnnotations[Annotation\DiscriminatorMap::class];
465 77
                    $discriminatorMap           = $discriminatorMapAnnotation->value;
466
467 77
                    $metadata->setDiscriminatorMap($discriminatorMap);
468
                }
469
            }
470
        }
471
472 372
        $this->attachLifecycleCallbacks($classAnnotations, $reflectionClass, $metadata);
473 372
        $this->attachEntityListeners($classAnnotations, $metadata);
474
475 372
        return $metadata;
476
    }
477
478
    /**
479
     * @param Annotation\Annotation[] $classAnnotations
480
     *
481
     * @throws Mapping\MappingException
482
     * @throws ReflectionException
483
     */
484 23
    private function convertClassAnnotationsToMappedSuperClassMetadata(
485
        array $classAnnotations,
486
        ReflectionClass $reflectionClass,
487
        Mapping\ClassMetadata $metadata
488
    ) : Mapping\ClassMetadata {
489
        /** @var Annotation\MappedSuperclass $mappedSuperclassAnnot */
490 23
        $mappedSuperclassAnnot = $classAnnotations[Annotation\MappedSuperclass::class];
491
492 23
        if ($mappedSuperclassAnnot->repositoryClass !== null) {
493 2
            $metadata->setCustomRepositoryClassName($mappedSuperclassAnnot->repositoryClass);
494
        }
495
496 23
        $metadata->isMappedSuperclass = true;
497 23
        $metadata->isEmbeddedClass    = false;
498
499 23
        $this->attachLifecycleCallbacks($classAnnotations, $reflectionClass, $metadata);
500 23
        $this->attachEntityListeners($classAnnotations, $metadata);
501
502 23
        return $metadata;
503
    }
504
505
    /**
506
     * @param Annotation\Annotation[] $classAnnotations
507
     */
508
    private function convertClassAnnotationsToEmbeddableClassMetadata(
509
        array $classAnnotations,
0 ignored issues
show
Unused Code introduced by
The parameter $classAnnotations is not used and could be removed. ( Ignorable by Annotation )

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

509
        /** @scrutinizer ignore-unused */ array $classAnnotations,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
510
        ReflectionClass $reflectionClass,
0 ignored issues
show
Unused Code introduced by
The parameter $reflectionClass is not used and could be removed. ( Ignorable by Annotation )

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

510
        /** @scrutinizer ignore-unused */ ReflectionClass $reflectionClass,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
511
        Mapping\ClassMetadata $metadata
512
    ) : Mapping\ClassMetadata {
513
        $metadata->isMappedSuperclass = false;
514
        $metadata->isEmbeddedClass    = true;
515
516
        return $metadata;
517
    }
518
519
    /**
520
     * @param Annotation\Annotation[] $propertyAnnotations
521
     *
522
     * @todo guilhermeblanco Remove nullable typehint once embeddables are back
523
     */
524 372
    private function convertPropertyAnnotationsToProperty(
525
        array $propertyAnnotations,
526
        ReflectionProperty $reflectionProperty,
527
        Mapping\ClassMetadata $metadata,
528
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
529
    ) : ?Mapping\Property {
530
        switch (true) {
531 372
            case isset($propertyAnnotations[Annotation\Column::class]):
532 367
                return $this->convertReflectionPropertyToFieldMetadata(
533 367
                    $reflectionProperty,
534 367
                    $propertyAnnotations,
535 367
                    $metadata,
536 367
                    $metadataBuildingContext
537
                );
538 254
            case isset($propertyAnnotations[Annotation\OneToOne::class]):
539 111
                return $this->convertReflectionPropertyToOneToOneAssociationMetadata(
540 111
                    $reflectionProperty,
541 111
                    $propertyAnnotations,
542 111
                    $metadata,
543 111
                    $metadataBuildingContext
544
                );
545 202
            case isset($propertyAnnotations[Annotation\ManyToOne::class]):
546 141
                return $this->convertReflectionPropertyToManyToOneAssociationMetadata(
547 141
                    $reflectionProperty,
548 141
                    $propertyAnnotations,
549 141
                    $metadata,
550 141
                    $metadataBuildingContext
551
                );
552 163
            case isset($propertyAnnotations[Annotation\OneToMany::class]):
553 109
                return $this->convertReflectionPropertyToOneToManyAssociationMetadata(
554 109
                    $reflectionProperty,
555 109
                    $propertyAnnotations,
556 109
                    $metadata,
557 109
                    $metadataBuildingContext
558
                );
559 104
            case isset($propertyAnnotations[Annotation\ManyToMany::class]):
560 89
                return $this->convertReflectionPropertyToManyToManyAssociationMetadata(
561 89
                    $reflectionProperty,
562 89
                    $propertyAnnotations,
563 89
                    $metadata,
564 89
                    $metadataBuildingContext
565
                );
566 29
            case isset($propertyAnnotations[Annotation\Embedded::class]):
567
                return null;
568
            default:
569 29
                $transientBuilder = new Builder\TransientMetadataBuilder($metadataBuildingContext);
570
571
                $transientBuilder
572 29
                    ->withComponentMetadata($metadata)
573 29
                    ->withFieldName($reflectionProperty->getName());
574
575 29
                return $transientBuilder->build();
576
        }
577
    }
578
579
    /**
580
     * @param Annotation\Annotation[] $propertyAnnotations
581
     *
582
     * @throws Mapping\MappingException
583
     */
584 367
    private function convertReflectionPropertyToFieldMetadata(
585
        ReflectionProperty $reflectionProperty,
586
        array $propertyAnnotations,
587
        Mapping\ClassMetadata $metadata,
588
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
589
    ) : Mapping\FieldMetadata {
590 367
        $fieldBuilder = new Builder\FieldMetadataBuilder($metadataBuildingContext);
591
592
        $fieldBuilder
593 367
            ->withComponentMetadata($metadata)
594 367
            ->withFieldName($reflectionProperty->getName())
595 367
            ->withColumnAnnotation($propertyAnnotations[Annotation\Column::class])
596 367
            ->withIdAnnotation($propertyAnnotations[Annotation\Id::class] ?? null)
597 367
            ->withVersionAnnotation($propertyAnnotations[Annotation\Version::class] ?? null);
598
599 367
        $fieldMetadata = $fieldBuilder->build();
600
601
        // Prevent column duplication
602 367
        if ($metadata->checkPropertyDuplication($fieldMetadata->getColumnName())) {
0 ignored issues
show
Bug introduced by
It seems like $fieldMetadata->getColumnName() 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

602
        if ($metadata->checkPropertyDuplication(/** @scrutinizer ignore-type */ $fieldMetadata->getColumnName())) {
Loading history...
603
            throw Mapping\MappingException::duplicateColumnName(
604
                $metadata->getClassName(),
605
                $fieldMetadata->getColumnName()
606
            );
607
        }
608
609
        // Check for GeneratedValue strategy
610 367
        if (isset($propertyAnnotations[Annotation\GeneratedValue::class])) {
611 311
            $generatedValueAnnot = $propertyAnnotations[Annotation\GeneratedValue::class];
612 311
            $strategy            = strtoupper($generatedValueAnnot->strategy);
0 ignored issues
show
Bug introduced by
Accessing strategy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
613 311
            $idGeneratorType     = constant(sprintf('%s::%s', Mapping\GeneratorType::class, $strategy));
614
615 311
            if ($idGeneratorType !== Mapping\GeneratorType::NONE) {
616 291
                $idGeneratorDefinition = [];
617
618
                // Check for CustomGenerator/SequenceGenerator/TableGenerator definition
619
                switch (true) {
620 291
                    case isset($propertyAnnotations[Annotation\SequenceGenerator::class]):
621 9
                        $seqGeneratorAnnot = $propertyAnnotations[Annotation\SequenceGenerator::class];
622
623
                        $idGeneratorDefinition = [
624 9
                            'sequenceName' => $seqGeneratorAnnot->sequenceName,
0 ignored issues
show
Bug introduced by
Accessing sequenceName on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
625 9
                            'allocationSize' => $seqGeneratorAnnot->allocationSize,
0 ignored issues
show
Bug introduced by
Accessing allocationSize on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
626
                        ];
627
628 9
                        break;
629
630 282
                    case isset($propertyAnnotations[Annotation\CustomIdGenerator::class]):
631 3
                        $customGeneratorAnnot = $propertyAnnotations[Annotation\CustomIdGenerator::class];
632
633
                        $idGeneratorDefinition = [
634 3
                            'class' => $customGeneratorAnnot->class,
635 3
                            'arguments' => $customGeneratorAnnot->arguments,
636
                        ];
637
638 3
                        if (! isset($idGeneratorDefinition['class'])) {
639
                            throw new Mapping\MappingException(
640
                                sprintf('Cannot instantiate custom generator, no class has been defined')
641
                            );
642
                        }
643
644 3
                        if (! class_exists($idGeneratorDefinition['class'])) {
645
                            throw new Mapping\MappingException(
646
                                sprintf('Cannot instantiate custom generator : %s', var_export($idGeneratorDefinition, true))
647
                            );
648
                        }
649
650 3
                        break;
651
652
                    /** @todo If it is not supported, why does this exist? */
653 279
                    case isset($propertyAnnotations['Doctrine\ORM\Mapping\TableGenerator']):
654
                        throw Mapping\MappingException::tableIdGeneratorNotImplemented($metadata->getClassName());
655
                }
656
657 291
                $fieldMetadata->setValueGenerator(
658 291
                    new Mapping\ValueGeneratorMetadata($idGeneratorType, $idGeneratorDefinition)
659
                );
660
            }
661
        }
662
663 367
        return $fieldMetadata;
664
    }
665
666
    /**
667
     * @param Annotation\Annotation[] $propertyAnnotations
668
     */
669 111
    private function convertReflectionPropertyToOneToOneAssociationMetadata(
670
        ReflectionProperty $reflectionProperty,
671
        array $propertyAnnotations,
672
        Mapping\ClassMetadata $metadata,
673
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
674
    ) : Mapping\OneToOneAssociationMetadata {
675 111
        $className     = $metadata->getClassName();
676 111
        $fieldName     = $reflectionProperty->getName();
677 111
        $oneToOneAnnot = $propertyAnnotations[Annotation\OneToOne::class];
678 111
        $assocMetadata = new Mapping\OneToOneAssociationMetadata($fieldName);
679 111
        $targetEntity  = $oneToOneAnnot->targetEntity;
0 ignored issues
show
Bug introduced by
Accessing targetEntity on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
680
681 111
        $assocMetadata->setTargetEntity($targetEntity);
682 111
        $assocMetadata->setCascade($this->getCascade($className, $fieldName, $oneToOneAnnot->cascade));
0 ignored issues
show
Bug introduced by
Accessing cascade on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
683 111
        $assocMetadata->setOrphanRemoval($oneToOneAnnot->orphanRemoval);
0 ignored issues
show
Bug introduced by
Accessing orphanRemoval on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
684 111
        $assocMetadata->setFetchMode($this->getFetchMode($className, $oneToOneAnnot->fetch));
0 ignored issues
show
Bug introduced by
Accessing fetch on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
685
686 111
        if (! empty($oneToOneAnnot->mappedBy)) {
0 ignored issues
show
Bug introduced by
Accessing mappedBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
687 40
            $assocMetadata->setMappedBy($oneToOneAnnot->mappedBy);
688 40
            $assocMetadata->setOwningSide(false);
689
        }
690
691 111
        if (! empty($oneToOneAnnot->inversedBy)) {
0 ignored issues
show
Bug introduced by
Accessing inversedBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
692 51
            $assocMetadata->setInversedBy($oneToOneAnnot->inversedBy);
693 51
            $assocMetadata->setOwningSide(true);
694
        }
695
696
        // Check for Id
697 111
        if (isset($propertyAnnotations[Annotation\Id::class])) {
698 12
            $assocMetadata->setPrimaryKey(true);
699
        }
700
701
        // Check for Cache
702 111
        if (isset($propertyAnnotations[Annotation\Cache::class])) {
703 4
            $cacheBuilder = new Builder\CacheMetadataBuilder($metadataBuildingContext);
704
705
            $cacheBuilder
706 4
                ->withComponentMetadata($metadata)
707 4
                ->withFieldName($fieldName)
708 4
                ->withCacheAnnotation($propertyAnnotations[Annotation\Cache::class]);
709
710 4
            $assocMetadata->setCache($cacheBuilder->build());
711
        }
712
713
        // Check for owning side to consider join column
714 111
        if (! $assocMetadata->isOwningSide()) {
715 40
            return $assocMetadata;
716
        }
717
718
        // Check for JoinColumn/JoinColumns annotations
719 105
        $joinColumnBuilder = new Builder\JoinColumnMetadataBuilder($metadataBuildingContext);
720
721
        $joinColumnBuilder
722 105
            ->withComponentMetadata($metadata)
723 105
            ->withFieldName($fieldName);
724
725
        switch (true) {
726 105
            case isset($propertyAnnotations[Annotation\JoinColumn::class]):
727 79
                $joinColumnBuilder->withJoinColumnAnnotation($propertyAnnotations[Annotation\JoinColumn::class]);
728
729 79
                $assocMetadata->addJoinColumn($joinColumnBuilder->build());
730 79
                break;
731
732 29
            case isset($propertyAnnotations[Annotation\JoinColumns::class]):
733 3
                $joinColumnsAnnot = $propertyAnnotations[Annotation\JoinColumns::class];
734
735 3
                foreach ($joinColumnsAnnot->value as $joinColumnAnnot) {
736 3
                    $joinColumnBuilder->withJoinColumnAnnotation($joinColumnAnnot);
737
738 3
                    $assocMetadata->addJoinColumn($joinColumnBuilder->build());
739
                }
740
741 3
                break;
742
743
            default:
744 26
                $assocMetadata->addJoinColumn($joinColumnBuilder->build());
745 26
                break;
746
        }
747
748 105
        return $assocMetadata;
749
    }
750
751
    /**
752
     * @param Annotation\Annotation[] $propertyAnnotations
753
     */
754 141
    private function convertReflectionPropertyToManyToOneAssociationMetadata(
755
        ReflectionProperty $reflectionProperty,
756
        array $propertyAnnotations,
757
        Mapping\ClassMetadata $metadata,
758
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
759
    ) : Mapping\ManyToOneAssociationMetadata {
760
        // ManyToOne must be owning side by design
761 141
        $className      = $metadata->getClassName();
762 141
        $fieldName      = $reflectionProperty->getName();
763 141
        $manyToOneAnnot = $propertyAnnotations[Annotation\ManyToOne::class];
764 141
        $assocMetadata  = new Mapping\ManyToOneAssociationMetadata($fieldName);
765 141
        $targetEntity   = $manyToOneAnnot->targetEntity;
0 ignored issues
show
Bug introduced by
Accessing targetEntity on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
766
767 141
        $assocMetadata->setTargetEntity($targetEntity);
768 141
        $assocMetadata->setCascade($this->getCascade($className, $fieldName, $manyToOneAnnot->cascade));
0 ignored issues
show
Bug introduced by
Accessing cascade on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
769 141
        $assocMetadata->setFetchMode($this->getFetchMode($className, $manyToOneAnnot->fetch));
0 ignored issues
show
Bug introduced by
Accessing fetch on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
770
771 141
        if (! empty($manyToOneAnnot->inversedBy)) {
0 ignored issues
show
Bug introduced by
Accessing inversedBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
772 94
            $assocMetadata->setInversedBy($manyToOneAnnot->inversedBy);
773
        }
774
775
        // Check for Id
776 141
        if (isset($propertyAnnotations[Annotation\Id::class])) {
777 34
            $assocMetadata->setPrimaryKey(true);
778
        }
779
780
        // Check for Cache
781 141
        if (isset($propertyAnnotations[Annotation\Cache::class])) {
782 12
            $cacheBuilder = new Builder\CacheMetadataBuilder($metadataBuildingContext);
783
784
            $cacheBuilder
785 12
                ->withComponentMetadata($metadata)
786 12
                ->withFieldName($fieldName)
787 12
                ->withCacheAnnotation($propertyAnnotations[Annotation\Cache::class]);
788
789 12
            $assocMetadata->setCache($cacheBuilder->build());
790
        }
791
792
        // Check for JoinColumn/JoinColumns annotations
793 141
        $joinColumnBuilder = new Builder\JoinColumnMetadataBuilder($metadataBuildingContext);
794
795
        $joinColumnBuilder
796 141
            ->withComponentMetadata($metadata)
797 141
            ->withFieldName($fieldName);
798
799
        switch (true) {
800 141
            case isset($propertyAnnotations[Annotation\JoinColumn::class]):
801 81
                $joinColumnBuilder->withJoinColumnAnnotation($propertyAnnotations[Annotation\JoinColumn::class]);
802
803 81
                $assocMetadata->addJoinColumn($joinColumnBuilder->build());
804 81
                break;
805
806 69
            case isset($propertyAnnotations[Annotation\JoinColumns::class]):
807 16
                $joinColumnsAnnot = $propertyAnnotations[Annotation\JoinColumns::class];
808
809 16
                foreach ($joinColumnsAnnot->value as $joinColumnAnnot) {
810 16
                    $joinColumnBuilder->withJoinColumnAnnotation($joinColumnAnnot);
811
812 16
                    $assocMetadata->addJoinColumn($joinColumnBuilder->build());
813
                }
814
815 16
                break;
816
817
            default:
818 56
                $assocMetadata->addJoinColumn($joinColumnBuilder->build());
819 56
                break;
820
        }
821
822 141
        return $assocMetadata;
823
    }
824
825
    /**
826
     * @param Annotation\Annotation[] $propertyAnnotations
827
     *
828
     * @throws Mapping\MappingException
829
     */
830 109
    private function convertReflectionPropertyToOneToManyAssociationMetadata(
831
        ReflectionProperty $reflectionProperty,
832
        array $propertyAnnotations,
833
        Mapping\ClassMetadata $metadata,
834
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
835
    ) : Mapping\OneToManyAssociationMetadata {
836 109
        $className      = $metadata->getClassName();
837 109
        $fieldName      = $reflectionProperty->getName();
838 109
        $oneToManyAnnot = $propertyAnnotations[Annotation\OneToMany::class];
839 109
        $assocMetadata  = new Mapping\OneToManyAssociationMetadata($fieldName);
840 109
        $targetEntity   = $oneToManyAnnot->targetEntity;
0 ignored issues
show
Bug introduced by
Accessing targetEntity on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
841
842 109
        $assocMetadata->setTargetEntity($targetEntity);
843 109
        $assocMetadata->setCascade($this->getCascade($className, $fieldName, $oneToManyAnnot->cascade));
0 ignored issues
show
Bug introduced by
Accessing cascade on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
844 109
        $assocMetadata->setOrphanRemoval($oneToManyAnnot->orphanRemoval);
0 ignored issues
show
Bug introduced by
Accessing orphanRemoval on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
845 109
        $assocMetadata->setFetchMode($this->getFetchMode($className, $oneToManyAnnot->fetch));
0 ignored issues
show
Bug introduced by
Accessing fetch on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
846 109
        $assocMetadata->setOwningSide(false);
847 109
        $assocMetadata->setMappedBy($oneToManyAnnot->mappedBy);
0 ignored issues
show
Bug introduced by
Accessing mappedBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
848
849 109
        if (! empty($oneToManyAnnot->indexBy)) {
0 ignored issues
show
Bug introduced by
Accessing indexBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
850 8
            $assocMetadata->setIndexedBy($oneToManyAnnot->indexBy);
851
        }
852
853
        // Check for OrderBy
854 109
        if (isset($propertyAnnotations[Annotation\OrderBy::class])) {
855 14
            $orderByAnnot = $propertyAnnotations[Annotation\OrderBy::class];
856
857 14
            $assocMetadata->setOrderBy($orderByAnnot->value);
0 ignored issues
show
Bug introduced by
Accessing value on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
858
        }
859
860
        // Check for Id
861 109
        if (isset($propertyAnnotations[Annotation\Id::class])) {
862
            throw Mapping\MappingException::illegalToManyIdentifierAssociation($className, $fieldName);
863
        }
864
865
        // Check for Cache
866 109
        if (isset($propertyAnnotations[Annotation\Cache::class])) {
867 9
            $cacheBuilder = new Builder\CacheMetadataBuilder($metadataBuildingContext);
868
869
            $cacheBuilder
870 9
                ->withComponentMetadata($metadata)
871 9
                ->withFieldName($fieldName)
872 9
                ->withCacheAnnotation($propertyAnnotations[Annotation\Cache::class]);
873
874 9
            $assocMetadata->setCache($cacheBuilder->build());
875
        }
876
877 109
        return $assocMetadata;
878
    }
879
880
    /**
881
     * @param Annotation\Annotation[] $propertyAnnotations
882
     *
883
     * @throws Mapping\MappingException
884
     */
885 89
    private function convertReflectionPropertyToManyToManyAssociationMetadata(
886
        ReflectionProperty $reflectionProperty,
887
        array $propertyAnnotations,
888
        Mapping\ClassMetadata $metadata,
889
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
890
    ) : Mapping\ManyToManyAssociationMetadata {
891 89
        $className       = $metadata->getClassName();
892 89
        $fieldName       = $reflectionProperty->getName();
893 89
        $manyToManyAnnot = $propertyAnnotations[Annotation\ManyToMany::class];
894 89
        $assocMetadata   = new Mapping\ManyToManyAssociationMetadata($fieldName);
895 89
        $targetEntity    = $manyToManyAnnot->targetEntity;
0 ignored issues
show
Bug introduced by
Accessing targetEntity on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
896
897 89
        $assocMetadata->setTargetEntity($targetEntity);
898 89
        $assocMetadata->setCascade($this->getCascade($className, $fieldName, $manyToManyAnnot->cascade));
0 ignored issues
show
Bug introduced by
Accessing cascade on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
899 89
        $assocMetadata->setOrphanRemoval($manyToManyAnnot->orphanRemoval);
0 ignored issues
show
Bug introduced by
Accessing orphanRemoval on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
900 89
        $assocMetadata->setFetchMode($this->getFetchMode($className, $manyToManyAnnot->fetch));
0 ignored issues
show
Bug introduced by
Accessing fetch on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
901
902 89
        if (! empty($manyToManyAnnot->mappedBy)) {
0 ignored issues
show
Bug introduced by
Accessing mappedBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
903 36
            $assocMetadata->setMappedBy($manyToManyAnnot->mappedBy);
904 36
            $assocMetadata->setOwningSide(false);
905
        }
906
907 89
        if (! empty($manyToManyAnnot->inversedBy)) {
0 ignored issues
show
Bug introduced by
Accessing inversedBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
908 45
            $assocMetadata->setInversedBy($manyToManyAnnot->inversedBy);
909
        }
910
911 89
        if (! empty($manyToManyAnnot->indexBy)) {
0 ignored issues
show
Bug introduced by
Accessing indexBy on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
912 3
            $assocMetadata->setIndexedBy($manyToManyAnnot->indexBy);
913
        }
914
915
        // Check for OrderBy
916 89
        if (isset($propertyAnnotations[Annotation\OrderBy::class])) {
917 3
            $orderByAnnot = $propertyAnnotations[Annotation\OrderBy::class];
918
919 3
            $assocMetadata->setOrderBy($orderByAnnot->value);
0 ignored issues
show
Bug introduced by
Accessing value on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
920
        }
921
922
        // Check for Id
923 89
        if (isset($propertyAnnotations[Annotation\Id::class])) {
924
            throw Mapping\MappingException::illegalToManyIdentifierAssociation($className, $fieldName);
925
        }
926
927
        // Check for Cache
928 89
        if (isset($propertyAnnotations[Annotation\Cache::class])) {
929 2
            $cacheBuilder = new Builder\CacheMetadataBuilder($metadataBuildingContext);
930
931
            $cacheBuilder
932 2
                ->withComponentMetadata($metadata)
933 2
                ->withFieldName($fieldName)
934 2
                ->withCacheAnnotation($propertyAnnotations[Annotation\Cache::class]);
935
936 2
            $assocMetadata->setCache($cacheBuilder->build());
937
        }
938
939
        // Check for owning side to consider join column
940 89
        if (! $assocMetadata->isOwningSide()) {
941 36
            return $assocMetadata;
942
        }
943
944 79
        $joinTableBuilder = new Builder\JoinTableMetadataBuilder($metadataBuildingContext);
945
946
        $joinTableBuilder
947 79
            ->withComponentMetadata($metadata)
948 79
            ->withTargetEntity($targetEntity)
949 79
            ->withFieldName($fieldName);
950
951
        // Check for JoinTable
952 79
        if (isset($propertyAnnotations[Annotation\JoinTable::class])) {
953 71
            $joinTableBuilder->withJoinTableAnnotation($propertyAnnotations[Annotation\JoinTable::class]);
954
        }
955
956 79
        $assocMetadata->setJoinTable($joinTableBuilder->build());
957
958 79
        return $assocMetadata;
959
    }
960
961
    /**
962
     * Parse the given Column as FieldMetadata
963
     */
964 3
    private function convertColumnAnnotationToFieldMetadata(
965
        Annotation\Column $columnAnnot,
966
        string $fieldName,
967
        bool $isVersioned
968
    ) : Mapping\FieldMetadata {
969 3
        $fieldMetadata = new Mapping\FieldMetadata($fieldName);
970
971 3
        $fieldMetadata->setType(Type::getType($columnAnnot->type));
972 3
        $fieldMetadata->setVersioned($isVersioned);
973
974 3
        if (! empty($columnAnnot->name)) {
975 3
            $fieldMetadata->setColumnName($columnAnnot->name);
976
        }
977
978 3
        if (! empty($columnAnnot->columnDefinition)) {
979
            $fieldMetadata->setColumnDefinition($columnAnnot->columnDefinition);
980
        }
981
982 3
        if (! empty($columnAnnot->length)) {
983 3
            $fieldMetadata->setLength($columnAnnot->length);
984
        }
985
986 3
        if ($columnAnnot->options) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $columnAnnot->options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
987
            $fieldMetadata->setOptions($columnAnnot->options);
988
        }
989
990 3
        $fieldMetadata->setScale($columnAnnot->scale);
991 3
        $fieldMetadata->setPrecision($columnAnnot->precision);
992 3
        $fieldMetadata->setNullable($columnAnnot->nullable);
993 3
        $fieldMetadata->setUnique($columnAnnot->unique);
994
995 3
        return $fieldMetadata;
996
    }
997
998
    /**
999
     * @param Annotation\Annotation[] $classAnnotations
1000
     */
1001 373
    private function attachLifecycleCallbacks(
1002
        array $classAnnotations,
1003
        ReflectionClass $reflectionClass,
1004
        Mapping\ClassMetadata $metadata
1005
    ) : void {
1006
        // Evaluate @HasLifecycleCallbacks annotation
1007 373
        if (isset($classAnnotations[Annotation\HasLifecycleCallbacks::class])) {
1008
            $eventMap = [
1009 14
                Events::prePersist  => Annotation\PrePersist::class,
1010 14
                Events::postPersist => Annotation\PostPersist::class,
1011 14
                Events::preUpdate   => Annotation\PreUpdate::class,
1012 14
                Events::postUpdate  => Annotation\PostUpdate::class,
1013 14
                Events::preRemove   => Annotation\PreRemove::class,
1014 14
                Events::postRemove  => Annotation\PostRemove::class,
1015 14
                Events::postLoad    => Annotation\PostLoad::class,
1016 14
                Events::preFlush    => Annotation\PreFlush::class,
1017
            ];
1018
1019
            /** @var ReflectionMethod $reflectionMethod */
1020 14
            foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
1021 13
                $annotations = $this->getMethodAnnotations($reflectionMethod);
1022
1023 13
                foreach ($eventMap as $eventName => $annotationClassName) {
1024 13
                    if (isset($annotations[$annotationClassName])) {
1025 12
                        $metadata->addLifecycleCallback($eventName, $reflectionMethod->getName());
1026
                    }
1027
                }
1028
            }
1029
        }
1030 373
    }
1031
1032
    /**
1033
     * @param Annotation\Annotation[] $classAnnotations
1034
     *
1035
     * @throws ReflectionException
1036
     * @throws Mapping\MappingException
1037
     */
1038 373
    private function attachEntityListeners(
1039
        array $classAnnotations,
1040
        Mapping\ClassMetadata $metadata
1041
    ) : void {
1042
        // Evaluate @EntityListeners annotation
1043 373
        if (isset($classAnnotations[Annotation\EntityListeners::class])) {
1044
            /** @var Annotation\EntityListeners $entityListenersAnnot */
1045 8
            $entityListenersAnnot = $classAnnotations[Annotation\EntityListeners::class];
1046
            $eventMap             = [
1047 8
                Events::prePersist  => Annotation\PrePersist::class,
1048 8
                Events::postPersist => Annotation\PostPersist::class,
1049 8
                Events::preUpdate   => Annotation\PreUpdate::class,
1050 8
                Events::postUpdate  => Annotation\PostUpdate::class,
1051 8
                Events::preRemove   => Annotation\PreRemove::class,
1052 8
                Events::postRemove  => Annotation\PostRemove::class,
1053 8
                Events::postLoad    => Annotation\PostLoad::class,
1054 8
                Events::preFlush    => Annotation\PreFlush::class,
1055
            ];
1056
1057 8
            foreach ($entityListenersAnnot->value as $listenerClassName) {
1058 8
                if (! class_exists($listenerClassName)) {
1059
                    throw Mapping\MappingException::entityListenerClassNotFound(
1060
                        $listenerClassName,
1061
                        $metadata->getClassName()
1062
                    );
1063
                }
1064
1065 8
                $listenerClass = new ReflectionClass($listenerClassName);
1066
1067
                /** @var ReflectionMethod $reflectionMethod */
1068 8
                foreach ($listenerClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
1069 8
                    $annotations = $this->getMethodAnnotations($reflectionMethod);
1070
1071 8
                    foreach ($eventMap as $eventName => $annotationClassName) {
1072 8
                        if (isset($annotations[$annotationClassName])) {
1073 6
                            $metadata->addEntityListener($eventName, $listenerClassName, $reflectionMethod->getName());
1074
                        }
1075
                    }
1076
                }
1077
            }
1078
        }
1079 373
    }
1080
1081
    /**
1082
     * @param Annotation\Annotation[] $classAnnotations
1083
     *
1084
     * @throws Mapping\MappingException
1085
     */
1086 370
    private function attachPropertyOverrides(
1087
        array $classAnnotations,
1088
        ReflectionClass $reflectionClass,
0 ignored issues
show
Unused Code introduced by
The parameter $reflectionClass is not used and could be removed. ( Ignorable by Annotation )

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

1088
        /** @scrutinizer ignore-unused */ ReflectionClass $reflectionClass,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1089
        Mapping\ClassMetadata $metadata,
1090
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext
1091
    ) : void {
1092
        // Evaluate AssociationOverrides annotation
1093 370
        if (isset($classAnnotations[Annotation\AssociationOverrides::class])) {
1094 5
            $associationOverridesAnnot = $classAnnotations[Annotation\AssociationOverrides::class];
1095
1096 5
            foreach ($associationOverridesAnnot->value as $associationOverride) {
0 ignored issues
show
Bug introduced by
Accessing value on the interface Doctrine\ORM\Annotation\Annotation suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
1097 5
                $fieldName = $associationOverride->name;
1098 5
                $property  = $metadata->getProperty($fieldName);
1099
1100 5
                if (! $property) {
1101
                    throw Mapping\MappingException::invalidOverrideFieldName($metadata->getClassName(), $fieldName);
1102
                }
1103
1104 5
                $existingClass = get_class($property);
1105 5
                $override      = new $existingClass($fieldName);
1106
1107 5
                $override->setTargetEntity($property->getTargetEntity());
0 ignored issues
show
Bug introduced by
The method getTargetEntity() does not exist on Doctrine\ORM\Mapping\Property. It seems like you code against a sub-type of Doctrine\ORM\Mapping\Property such as Doctrine\ORM\Mapping\AssociationMetadata. ( Ignorable by Annotation )

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

1107
                $override->setTargetEntity($property->/** @scrutinizer ignore-call */ getTargetEntity());
Loading history...
1108
1109
                // Check for JoinColumn/JoinColumns annotations
1110 5
                if ($associationOverride->joinColumns) {
1111 3
                    $joinColumnBuilder = new Builder\JoinColumnMetadataBuilder($metadataBuildingContext);
1112
1113
                    $joinColumnBuilder
1114 3
                        ->withComponentMetadata($metadata)
1115 3
                        ->withFieldName($fieldName);
1116
1117 3
                    $joinColumns = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $joinColumns is dead and can be removed.
Loading history...
1118
1119 3
                    foreach ($associationOverride->joinColumns as $joinColumnAnnotation) {
1120 3
                        $joinColumnBuilder->withJoinColumnAnnotation($joinColumnAnnotation);
1121
1122 3
                        $override->addJoinColumn($joinColumnBuilder->build());
1123
                    }
1124
                }
1125
1126
                // Check for JoinTable annotations
1127 5
                if ($associationOverride->joinTable) {
1128 2
                    $joinTableBuilder = new Builder\JoinTableMetadataBuilder($metadataBuildingContext);
1129
1130
                    $joinTableBuilder
1131 2
                        ->withComponentMetadata($metadata)
1132 2
                        ->withFieldName($fieldName)
1133 2
                        ->withTargetEntity($property->getTargetEntity())
1134 2
                        ->withJoinTableAnnotation($associationOverride->joinTable);
1135
1136 2
                    $override->setJoinTable($joinTableBuilder->build());
1137
                }
1138
1139
                // Check for inversedBy
1140 5
                if ($associationOverride->inversedBy) {
1141 1
                    $override->setInversedBy($associationOverride->inversedBy);
1142
                }
1143
1144
                // Check for fetch
1145 5
                if ($associationOverride->fetch) {
1146 1
                    $override->setFetchMode(
1147 1
                        constant(Mapping\FetchMode::class . '::' . $associationOverride->fetch)
1148
                    );
1149
                }
1150
1151 5
                $metadata->setPropertyOverride($override);
1152
            }
1153
        }
1154
1155
        // Evaluate AttributeOverrides annotation
1156 370
        if (isset($classAnnotations[Annotation\AttributeOverrides::class])) {
1157 3
            $attributeOverridesAnnot = $classAnnotations[Annotation\AttributeOverrides::class];
1158
1159 3
            foreach ($attributeOverridesAnnot->value as $attributeOverrideAnnot) {
1160 3
                $fieldMetadata = $this->convertColumnAnnotationToFieldMetadata(
1161 3
                    $attributeOverrideAnnot->column,
1162 3
                    $attributeOverrideAnnot->name,
1163 3
                    false
1164
                );
1165
1166 3
                $metadata->setPropertyOverride($fieldMetadata);
1167
            }
1168
        }
1169 370
    }
1170
1171
    /**
1172
     * Attempts to resolve the cascade modes.
1173
     *
1174
     * @param string   $className        The class name.
1175
     * @param string   $fieldName        The field name.
1176
     * @param string[] $originalCascades The original unprocessed field cascades.
1177
     *
1178
     * @return string[] The processed field cascades.
1179
     *
1180
     * @throws Mapping\MappingException If a cascade option is not valid.
1181
     */
1182 251
    private function getCascade(string $className, string $fieldName, array $originalCascades) : array
1183
    {
1184 251
        $cascadeTypes = ['remove', 'persist', 'refresh'];
1185 251
        $cascades     = array_map('strtolower', $originalCascades);
1186
1187 251
        if (in_array('all', $cascades, true)) {
1188 23
            $cascades = $cascadeTypes;
1189
        }
1190
1191 251
        if (count($cascades) !== count(array_intersect($cascades, $cascadeTypes))) {
1192
            $diffCascades = array_diff($cascades, array_intersect($cascades, $cascadeTypes));
1193
1194
            throw Mapping\MappingException::invalidCascadeOption($diffCascades, $className, $fieldName);
1195
        }
1196
1197 251
        return $cascades;
1198
    }
1199
1200
    /**
1201
     * Attempts to resolve the fetch mode.
1202
     *
1203
     * @param string $className The class name.
1204
     * @param string $fetchMode The fetch mode.
1205
     *
1206
     * @return string The fetch mode as defined in ClassMetadata.
1207
     *
1208
     * @throws Mapping\MappingException If the fetch mode is not valid.
1209
     */
1210 251
    private function getFetchMode($className, $fetchMode) : string
1211
    {
1212 251
        $fetchModeConstant = sprintf('%s::%s', Mapping\FetchMode::class, $fetchMode);
1213
1214 251
        if (! defined($fetchModeConstant)) {
1215
            throw Mapping\MappingException::invalidFetchMode($className, $fetchMode);
1216
        }
1217
1218 251
        return constant($fetchModeConstant);
1219
    }
1220
1221
    /**
1222
     * @return Annotation\Annotation[]
1223
     */
1224 379
    private function getClassAnnotations(ReflectionClass $reflectionClass) : array
1225
    {
1226 379
        $classAnnotations = $this->reader->getClassAnnotations($reflectionClass);
1227
1228 379
        foreach ($classAnnotations as $key => $annot) {
1229 373
            if (! is_numeric($key)) {
1230
                continue;
1231
            }
1232
1233 373
            $classAnnotations[get_class($annot)] = $annot;
1234
        }
1235
1236 379
        return $classAnnotations;
1237
    }
1238
1239
    /**
1240
     * @return Annotation\Annotation[]
1241
     */
1242 373
    private function getPropertyAnnotations(ReflectionProperty $reflectionProperty) : array
1243
    {
1244 373
        $propertyAnnotations = $this->reader->getPropertyAnnotations($reflectionProperty);
1245
1246 372
        foreach ($propertyAnnotations as $key => $annot) {
1247 372
            if (! is_numeric($key)) {
1248
                continue;
1249
            }
1250
1251 372
            $propertyAnnotations[get_class($annot)] = $annot;
1252
        }
1253
1254 372
        return $propertyAnnotations;
1255
    }
1256
1257
    /**
1258
     * @return Annotation\Annotation[]
1259
     */
1260 21
    private function getMethodAnnotations(ReflectionMethod $reflectionMethod) : array
1261
    {
1262 21
        $methodAnnotations = $this->reader->getMethodAnnotations($reflectionMethod);
1263
1264 21
        foreach ($methodAnnotations as $key => $annot) {
1265 18
            if (! is_numeric($key)) {
1266
                continue;
1267
            }
1268
1269 18
            $methodAnnotations[get_class($annot)] = $annot;
1270
        }
1271
1272 21
        return $methodAnnotations;
1273
    }
1274
}
1275