Completed
Pull Request — master (#5586)
by Mihai
11:08
created

YamlDriver::loadMetadataForClass()   F

Complexity

Conditions 156
Paths > 20000

Size

Total Lines 619
Code Lines 326

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 280
CRAP Score 182.284

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 619
ccs 280
cts 312
cp 0.8974
rs 2
cc 156
eloc 326
nc 255590401
nop 2
crap 182.284

How to fix   Long Method    Complexity   

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
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Mapping\Driver;
21
22
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
23
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
24
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
25
use Doctrine\ORM\Mapping\MappingException;
26
use Symfony\Component\Yaml\Yaml;
27
28
/**
29
 * The YamlDriver reads the mapping metadata from yaml schema files.
30
 *
31
 * @since 2.0
32
 * @author Benjamin Eberlei <[email protected]>
33
 * @author Guilherme Blanco <[email protected]>
34
 * @author Jonathan H. Wage <[email protected]>
35
 * @author Roman Borschel <[email protected]>
36
 */
37
class YamlDriver extends FileDriver
38
{
39
    const DEFAULT_FILE_EXTENSION = '.dcm.yml';
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 67
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
45
    {
46 67
        parent::__construct($locator, $fileExtension);
47 67
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 62
    public function loadMetadataForClass($className, ClassMetadata $metadata)
53
    {
54
        /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */
55 62
        $element = $this->getElement($className);
56
57 60
        if ($element['type'] == 'entity') {
58 58
            if (isset($element['repositoryClass'])) {
59
                $metadata->setCustomRepositoryClass($element['repositoryClass']);
60
            }
61 58
            if (isset($element['persisterClass'])) {
62
                $metadata->setCustomPersisterClass($element['persisterClass']);
63
            }
64 58
            if (isset($element['readOnly']) && $element['readOnly'] == true) {
65 58
                $metadata->markReadOnly();
66
            }
67 14
        } else if ($element['type'] == 'mappedSuperclass') {
68 12
            $metadata->setCustomRepositoryClass(
69 12
                isset($element['repositoryClass']) ? $element['repositoryClass'] : null
70
            );
71 12
            $metadata->setCustomPersisterClass(
72 12
                isset($element['persisterClass']) ? $element['persisterClass'] : null
73
            );
74 12
            $metadata->isMappedSuperclass = true;
75 2
        } else if ($element['type'] == 'embeddable') {
76
            $metadata->isEmbeddedClass = true;
77
        } else {
78 2
            throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
79
        }
80
81
        // Evaluate root level properties
82 58
        $primaryTable = array();
83
84 58
        if (isset($element['table'])) {
85 24
            $primaryTable['name'] = $element['table'];
86
        }
87
88 58
        if (isset($element['schema'])) {
89 2
            $primaryTable['schema'] = $element['schema'];
90
        }
91
92
        // Evaluate second level cache
93 58
        if (isset($element['cache'])) {
94 2
            $metadata->enableCache($this->cacheToArray($element['cache']));
95
        }
96
97 58
        $metadata->setPrimaryTable($primaryTable);
98
99
        // Evaluate named queries
100 58
        if (isset($element['namedQueries'])) {
101 6
            foreach ($element['namedQueries'] as $name => $queryMapping) {
102 6
                if (is_string($queryMapping)) {
103 6
                    $queryMapping = array('query' => $queryMapping);
104
                }
105
106 6
                if ( ! isset($queryMapping['name'])) {
107 6
                    $queryMapping['name'] = $name;
108
                }
109
110 6
                $metadata->addNamedQuery($queryMapping);
111
            }
112
        }
113
114
        // Evaluate named native queries
115 58
        if (isset($element['namedNativeQueries'])) {
116 6
            foreach ($element['namedNativeQueries'] as $name => $mappingElement) {
117 6
                if (!isset($mappingElement['name'])) {
118 6
                    $mappingElement['name'] = $name;
119
                }
120 6
                $metadata->addNamedNativeQuery(array(
121 6
                    'name'              => $mappingElement['name'],
122 6
                    'query'             => isset($mappingElement['query']) ? $mappingElement['query'] : null,
123 6
                    'resultClass'       => isset($mappingElement['resultClass']) ? $mappingElement['resultClass'] : null,
124 6
                    'resultSetMapping'  => isset($mappingElement['resultSetMapping']) ? $mappingElement['resultSetMapping'] : null,
125
                ));
126
            }
127
        }
128
129
        // Evaluate sql result set mappings
130 58
        if (isset($element['sqlResultSetMappings'])) {
131 6
            foreach ($element['sqlResultSetMappings'] as $name => $resultSetMapping) {
132 6
                if (!isset($resultSetMapping['name'])) {
133 6
                    $resultSetMapping['name'] = $name;
134
                }
135
136 6
                $entities = array();
137 6
                $columns  = array();
138 6
                if (isset($resultSetMapping['entityResult'])) {
139 6
                    foreach ($resultSetMapping['entityResult'] as $entityResultElement) {
140
                        $entityResult = array(
141 6
                            'fields'                => array(),
142 6
                            'entityClass'           => isset($entityResultElement['entityClass']) ? $entityResultElement['entityClass'] : null,
143 6
                            'discriminatorColumn'   => isset($entityResultElement['discriminatorColumn']) ? $entityResultElement['discriminatorColumn'] : null,
144
                        );
145
146 6
                        if (isset($entityResultElement['fieldResult'])) {
147 6
                            foreach ($entityResultElement['fieldResult'] as $fieldResultElement) {
148 6
                                $entityResult['fields'][] = array(
149 6
                                    'name'      => isset($fieldResultElement['name']) ? $fieldResultElement['name'] : null,
150 6
                                    'column'    => isset($fieldResultElement['column']) ? $fieldResultElement['column'] : null,
151
                                );
152
                            }
153
                        }
154
155 6
                        $entities[] = $entityResult;
156
                    }
157
                }
158
159
160 6
                if (isset($resultSetMapping['columnResult'])) {
161 6
                    foreach ($resultSetMapping['columnResult'] as $columnResultAnnot) {
162 6
                        $columns[] = array(
163 6
                            'name' => isset($columnResultAnnot['name']) ? $columnResultAnnot['name'] : null,
164
                        );
165
                    }
166
                }
167
168 6
                $metadata->addSqlResultSetMapping(array(
169 6
                    'name'          => $resultSetMapping['name'],
170 6
                    'entities'      => $entities,
171 6
                    'columns'       => $columns
172
                ));
173
            }
174
        }
175
176 58
        if (isset($element['inheritanceType'])) {
177 18
            $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType'])));
178
179 18
            if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
180
                // Evaluate discriminatorColumn
181 18
                if (isset($element['discriminatorColumn'])) {
182 12
                    $discrColumn = $element['discriminatorColumn'];
183 12
                    $metadata->setDiscriminatorColumn(array(
184 12
                        'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null,
185 12
                        'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string',
186 12
                        'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255,
187 12
                        'columnDefinition' => isset($discrColumn['columnDefinition']) ? (string) $discrColumn['columnDefinition'] : null
188
                    ));
189
                } else {
190 12
                    $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
191
                }
192
193
                // Evaluate discriminatorMap
194 18
                if (isset($element['discriminatorMap'])) {
195 18
                    $metadata->setDiscriminatorMap($element['discriminatorMap']);
196
                }
197
            }
198
        }
199
200
201
        // Evaluate changeTrackingPolicy
202 58
        if (isset($element['changeTrackingPolicy'])) {
203
            $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
204
                . strtoupper($element['changeTrackingPolicy'])));
205
        }
206
207
        // Evaluate indexes
208 58
        if (isset($element['indexes'])) {
209 8
            foreach ($element['indexes'] as $name => $indexYml) {
210 8
                if ( ! isset($indexYml['name'])) {
211 8
                    $indexYml['name'] = $name;
212
                }
213
214 8
                if (is_string($indexYml['columns'])) {
215 8
                    $index = array('columns' => array_map('trim', explode(',', $indexYml['columns'])));
216
                } else {
217
                    $index = array('columns' => $indexYml['columns']);
218
                }
219
220 8
                if (isset($indexYml['flags'])) {
221 2
                    if (is_string($indexYml['flags'])) {
222 2
                        $index['flags'] = array_map('trim', explode(',', $indexYml['flags']));
223
                    } else {
224
                        $index['flags'] = $indexYml['flags'];
225
                    }
226
                }
227
228 8
                if (isset($indexYml['options'])) {
229 2
                    $index['options'] = $indexYml['options'];
230
                }
231
232 8
                $metadata->table['indexes'][$indexYml['name']] = $index;
233
            }
234
        }
235
236
        // Evaluate uniqueConstraints
237 58
        if (isset($element['uniqueConstraints'])) {
238 7
            foreach ($element['uniqueConstraints'] as $name => $uniqueYml) {
239 7
                if ( ! isset($uniqueYml['name'])) {
240 7
                    $uniqueYml['name'] = $name;
241
                }
242
243 7
                if (is_string($uniqueYml['columns'])) {
244 6
                    $unique = array('columns' => array_map('trim', explode(',', $uniqueYml['columns'])));
245
                } else {
246 1
                    $unique = array('columns' => $uniqueYml['columns']);
247
                }
248
249 7
                if (isset($uniqueYml['options'])) {
250 4
                    $unique['options'] = $uniqueYml['options'];
251
                }
252
253 7
                $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique;
254
            }
255
        }
256
257 58
        if (isset($element['options'])) {
258 6
            $metadata->table['options'] = $element['options'];
259
        }
260
261 58
        $associationIds = array();
262 58
        if (isset($element['id'])) {
263
            // Evaluate identifier settings
264 54
            foreach ($element['id'] as $name => $idElement) {
265 54
                if (isset($idElement['associationKey']) && $idElement['associationKey'] == true) {
266
                    $associationIds[$name] = true;
267
                    continue;
268
                }
269
270
                $mapping = array(
271 54
                    'id' => true,
272 54
                    'fieldName' => $name
273
                );
274
275 54
                if (isset($idElement['type'])) {
276 34
                    $mapping['type'] = $idElement['type'];
277
                }
278
279 54
                if (isset($idElement['column'])) {
280 6
                    $mapping['columnName'] = $idElement['column'];
281
                }
282
283 54
                if (isset($idElement['length'])) {
284 6
                    $mapping['length'] = $idElement['length'];
285
                }
286
287 54
                if (isset($idElement['columnDefinition'])) {
288 2
                    $mapping['columnDefinition'] = $idElement['columnDefinition'];
289
                }
290
291 54
                if (isset($idElement['options'])) {
292 4
                    $mapping['options'] = $idElement['options'];
293
                }
294
295 54
                $metadata->mapField($mapping);
296
297 54
                if (isset($idElement['generator'])) {
298 51
                    $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
299 51
                        . strtoupper($idElement['generator']['strategy'])));
300
                }
301
                // Check for SequenceGenerator/TableGenerator definition
302 54
                if (isset($idElement['sequenceGenerator'])) {
303 4
                    $metadata->setSequenceGeneratorDefinition($idElement['sequenceGenerator']);
304 50
                } else if (isset($idElement['customIdGenerator'])) {
305 4
                    $customGenerator = $idElement['customIdGenerator'];
306 4
                    $metadata->setCustomGeneratorDefinition(array(
307 4
                        'class' => (string) $customGenerator['class']
308
                    ));
309 46
                } else if (isset($idElement['tableGenerator'])) {
310 54
                    throw MappingException::tableIdGeneratorNotImplemented($className);
311
                }
312
            }
