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 | public static function registerAnnotationClasses() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | 901 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 61 | { |
||
| 62 | /** @var $class ClassMetadataInfo */ |
||
| 63 | 901 | $reflClass = $class->getReflectionClass(); |
|
| 64 | |||
| 65 | 901 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
| 66 | |||
| 67 | 901 | $documentAnnots = array(); |
|
| 68 | 901 | foreach ($classAnnotations as $annot) { |
|
| 69 | 899 | $classAnnotations[get_class($annot)] = $annot; |
|
| 70 | |||
| 71 | 899 | foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
| 72 | 899 | if ($annot instanceof $annotClass) { |
|
| 73 | 899 | $documentAnnots[$i] = $annot; |
|
| 74 | 899 | continue 2; |
|
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | // non-document class annotations |
||
| 79 | 394 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 80 | 13 | $this->addIndex($class, $annot); |
|
| 81 | } |
||
| 82 | 394 | if ($annot instanceof ODM\Indexes) { |
|
| 83 | 65 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
|
| 84 | 65 | $this->addIndex($class, $index); |
|
| 85 | } |
||
| 86 | 383 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
| 87 | 311 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
| 88 | 381 | } elseif ($annot instanceof ODM\DiscriminatorField) { |
|
| 89 | // $fieldName property is deprecated, but fall back for BC |
||
| 90 | 121 | if (isset($annot->value)) { |
|
| 91 | 121 | $class->setDiscriminatorField($annot->value); |
|
|
|
|||
| 92 | } elseif (isset($annot->name)) { |
||
| 93 | $class->setDiscriminatorField($annot->name); |
||
| 94 | } elseif (isset($annot->fieldName)) { |
||
| 95 | $class->setDiscriminatorField($annot->fieldName); |
||
| 96 | } |
||
| 97 | 380 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
| 98 | 121 | $class->setDiscriminatorMap($annot->value); |
|
| 99 | 318 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
| 100 | $class->setDiscriminatorValue($annot->value); |
||
| 101 | 318 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
| 102 | 57 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
| 103 | 315 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
| 104 | 57 | $class->setDefaultDiscriminatorValue($annot->value); |
|
| 105 | } |
||
| 106 | |||
| 107 | } |
||
| 108 | |||
| 109 | 901 | if ( ! $documentAnnots) { |
|
| 110 | 3 | throw MappingException::classIsNotAValidDocument($className); |
|
| 111 | } |
||
| 112 | |||
| 113 | // find the winning document annotation |
||
| 114 | 899 | ksort($documentAnnots); |
|
| 115 | 899 | $documentAnnot = reset($documentAnnots); |
|
| 116 | |||
| 117 | 899 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
| 118 | 300 | $class->isMappedSuperclass = true; |
|
| 119 | 898 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
| 120 | 298 | $class->isEmbeddedDocument = true; |
|
| 121 | 891 | } elseif ($documentAnnot instanceof ODM\QueryResultDocument) { |
|
| 122 | 52 | $class->isQueryResultDocument = true; |
|
| 123 | } |
||
| 124 | 899 | if (isset($documentAnnot->db)) { |
|
| 125 | 1 | $class->setDatabase($documentAnnot->db); |
|
| 126 | } |
||
| 127 | 899 | if (isset($documentAnnot->collection)) { |
|
| 128 | 349 | $class->setCollection($documentAnnot->collection); |
|
| 129 | } |
||
| 130 | 899 | if (isset($documentAnnot->repositoryClass)) { |
|
| 131 | 69 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
| 132 | } |
||
| 133 | 899 | if (isset($documentAnnot->writeConcern)) { |
|
| 134 | 11 | $class->setWriteConcern($documentAnnot->writeConcern); |
|
| 135 | } |
||
| 136 | 899 | if (isset($documentAnnot->indexes)) { |
|
| 137 | 898 | foreach ($documentAnnot->indexes as $index) { |
|
| 138 | $this->addIndex($class, $index); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | 899 | if (isset($documentAnnot->requireIndexes)) { |
|
| 142 | 891 | $class->setRequireIndexes($documentAnnot->requireIndexes); |
|
| 143 | } |
||
| 144 | 899 | if (isset($documentAnnot->slaveOkay)) { |
|
| 145 | 1 | $class->setSlaveOkay($documentAnnot->slaveOkay); |
|
| 146 | } |
||
| 147 | |||
| 148 | 899 | foreach ($reflClass->getProperties() as $property) { |
|
| 149 | 898 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
| 150 | || |
||
| 151 | 897 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
| 152 | 345 | continue; |
|
| 153 | } |
||
| 154 | |||
| 155 | 897 | $indexes = array(); |
|
| 156 | 897 | $mapping = array('fieldName' => $property->getName()); |
|
| 157 | 897 | $fieldAnnot = null; |
|
| 158 | |||
| 159 | 897 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
| 160 | 897 | if ($annot instanceof ODM\AbstractField) { |
|
| 161 | 897 | $fieldAnnot = $annot; |
|
| 162 | } |
||
| 163 | 897 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 164 | 198 | $indexes[] = $annot; |
|
| 165 | } |
||
| 166 | 897 | if ($annot instanceof ODM\Indexes) { |
|
| 167 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
||
| 168 | $indexes[] = $index; |
||
| 169 | } |
||
| 170 | 897 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
| 171 | 15 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
| 172 | 897 | } elseif ($annot instanceof ODM\Version) { |
|
| 173 | 97 | $mapping['version'] = true; |
|
| 174 | 897 | } elseif ($annot instanceof ODM\Lock) { |
|
| 175 | 24 | $mapping['lock'] = true; |
|
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | 897 | if ($fieldAnnot) { |
|
| 180 | 897 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
| 181 | 897 | $class->mapField($mapping); |
|
| 182 | } |
||
| 183 | |||
| 184 | 897 | if ($indexes) { |
|
| 185 | 198 | foreach ($indexes as $index) { |
|
| 186 | 198 | $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
| 187 | 198 | $keys = array($name => $index->order ?: 'asc'); |
|
| 188 | 198 | $this->addIndex($class, $index, $keys); |
|
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | // Set shard key after all fields to ensure we mapped all its keys |
||
| 194 | 897 | if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
| 195 | 62 | $this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
| 196 | } |
||
| 197 | |||
| 198 | /** @var $method \ReflectionMethod */ |
||
| 199 | 896 | foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
| 200 | /* Filter for the declaring class only. Callbacks from parent |
||
| 201 | * classes will already be registered. |
||
| 202 | */ |
||
| 203 | 632 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
| 204 | 312 | continue; |
|
| 205 | } |
||
| 206 | |||
| 207 | 632 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
| 208 | 305 | if ($annot instanceof ODM\AlsoLoad) { |
|
| 209 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
| 210 | } |
||
| 211 | |||
| 212 | 305 | if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
| 213 | 72 | continue; |
|
| 214 | } |
||
| 215 | |||
| 216 | 286 | if ($annot instanceof ODM\PrePersist) { |
|
| 217 | 265 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
| 218 | 86 | } elseif ($annot instanceof ODM\PostPersist) { |
|
| 219 | 11 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
| 220 | 85 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
| 221 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
| 222 | 80 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
| 223 | 67 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
| 224 | 74 | } elseif ($annot instanceof ODM\PreRemove) { |
|
| 225 | 72 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
| 226 | 74 | } elseif ($annot instanceof ODM\PostRemove) { |
|
| 227 | 70 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
| 228 | 74 | } elseif ($annot instanceof ODM\PreLoad) { |
|
| 229 | 71 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
| 230 | 73 | } elseif ($annot instanceof ODM\PostLoad) { |
|
| 231 | 72 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
| 232 | 11 | } elseif ($annot instanceof ODM\PreFlush) { |
|
| 233 | 632 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | 896 | } |
|
| 238 | |||
| 239 | 222 | private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param ClassMetadataInfo $class |
||
| 258 | * @param ODM\ShardKey $shardKey |
||
| 259 | * |
||
| 260 | * @throws MappingException |
||
| 261 | */ |
||
| 262 | 62 | private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
|
| 263 | { |
||
| 264 | 62 | $options = array(); |
|
| 265 | 62 | $allowed = array('unique', 'numInitialChunks'); |
|
| 266 | 62 | foreach ($allowed as $name) { |
|
| 267 | 62 | if (isset($shardKey->$name)) { |
|
| 268 | 1 | $options[$name] = $shardKey->$name; |
|
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | 62 | $class->setShardKey($shardKey->keys, $options); |
|
| 273 | 61 | } |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Factory method for the Annotation Driver |
||
| 277 | * |
||
| 278 | * @param array|string $paths |
||
| 279 | * @param Reader $reader |
||
| 280 | * @return AnnotationDriver |
||
| 281 | */ |
||
| 282 | 1136 | public static function create($paths = array(), Reader $reader = null) |
|
| 289 | } |
||
| 290 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.