Complex classes like YamlDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use YamlDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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) |
|
| 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); |
|
|
|
|||
| 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) |
|
| 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) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Parse / Normalize the cache configuration |
||
| 780 | * |
||
| 781 | * @param array $cacheMapping |
||
| 782 | * |
||
| 783 | * @return array |
||
| 784 | */ |
||
| 785 | 2 | private function cacheToArray($cacheMapping) |
|
| 803 | |||
| 804 | /** |
||
| 805 | * {@inheritDoc} |
||
| 806 | */ |
||
| 807 | 62 | protected function loadMappingFile($file) |
|
| 811 | } |
||
| 812 |
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.