313
        }
314
315
        // Evaluate fields
316 58
        if (isset($element['fields'])) {
317 41
            foreach ($element['fields'] as $name => $fieldMapping) {
318
319 41
                $mapping = $this->columnToArray($name, $fieldMapping);
320
321 41
                if (isset($fieldMapping['id'])) {
322
                    $mapping['id'] = true;
323
                    if (isset($fieldMapping['generator']['strategy'])) {
324
                        $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
325
                            . strtoupper($fieldMapping['generator']['strategy'])));
326
                    }
327
                }
328
329 41
                if (isset($mapping['version'])) {
330 4
                    $metadata->setVersionMapping($mapping);
331 4
                    unset($mapping['version']);
332
                }
333
334 41
                $metadata->mapField($mapping);
335
            }
336
        }
337
338 58
        if (isset($element['embedded'])) {
339
            foreach ($element['embedded'] as $name => $embeddedMapping) {
340
                $mapping = array(
341
                    'fieldName' => $name,
342
                    'class' => $embeddedMapping['class'],
343
                    'columnPrefix' => isset($embeddedMapping['columnPrefix']) ? $embeddedMapping['columnPrefix'] : null,
344
                );
345
                $metadata->mapEmbedded($mapping);
346
            }
347
        }
348
349
        // Evaluate oneToOne relationships
