Complex classes like AnnotationDriver 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 AnnotationDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class AnnotationDriver extends AbstractAnnotationDriver |
||
| 39 | { |
||
| 40 | protected $entityAnnotationClasses = array( |
||
| 41 | ODM\Document::class => 1, |
||
| 42 | ODM\MappedSuperclass::class => 2, |
||
| 43 | ODM\EmbeddedDocument::class => 3, |
||
| 44 | ODM\QueryResultDocument::class => 4, |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Registers annotation classes to the common registry. |
||
| 49 | * |
||
| 50 | * This method should be called when bootstrapping your application. |
||
| 51 | * |
||
| 52 | * @deprecated method was deprecated in 1.2 and will be removed in 2.0. Register class loader through AnnotationRegistry::registerLoader instead. |
||
| 53 | */ |
||
| 54 | public static function registerAnnotationClasses() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 903 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 67 | { |
||
| 68 | /** @var $class ClassMetadataInfo */ |
||
| 69 | 903 | $reflClass = $class->getReflectionClass(); |
|
| 70 | |||
| 71 | 903 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
| 72 | |||
| 73 | 903 | $documentAnnots = array(); |
|
| 74 | 903 | foreach ($classAnnotations as $annot) { |
|
| 75 | 901 | $classAnnotations[get_class($annot)] = $annot; |
|
| 76 | |||
| 77 | 901 | foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
| 78 | 901 | if ($annot instanceof $annotClass) { |
|
| 79 | 901 | $documentAnnots[$i] = $annot; |
|
| 80 | 901 | continue 2; |
|
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | // non-document class annotations |
||
| 85 | 396 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 86 | 13 | $this->addIndex($class, $annot); |
|
| 87 | } |
||
| 88 | 396 | if ($annot instanceof ODM\Indexes) { |
|
| 89 | 65 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
|
| 90 | 65 | $this->addIndex($class, $index); |
|
| 91 | } |
||
| 92 | 385 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
| 93 | 313 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
| 94 | 383 | } elseif ($annot instanceof ODM\DiscriminatorField) { |
|
| 95 | // $fieldName property is deprecated, but fall back for BC |
||
| 96 | 121 | if (isset($annot->value)) { |
|
| 97 | 121 | $class->setDiscriminatorField($annot->value); |
|
| 98 | } elseif (isset($annot->name)) { |
||
| 99 | $class->setDiscriminatorField($annot->name); |
||
| 100 | } elseif (isset($annot->fieldName)) { |
||
| 101 | 121 | $class->setDiscriminatorField($annot->fieldName); |
|
| 102 | } |
||
| 103 | 382 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
| 104 | 121 | $class->setDiscriminatorMap($annot->value); |
|
| 105 | 320 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
| 106 | $class->setDiscriminatorValue($annot->value); |
||
| 107 | 320 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
| 108 | 57 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
| 109 | 317 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
| 110 | 396 | $class->setDefaultDiscriminatorValue($annot->value); |
|
| 111 | } |
||
| 112 | |||
| 113 | } |
||
| 114 | |||
| 115 | 903 | if ( ! $documentAnnots) { |
|
| 116 | 3 | throw MappingException::classIsNotAValidDocument($className); |
|
| 117 | } |
||
| 118 | |||
| 119 | // find the winning document annotation |
||
| 120 | 901 | ksort($documentAnnots); |
|
| 121 | 901 | $documentAnnot = reset($documentAnnots); |
|
| 122 | |||
| 123 | 901 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
| 124 | 302 | $class->isMappedSuperclass = true; |
|
| 125 | 900 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
| 126 | 298 | $class->isEmbeddedDocument = true; |
|
| 127 | 893 | } elseif ($documentAnnot instanceof ODM\QueryResultDocument) { |
|
| 128 | 52 | $class->isQueryResultDocument = true; |
|
| 129 | } |
||
| 130 | 901 | if (isset($documentAnnot->db)) { |
|
| 131 | 1 | $class->setDatabase($documentAnnot->db); |
|
| 132 | } |
||
| 133 | 901 | if (isset($documentAnnot->collection)) { |
|
| 134 | 351 | $class->setCollection($documentAnnot->collection); |
|
| 135 | } |
||
| 136 | 901 | if (isset($documentAnnot->repositoryClass)) { |
|
| 137 | 69 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
| 138 | } |
||
| 139 | 901 | if (isset($documentAnnot->writeConcern)) { |
|
| 140 | 11 | $class->setWriteConcern($documentAnnot->writeConcern); |
|
| 141 | } |
||
| 142 | 901 | if (isset($documentAnnot->indexes)) { |
|
| 143 | 900 | foreach ($documentAnnot->indexes as $index) { |
|
| 144 | $this->addIndex($class, $index); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | 901 | if (isset($documentAnnot->requireIndexes)) { |
|
| 148 | 893 | $class->setRequireIndexes($documentAnnot->requireIndexes); |
|
| 149 | } |
||
| 150 | 901 | if (isset($documentAnnot->slaveOkay)) { |
|
| 151 | 1 | $class->setSlaveOkay($documentAnnot->slaveOkay); |
|
| 152 | } |
||
| 153 | |||
| 154 | 901 | foreach ($reflClass->getProperties() as $property) { |
|
| 155 | 900 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
| 156 | || |
||
| 157 | 900 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
| 158 | 347 | continue; |
|
| 159 | } |
||
| 160 | |||
| 161 | 899 | $indexes = array(); |
|
| 162 | 899 | $mapping = array('fieldName' => $property->getName()); |
|
| 163 | 899 | $fieldAnnot = null; |
|
| 164 | |||
| 165 | 899 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
| 166 | 899 | if ($annot instanceof ODM\AbstractField) { |
|
| 167 | 899 | $fieldAnnot = $annot; |
|
| 168 | } |
||
| 169 | 899 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 170 | 198 | $indexes[] = $annot; |
|
| 171 | } |
||
| 172 | 899 | if ($annot instanceof ODM\Indexes) { |
|
| 173 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
||
| 174 | $indexes[] = $index; |
||
| 175 | } |
||
| 176 | 899 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
| 177 | 15 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
| 178 | 899 | } elseif ($annot instanceof ODM\Version) { |
|
| 179 | 97 | $mapping['version'] = true; |
|
| 180 | 899 | } elseif ($annot instanceof ODM\Lock) { |
|
| 181 | 899 | $mapping['lock'] = true; |
|
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | 899 | if ($fieldAnnot) { |
|
| 186 | 899 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
| 187 | 899 | $class->mapField($mapping); |
|
| 188 | } |
||
| 189 | |||
| 190 | 899 | if ($indexes) { |
|
| 191 | 198 | foreach ($indexes as $index) { |
|
| 192 | 198 | $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
| 193 | 198 | $keys = array($name => $index->order ?: 'asc'); |
|
| 194 | 899 | $this->addIndex($class, $index, $keys); |
|
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | // Set shard key after all fields to ensure we mapped all its keys |
||
| 200 | 899 | if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
| 201 | 62 | $this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
| 202 | } |
||
| 203 | |||
| 204 | /** @var $method \ReflectionMethod */ |
||
| 205 | 898 | foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
| 206 | /* Filter for the declaring class only. Callbacks from parent |
||
| 207 | * classes will already be registered. |
||
| 208 | */ |
||
| 209 | 634 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
| 210 | 314 | continue; |
|
| 211 | } |
||
| 212 | |||
| 213 | 634 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
| 214 | 307 | if ($annot instanceof ODM\AlsoLoad) { |
|
| 215 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
| 216 | } |
||
| 217 | |||
| 218 | 307 | if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
| 219 | 72 | continue; |
|
| 220 | } |
||
| 221 | |||
| 222 | 288 | if ($annot instanceof ODM\PrePersist) { |
|
| 223 | 267 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
| 224 | 86 | } elseif ($annot instanceof ODM\PostPersist) { |
|
| 225 | 11 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
| 226 | 85 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
| 227 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
| 228 | 80 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
| 229 | 67 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
| 230 | 74 | } elseif ($annot instanceof ODM\PreRemove) { |
|
| 231 | 72 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
| 232 | 74 | } elseif ($annot instanceof ODM\PostRemove) { |
|
| 233 | 70 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
| 234 | 74 | } elseif ($annot instanceof ODM\PreLoad) { |
|
| 235 | 71 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
| 236 | 73 | } elseif ($annot instanceof ODM\PostLoad) { |
|
| 237 | 72 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
| 238 | 11 | } elseif ($annot instanceof ODM\PreFlush) { |
|
| 239 | 634 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | 898 | } |
|
| 244 | |||
| 245 | 222 | private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param ClassMetadataInfo $class |
||
| 264 | * @param ODM\ShardKey $shardKey |
||
| 265 | * |
||
| 266 | * @throws MappingException |
||
| 267 | */ |
||
| 268 | 62 | private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Factory method for the Annotation Driver |
||
| 283 | * |
||
| 284 | * @param array|string $paths |
||
| 285 | * @param Reader $reader |
||
| 286 | * @return AnnotationDriver |
||
| 287 | */ |
||
| 288 | 1138 | public static function create($paths = array(), Reader $reader = null) |
|
| 295 | } |
||
| 296 |
If you suppress an error, we recommend checking for the error condition explicitly: