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 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | 1383 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 51 | { |
||
| 52 | /** @var $class ClassMetadataInfo */ |
||
| 53 | 1383 | $reflClass = $class->getReflectionClass(); |
|
| 54 | |||
| 55 | 1383 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
| 56 | |||
| 57 | 1383 | $documentAnnots = array(); |
|
| 58 | 1383 | foreach ($classAnnotations as $annot) { |
|
| 59 | 1381 | $classAnnotations[get_class($annot)] = $annot; |
|
| 60 | |||
| 61 | 1381 | foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
| 62 | 1381 | if ($annot instanceof $annotClass) { |
|
| 63 | 1381 | $documentAnnots[$i] = $annot; |
|
| 64 | 1381 | continue 2; |
|
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | // non-document class annotations |
||
| 69 | 919 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 70 | 6 | $this->addIndex($class, $annot); |
|
| 71 | } |
||
| 72 | 919 | if ($annot instanceof ODM\Indexes) { |
|
| 73 | 62 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
|
| 74 | 62 | $this->addIndex($class, $index); |
|
| 75 | } |
||
| 76 | 903 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
| 77 | 840 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
| 78 | 901 | } elseif ($annot instanceof ODM\DiscriminatorField) { |
|
| 79 | 111 | $class->setDiscriminatorField($annot->value); |
|
|
|
|||
| 80 | 900 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
| 81 | 111 | $class->setDiscriminatorMap($annot->value); |
|
| 82 | 839 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
| 83 | $class->setDiscriminatorValue($annot->value); |
||
| 84 | 839 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
| 85 | 48 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
| 86 | 837 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
| 87 | 48 | $class->setDefaultDiscriminatorValue($annot->value); |
|
| 88 | 832 | } elseif ($annot instanceof ODM\ReadPreference) { |
|
| 89 | 919 | $class->setReadPreference($annot->value, $annot->tags); |
|
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | 1383 | if ( ! $documentAnnots) { |
|
| 95 | 3 | throw MappingException::classIsNotAValidDocument($className); |
|
| 96 | } |
||
| 97 | |||
| 98 | // find the winning document annotation |
||
| 99 | 1381 | ksort($documentAnnots); |
|
| 100 | 1381 | $documentAnnot = reset($documentAnnots); |
|
| 101 | |||
| 102 | 1381 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
| 103 | 829 | $class->isMappedSuperclass = true; |
|
| 104 | 1380 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
| 105 | 234 | $class->isEmbeddedDocument = true; |
|
| 106 | 1373 | } elseif ($documentAnnot instanceof ODM\QueryResultDocument) { |
|
| 107 | 47 | $class->isQueryResultDocument = true; |
|
| 108 | } |
||
| 109 | 1381 | if (isset($documentAnnot->db)) { |
|
| 110 | 1 | $class->setDatabase($documentAnnot->db); |
|
| 111 | } |
||
| 112 | 1381 | if (isset($documentAnnot->collection)) { |
|
| 113 | 876 | $class->setCollection($documentAnnot->collection); |
|
| 114 | } |
||
| 115 | 1381 | if (isset($documentAnnot->repositoryClass)) { |
|
| 116 | 59 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
| 117 | } |
||
| 118 | 1381 | if (isset($documentAnnot->writeConcern)) { |
|
| 119 | 10 | $class->setWriteConcern($documentAnnot->writeConcern); |
|
| 120 | } |
||
| 121 | 1381 | if (isset($documentAnnot->indexes)) { |
|
| 122 | 1380 | foreach ($documentAnnot->indexes as $index) { |
|
| 123 | $this->addIndex($class, $index); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | 1381 | if (isset($documentAnnot->slaveOkay)) { |
|
| 127 | $class->setSlaveOkay($documentAnnot->slaveOkay); |
||
| 128 | } |
||
| 129 | 1381 | if (! empty($documentAnnot->readOnly)) { |
|
| 130 | 5 | $class->markReadOnly(); |
|
| 131 | } |
||
| 132 | |||
| 133 | 1381 | foreach ($reflClass->getProperties() as $property) { |
|
| 134 | 1380 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
| 135 | || |
||
| 136 | 1380 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
| 137 | 872 | continue; |
|
| 138 | } |
||
| 139 | |||
| 140 | 1379 | $indexes = array(); |
|
| 141 | 1379 | $mapping = array('fieldName' => $property->getName()); |
|
| 142 | 1379 | $fieldAnnot = null; |
|
| 143 | |||
| 144 | 1379 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
| 145 | 1379 | if ($annot instanceof ODM\AbstractField) { |
|
| 146 | 1379 | $fieldAnnot = $annot; |
|
| 147 | 1379 | if ($annot->isDeprecated()) { |
|
| 148 | @trigger_error($annot->getDeprecationMessage(), E_USER_DEPRECATED); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | 1379 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 152 | 159 | $indexes[] = $annot; |
|
| 153 | } |
||
| 154 | 1379 | if ($annot instanceof ODM\Indexes) { |
|
| 155 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
||
| 156 | $indexes[] = $index; |
||
| 157 | } |
||
| 158 | 1379 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
| 159 | 15 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
| 160 | 1379 | } elseif ($annot instanceof ODM\Version) { |
|
| 161 | 67 | $mapping['version'] = true; |
|
| 162 | 1379 | } elseif ($annot instanceof ODM\Lock) { |
|
| 163 | 1379 | $mapping['lock'] = true; |
|
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | 1379 | if ($fieldAnnot) { |
|
| 168 | 1379 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
| 169 | 1379 | $class->mapField($mapping); |
|
| 170 | } |
||
| 171 | |||
| 172 | 1379 | if ($indexes) { |
|
| 173 | 159 | foreach ($indexes as $index) { |
|
| 174 | 159 | $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
| 175 | 159 | $keys = array($name => $index->order ?: 'asc'); |
|
| 176 | 1379 | $this->addIndex($class, $index, $keys); |
|
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | // Set shard key after all fields to ensure we mapped all its keys |
||
| 182 | 1379 | if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
| 183 | 59 | $this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
| 184 | } |
||
| 185 | |||
| 186 | /** @var $method \ReflectionMethod */ |
||
| 187 | 1378 | foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
| 188 | /* Filter for the declaring class only. Callbacks from parent |
||
| 189 | * classes will already be registered. |
||
| 190 | */ |
||
| 191 | 1144 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
| 192 | 839 | continue; |
|
| 193 | } |
||
| 194 | |||
| 195 | 1144 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
| 196 | 829 | if ($annot instanceof ODM\AlsoLoad) { |
|
| 197 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
| 198 | } |
||
| 199 | |||
| 200 | 829 | if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
| 201 | 64 | continue; |
|
| 202 | } |
||
| 203 | |||
| 204 | 810 | if ($annot instanceof ODM\PrePersist) { |
|
| 205 | 793 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
| 206 | 73 | } elseif ($annot instanceof ODM\PostPersist) { |
|
| 207 | 10 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
| 208 | 73 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
| 209 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
| 210 | 68 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
| 211 | 55 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
| 212 | 66 | } elseif ($annot instanceof ODM\PreRemove) { |
|
| 213 | 64 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
| 214 | 66 | } elseif ($annot instanceof ODM\PostRemove) { |
|
| 215 | 62 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
| 216 | 66 | } elseif ($annot instanceof ODM\PreLoad) { |
|
| 217 | 63 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
| 218 | 65 | } elseif ($annot instanceof ODM\PostLoad) { |
|
| 219 | 64 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
| 220 | 11 | } elseif ($annot instanceof ODM\PreFlush) { |
|
| 221 | 1144 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | 1378 | } |
|
| 226 | |||
| 227 | 181 | private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param ClassMetadataInfo $class |
||
| 246 | * @param ODM\ShardKey $shardKey |
||
| 247 | * |
||
| 248 | * @throws MappingException |
||
| 249 | */ |
||
| 250 | 59 | private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Factory method for the Annotation Driver |
||
| 265 | * |
||
| 266 | * @param array|string $paths |
||
| 267 | * @param Reader $reader |
||
| 268 | * @return AnnotationDriver |
||
| 269 | */ |
||
| 270 | 1676 | public static function create($paths = array(), Reader $reader = null) |
|
| 277 | } |
||
| 278 |
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.