350 58
        if (isset($element['oneToOne'])) {
351 13
            foreach ($element['oneToOne'] as $name => $oneToOneElement) {
352
                $mapping = array(
353 13
                    'fieldName' => $name,
354 13
                    'targetEntity' => $oneToOneElement['targetEntity']
355
                );
356
357 13
                if (isset($associationIds[$mapping['fieldName']])) {
358
                    $mapping['id'] = true;
359
                }
360
361 13
                if (isset($oneToOneElement['fetch'])) {
362 3
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToOneElement['fetch']);
363
                }
364
365 13
                if (isset($oneToOneElement['mappedBy'])) {
366 1
                    $mapping['mappedBy'] = $oneToOneElement['mappedBy'];
367
                } else {
368 12
                    if (isset($oneToOneElement['inversedBy'])) {
369 12
                        $mapping['inversedBy'] = $oneToOneElement['inversedBy'];
370
                    }
371
372 12
                    $joinColumns = array();
373
374 12
                    if (isset($oneToOneElement['joinColumn'])) {
375 11
                        $joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']);
376 1
                    } else if (isset($oneToOneElement['joinColumns'])) {
377 1
                        foreach ($oneToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
378 1
                            if ( ! isset($joinColumnElement['name'])) {
379 1
                                $joinColumnElement['name'] = $joinColumnName;
380
                            }
381
382 1
                            $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
383
                        }
384
                    }
385
386 12
                    $mapping['joinColumns'] = $joinColumns;
387
                }
388
389 13
                if (isset($oneToOneElement['cascade'])) {
390 9
                    $mapping['cascade'] = $oneToOneElement['cascade'];
391
                }
392
393 13
                if (isset($oneToOneElement['orphanRemoval'])) {
394 5
                    $mapping['orphanRemoval'] = (bool) $oneToOneElement['orphanRemoval'];
395
                }
396
397
                // Evaluate second level cache
398 13
                if (isset($oneToOneElement['cache'])) {
399
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToOneElement['cache']));
400
                }
401
402 13
                $metadata->mapOneToOne($mapping);
403
            }
404
        }
405
406
        // Evaluate oneToMany relationships
407 58
        if (isset($element['oneToMany'])) {
408 8
            foreach ($element['oneToMany'] as $name => $oneToManyElement) {
409
                $mapping = array(
410 8
                    'fieldName' => $name,
411 8
                    'targetEntity' => $oneToManyElement['targetEntity'],
412 8
                    'mappedBy' => $oneToManyElement['mappedBy']
413
                );
414
415 8
                if (isset($oneToManyElement['fetch'])) {
416 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToManyElement['fetch']);
417
                }
418
419 8
                if (isset($oneToManyElement['persister'])) {
420
                    $mapping['persister'] = $oneToManyElement['persister'];
421
                }
422
423 8
                if (isset($oneToManyElement['cascade'])) {
424 6
                    $mapping['cascade'] = $oneToManyElement['cascade'];
425
                }
426
427 8
                if (isset($oneToManyElement['orphanRemoval'])) {
428 6
                    $mapping['orphanRemoval'] = (bool) $oneToManyElement['orphanRemoval'];
429
                }
430
431 8
                if (isset($oneToManyElement['orderBy'])) {
432 8
                    $mapping['orderBy'] = $oneToManyElement['orderBy'];
433
                }
434
435 8
                if (isset($oneToManyElement['indexBy'])) {
436
                    $mapping['indexBy'] = $oneToManyElement['indexBy'];
437
                }
438
439
440
                // Evaluate second level cache
441 8
                if (isset($oneToManyElement['cache'])) {
442 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToManyElement['cache']));
443
                }
444
445 8
                $metadata->mapOneToMany($mapping);
446
            }
447
        }
448
449
        // Evaluate manyToOne relationships
450 58
        if (isset($element['manyToOne'])) {
451 10
            foreach ($element['manyToOne'] as $name => $manyToOneElement) {
452
                $mapping = array(
453 10
                    'fieldName' => $name,
454 10
                    'targetEntity' => $manyToOneElement['targetEntity']
455
                );
456
457 10
                if (isset($associationIds[$mapping['fieldName']])) {
458
                    $mapping['id'] = true;
459
                }
460
461 10
                if (isset($manyToOneElement['fetch'])) {
462 1
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToOneElement['fetch']);
463
                }
464
465 10
                if (isset($manyToOneElement['inversedBy'])) {
466 2
                    $mapping['inversedBy'] = $manyToOneElement['inversedBy'];
467
                }
468
469 10
                $joinColumns = array();
470
471 10
                if (isset($manyToOneElement['joinColumn'])) {
472 4
                    $joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']);
473 6
                } else if (isset($manyToOneElement['joinColumns'])) {
474 3
                    foreach ($manyToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
475 3
                        if ( ! isset($joinColumnElement['name'])) {
476 3
                            $joinColumnElement['name'] = $joinColumnName;
477
                        }
478
479 3
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
480
                    }
481
                }
482
483 10
                $mapping['joinColumns'] = $joinColumns;
484
485 10
                if (isset($manyToOneElement['cascade'])) {
486 5
                    $mapping['cascade'] = $manyToOneElement['cascade'];
487
                }
488
489
                // Evaluate second level cache
490 10
                if (isset($manyToOneElement['cache'])) {
491 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToOneElement['cache']));
492
                }
493
494 10
                $metadata->mapManyToOne($mapping);
495
            }
496
        }
497
498
        // Evaluate manyToMany relationships
499 58
        if (isset($element['manyToMany'])) {
500 17
            foreach ($element['manyToMany'] as $name => $manyToManyElement) {
501
                $mapping = array(
502 17
                    'fieldName' => $name,
503 17
                    'targetEntity' => $manyToManyElement['targetEntity']
504
                );
505
506 17
                if (isset($manyToManyElement['fetch'])) {
507 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToManyElement['fetch']);
508
                }
509
510 17
                if (isset($manyToManyElement['persister'])) {
511
                    $mapping['persister'] = $manyToManyElement['persister'];
512
                }
513
514 17
                if (isset($manyToManyElement['mappedBy'])) {
515 2
                    $mapping['mappedBy'] = $manyToManyElement['mappedBy'];
516 15
                } else if (isset($manyToManyElement['joinTable'])) {
517
518 13
                    $joinTableElement = $manyToManyElement['joinTable'];
519
                    $joinTable = array(
520 13
                        'name' => $joinTableElement['name']
521
                    );
522
523 13
                    if (isset($joinTableElement['schema'])) {
524
                        $joinTable['schema'] = $joinTableElement['schema'];
525
                    }
526
527 13
                    if (isset($joinTableElement['joinColumns'])) {
528 13
                        foreach ($joinTableElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
529 13
                            if ( ! isset($joinColumnElement['name'])) {
530 12
                                $joinColumnElement['name'] = $joinColumnName;
531
                            }
532 13
                            $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
533
                        }
534
                    }
535
536 13
                    if (isset($joinTableElement['inverseJoinColumns'])) {
537 13
                        foreach ($joinTableElement['inverseJoinColumns'] as $joinColumnName => $joinColumnElement) {
538 13
                            if ( ! isset($joinColumnElement['name'])) {
539 12
                                $joinColumnElement['name'] = $joinColumnName;
540
                            }
541 13
                            $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
542
                        }
543
                    }
544
545 13
                    $mapping['joinTable'] = $joinTable;
546
                }
547
548 17
                if (isset($manyToManyElement['inversedBy'])) {
549
                    $mapping['inversedBy'] = $manyToManyElement['inversedBy'];
550
                }
551
552 17
                if (isset($manyToManyElement['cascade'])) {
553 12
                    $mapping['cascade'] = $manyToManyElement['cascade'];
554
                }
555
556 17
                if (isset($manyToManyElement['orderBy'])) {
557
                    $mapping['orderBy'] = $manyToManyElement['orderBy'];
558
                }
559
560 17
                if (isset($manyToManyElement['indexBy'])) {
561
                    $mapping['indexBy'] = $manyToManyElement['indexBy'];
562
                }
563
564 17
                if (isset($manyToManyElement['orphanRemoval'])) {
565
                    $mapping['orphanRemoval'] = (bool) $manyToManyElement['orphanRemoval'];
566
                }
567
568
                // Evaluate second level cache
569 17
                if (isset($manyToManyElement['cache'])) {
570
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToManyElement['cache']));
571
                }
572
573 17
                $metadata->mapManyToMany($mapping);
574
            }
575
        }
576
577
        // Evaluate associationOverride
578 58
        if (isset($element['associationOverride']) && is_array($element['associationOverride'])) {
579
580 6
            foreach ($element['associationOverride'] as $fieldName => $associationOverrideElement) {
581 6
                $override   = array();
582
583
                // Check for joinColumn
584 6
                if (isset($associationOverrideElement['joinColumn'])) {
585 4
                    $joinColumns = array();
586 4
                    foreach ($associationOverrideElement['joinColumn'] as $name => $joinColumnElement) {
587 4
                        if ( ! isset($joinColumnElement['name'])) {
588
                            $joinColumnElement['name'] = $name;
589
                        }
590 4
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
591
                    }
592 4
                    $override['joinColumns'] = $joinColumns;
593
                }
594
595
                // Check for joinTable
596 6
                if (isset($associationOverrideElement['joinTable'])) {
597
598 4
                    $joinTableElement   = $associationOverrideElement['joinTable'];
599
                    $joinTable          =  array(
600 4
                        'name' => $joinTableElement['name']
601
                    );
602
603 4
                    if (isset($joinTableElement['schema'])) {
604
                        $joinTable['schema'] = $joinTableElement['schema'];
605
                    }
606
607 4
                    foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
608 4
                        if ( ! isset($joinColumnElement['name'])) {
609 4
                            $joinColumnElement['name'] = $name;
610
                        }
611
612 4
                        $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
613
                    }
614
615 4
                    foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
616 4
                        if ( ! isset($joinColumnElement['name'])) {
617 4
                            $joinColumnElement['name'] = $name;
618
                        }
619
620 4
                        $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
621
                    }
622
623 4
                    $override['joinTable'] = $joinTable;
624
                }
625
626
                // Check for inversedBy
627 6
                if (isset($associationOverrideElement['inversedBy'])) {
628 2
                    $override['inversedBy'] = (string) $associationOverrideElement['inversedBy'];
629
                }
630
631 6
                $metadata->setAssociationOverride($fieldName, $override);
632
            }
633
        }
634
635
        // Evaluate associationOverride
636 58
        if (isset($element['attributeOverride']) && is_array($element['attributeOverride'])) {
637
638 4
            foreach ($element['attributeOverride'] as $fieldName => $attributeOverrideElement) {
639 4
                $mapping = $this->columnToArray($fieldName, $attributeOverrideElement);
640 4
                $metadata->setAttributeOverride($fieldName, $mapping);
641
            }
642
        }
643
644
        // Evaluate lifeCycleCallbacks
645 58
        if (isset($element['lifecycleCallbacks'])) {
646 7
            foreach ($element['lifecycleCallbacks'] as $type => $methods) {
647 6
                foreach ($methods as $method) {
648 6
                    $metadata->addLifecycleCallback($method, constant('Doctrine\ORM\Events::' . $type));
649
                }
650
            }
651
        }
652
653
        // Evaluate entityListeners
654 58
        if (isset($element['entityListeners'])) {
655 8
            foreach ($element['entityListeners'] as $className => $entityListener) {
656
                // Evaluate the listener using naming convention.
657 8
                if (empty($entityListener)) {
658 4
                    EntityListenerBuilder::bindEntityListener($metadata, $className);
0 ignored issues
show
Compatibility introduced by
$metadata of type object<Doctrine\ORM\Mapping\ClassMetadataInfo> is not a sub-type of object<Doctrine\ORM\Mapping\ClassMetadata>. It seems like you assume a child class of the class Doctrine\ORM\Mapping\ClassMetadataInfo to be always present.

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

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

Loading history...
659
660 4
                    continue;
661
                }
662
663 4
                foreach ($entityListener as $eventName => $callbackElement) {
664 4
                    foreach ($callbackElement as $methodName) {
665 4
                        $metadata->addEntityListener($eventName, $className, $methodName);
666
                    }
667
                }
668
            }
669
        }
670 58
    }
671
672
    /**
673
     * Constructs a joinColumn mapping array based on the information
674
     * found in the given join column element.
675
     *
676
     * @param array $joinColumnElement The array join column element.
677
     *
678
     * @return array The mapping array.
679
     */
680 19
    private function joinColumnToArray($joinColumnElement)
681
    {
682 19
        $joinColumn = array();
683 19
        if (isset($joinColumnElement['referencedColumnName'])) {
684 19
            $joinColumn['referencedColumnName'] = (string) $joinColumnElement['referencedColumnName'];
685
        }
686
687 19
        if (isset($joinColumnElement['name'])) {
688 15
            $joinColumn['name'] = (string) $joinColumnElement['name'];
689
        }
690
691 19
        if (isset($joinColumnElement['fieldName'])) {
692
            $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName'];
693
        }
694
695 19
        if (isset($joinColumnElement['unique'])) {
696 6
            $joinColumn['unique'] = (bool) $joinColumnElement['unique'];
697
        }
698
699 19
        if (isset($joinColumnElement['nullable'])) {
700 6
            $joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
701
        }
702
703 19
        if (isset($joinColumnElement['onDelete'])) {
704 6
            $joinColumn['onDelete'] = $joinColumnElement['onDelete'];
705
        }
706
707 19
        if (isset($joinColumnElement['columnDefinition'])) {
708 6
            $joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition'];
709
        }
710
711 19
        return $joinColumn;
712
    }
713
714
    /**
715
     * Parses the given column as array.
716
     *
717
     * @param string $fieldName
718
     * @param array  $column
719
     *
720
     * @return  array
721
     */
722 41
    private function columnToArray($fieldName, $column)
723
    {
724
        $mapping = array(
725 41
            'fieldName' => $fieldName
726
        );
727
728 41
        if (isset($column['type'])) {
729 35
            $params = explode('(', $column['type']);
730
731 35
            $column['type']  = $params[0];
732 35
            $mapping['type'] = $column['type'];
733
734 35
            if (isset($params[1])) {
735 2
                $column['length'] = (integer) substr($params[1], 0, strlen($params[1]) - 1);
736
            }
737
        }
738
739 41
        if (isset($column['column'])) {
740 13
            $mapping['columnName'] = $column['column'];
741
        }
742
743 41
        if (isset($column['length'])) {
744 23
            $mapping['length'] = $column['length'];
745
        }
746
747 41
        if (isset($column['precision'])) {
748
            $mapping['precision'] = $column['precision'];
749
        }
750
751 41
        if (isset($column['scale'])) {
752
            $mapping['scale'] = $column['scale'];
753
        }
754
755 41
        if (isset($column['unique'])) {
756 15
            $mapping['unique'] = (bool) $column['unique'];
757
        }
758
759 41
        if (isset($column['options'])) {
760 6
            $mapping['options'] = $column['options'];
761
        }
762
763 41
        if (isset($column['nullable'])) {
764 12
            $mapping['nullable'] = $column['nullable'];
765
        }
766
767 41
        if (isset($column['version']) && $column['version']) {
768 4
            $mapping['version'] = $column['version'];
769
        }
770
771 41
        if (isset($column['columnDefinition'])) {
772 8
            $mapping['columnDefinition'] = $column['columnDefinition'];
773
        }
774
775 41
        return $mapping;
776
    }
777
778
    /**
779
     * Parse / Normalize the cache configuration
780
     *
781
     * @param array $cacheMapping
782
     *
783
     * @return array
784
     */
785 2
    private function cacheToArray($cacheMapping)
786
    {
787 2
        $region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null;
788 2
        $usage  = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null;
789
790 2
        if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $usage of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
791
            throw new \InvalidArgumentException(sprintf('Invalid cache usage "%s"', $usage));
792
        }
793
794 2
        if ($usage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $usage of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
795 2
            $usage = constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage);
796
        }
797
798
        return array(
799 2
            'usage'  => $usage,
800 2
            'region' => $region,
801
        );
802
    }
803
804
    /**
805
     * {@inheritDoc}
806
     */
807 62
    protected function loadMappingFile($file)
808
    {
809 62
        return Yaml::parse(file_get_contents($file));
810
    }
811
}
812