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 |
||
| 40 | class AnnotationDriver extends AbstractAnnotationDriver |
||
| 41 | { |
||
| 42 | protected $entityAnnotationClasses = array( |
||
| 43 | ODM\Document::class => 1, |
||
| 44 | ODM\MappedSuperclass::class => 2, |
||
| 45 | ODM\EmbeddedDocument::class => 3, |
||
| 46 | ODM\AggregationResultDocument::class => 4, |
||
| 47 | ); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Registers annotation classes to the common registry. |
||
| 51 | * |
||
| 52 | * This method should be called when bootstrapping your application. |
||
| 53 | */ |
||
| 54 | public static function registerAnnotationClasses() |
||
| 55 | { |
||
| 56 | AnnotationRegistry::registerFile(__DIR__ . '/../Annotations/DoctrineAnnotations.php'); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | 767 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 63 | { |
||
| 64 | /** @var $class ClassMetadataInfo */ |
||
| 65 | 767 | $reflClass = $class->getReflectionClass(); |
|
| 66 | |||
| 67 | 767 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
| 68 | |||
| 69 | 767 | $documentAnnots = array(); |
|
| 70 | 767 | foreach ($classAnnotations as $annot) { |
|
| 71 | 765 | $classAnnotations[get_class($annot)] = $annot; |
|
| 72 | |||
| 73 | 765 | foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
| 74 | 765 | if ($annot instanceof $annotClass) { |
|
| 75 | 765 | $documentAnnots[$i] = $annot; |
|
| 76 | 765 | continue 2; |
|
| 77 | } |
||
| 78 | 503 | } |
|
| 79 | |||
| 80 | // non-document class annotations |
||
| 81 | 321 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 82 | 13 | $this->addIndex($class, $annot); |
|
| 83 | 13 | } |
|
| 84 | 321 | if ($annot instanceof ODM\Indexes) { |
|
| 85 | 37 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
|
| 86 | 37 | $this->addIndex($class, $index); |
|
| 87 | 37 | } |
|
| 88 | 321 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
| 89 | 242 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
| 90 | 303 | } elseif ($annot instanceof ODM\DiscriminatorField) { |
|
| 91 | // $fieldName property is deprecated, but fall back for BC |
||
| 92 | 75 | if (isset($annot->value)) { |
|
| 93 | 19 | $class->setDiscriminatorField($annot->value); |
|
|
|
|||
| 94 | 75 | } elseif (isset($annot->name)) { |
|
| 95 | $class->setDiscriminatorField($annot->name); |
||
| 96 | 72 | } elseif (isset($annot->fieldName)) { |
|
| 97 | 72 | $class->setDiscriminatorField($annot->fieldName); |
|
| 98 | 72 | } |
|
| 99 | 301 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
| 100 | 75 | $class->setDiscriminatorMap($annot->value); |
|
| 101 | 300 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
| 102 | $class->setDiscriminatorValue($annot->value); |
||
| 103 | 249 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
| 104 | 21 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
| 105 | 249 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
| 106 | 22 | $class->setDefaultDiscriminatorValue($annot->value); |
|
| 107 | 22 | } |
|
| 108 | |||
| 109 | 767 | } |
|
| 110 | |||
| 111 | 767 | if ( ! $documentAnnots) { |
|
| 112 | 2 | throw MappingException::classIsNotAValidDocument($className); |
|
| 113 | } |
||
| 114 | |||
| 115 | // find the winning document annotation |
||
| 116 | 765 | ksort($documentAnnots); |
|
| 117 | 765 | $documentAnnot = reset($documentAnnots); |
|
| 118 | |||
| 119 | 765 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
| 120 | 241 | $class->isMappedSuperclass = true; |
|
| 121 | 765 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
| 122 | 399 | $class->isEmbeddedDocument = true; |
|
| 123 | 764 | } elseif ($documentAnnot instanceof ODM\AggregationResultDocument) { |
|
| 124 | 17 | $class->isAggregationResultDocument = true; |
|
| 125 | 17 | } |
|
| 126 | 765 | if (isset($documentAnnot->db)) { |
|
| 127 | 1 | $class->setDatabase($documentAnnot->db); |
|
| 128 | 1 | } |
|
| 129 | 765 | if (isset($documentAnnot->collection)) { |
|
| 130 | 278 | $class->setCollection($documentAnnot->collection); |
|
| 131 | 278 | } |
|
| 132 | 765 | if (isset($documentAnnot->repositoryClass) && !$class->isEmbeddedDocument && !$class->isAggregationResultDocument) { |
|
| 133 | 32 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
| 134 | 32 | } |
|
| 135 | 765 | if (isset($documentAnnot->indexes)) { |
|
| 136 | 764 | foreach ($documentAnnot->indexes as $index) { |
|
| 137 | $this->addIndex($class, $index); |
||
| 138 | 764 | } |
|
| 139 | 764 | } |
|
| 140 | 765 | if (isset($documentAnnot->requireIndexes)) { |
|
| 141 | 758 | $class->setRequireIndexes($documentAnnot->requireIndexes); |
|
| 142 | 758 | } |
|
| 143 | 765 | if (isset($documentAnnot->slaveOkay)) { |
|
| 144 | 1 | $class->setSlaveOkay($documentAnnot->slaveOkay); |
|
| 145 | 1 | } |
|
| 146 | |||
| 147 | 765 | foreach ($reflClass->getProperties() as $property) { |
|
| 148 | 764 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
| 149 | || |
||
| 150 | 764 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
| 151 | 273 | continue; |
|
| 152 | } |
||
| 153 | |||
| 154 | 763 | $indexes = array(); |
|
| 155 | 763 | $mapping = array('fieldName' => $property->getName()); |
|
| 156 | 763 | $fieldAnnot = null; |
|
| 157 | |||
| 158 | 763 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
| 159 | 763 | if ($annot instanceof ODM\AbstractField) { |
|
| 160 | 763 | $fieldAnnot = $annot; |
|
| 161 | 763 | } |
|
| 162 | 763 | if ($annot instanceof ODM\AbstractIndex) { |
|
| 163 | 150 | $indexes[] = $annot; |
|
| 164 | 150 | } |
|
| 165 | 763 | if ($annot instanceof ODM\Indexes) { |
|
| 166 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
||
| 167 | $indexes[] = $index; |
||
| 168 | } |
||
| 169 | 763 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
| 170 | 13 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
| 171 | 763 | } elseif ($annot instanceof ODM\Version) { |
|
| 172 | 61 | $mapping['version'] = true; |
|
| 173 | 763 | } elseif ($annot instanceof ODM\Lock) { |
|
| 174 | 24 | $mapping['lock'] = true; |
|
| 175 | 24 | } |
|
| 176 | 763 | } |
|
| 177 | |||
| 178 | 763 | if ($fieldAnnot) { |
|
| 179 | 763 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
| 180 | 763 | $class->mapField($mapping); |
|
| 181 | 763 | } |
|
| 182 | |||
| 183 | 763 | if ($indexes) { |
|
| 184 | 150 | foreach ($indexes as $index) { |
|
| 185 | 150 | $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
| 186 | 150 | $keys = array($name => $index->order ?: 'asc'); |
|
| 187 | 150 | $this->addIndex($class, $index, $keys); |
|
| 188 | 150 | } |
|
| 189 | 150 | } |
|
| 190 | 765 | } |
|
| 191 | |||
| 192 | /** @var $method \ReflectionMethod */ |
||
| 193 | 763 | foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
| 194 | /* Filter for the declaring class only. Callbacks from parent |
||
| 195 | * classes will already be registered. |
||
| 196 | */ |
||
| 197 | 529 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
| 198 | 258 | continue; |
|
| 199 | } |
||
| 200 | |||
| 201 | 529 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
| 202 | 247 | if ($annot instanceof ODM\AlsoLoad) { |
|
| 203 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
| 204 | 13 | } |
|
| 205 | |||
| 206 | 247 | if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
| 207 | 37 | continue; |
|
| 208 | } |
||
| 209 | |||
| 210 | 228 | if ($annot instanceof ODM\PrePersist) { |
|
| 211 | 207 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
| 212 | 228 | } elseif ($annot instanceof ODM\PostPersist) { |
|
| 213 | 11 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
| 214 | 51 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
| 215 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
| 216 | 50 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
| 217 | 32 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
| 218 | 45 | } elseif ($annot instanceof ODM\PreRemove) { |
|
| 219 | 37 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
| 220 | 39 | } elseif ($annot instanceof ODM\PostRemove) { |
|
| 221 | 35 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
| 222 | 39 | } elseif ($annot instanceof ODM\PreLoad) { |
|
| 223 | 36 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
| 224 | 39 | } elseif ($annot instanceof ODM\PostLoad) { |
|
| 225 | 37 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
| 226 | 38 | } elseif ($annot instanceof ODM\PreFlush) { |
|
| 227 | 11 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
| 228 | 11 | } |
|
| 229 | 529 | } |
|
| 230 | 763 | } |
|
| 231 | 763 | } |
|
| 232 | |||
| 233 | 181 | private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
| 234 | { |
||
| 235 | 181 | $keys = array_merge($keys, $index->keys); |
|
| 236 | 181 | $options = array(); |
|
| 237 | 181 | $allowed = array('name', 'dropDups', 'background', 'safe', 'unique', 'sparse', 'expireAfterSeconds'); |
|
| 238 | 181 | foreach ($allowed as $name) { |
|
| 239 | 181 | if (isset($index->$name)) { |
|
| 240 | 181 | $options[$name] = $index->$name; |
|
| 241 | 181 | } |
|
| 242 | 181 | } |
|
| 243 | 181 | $options = array_merge($options, $index->options); |
|
| 244 | 181 | $class->addIndex($keys, $options); |
|
| 245 | 181 | } |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Factory method for the Annotation Driver |
||
| 249 | * |
||
| 250 | * @param array|string $paths |
||
| 251 | * @param Reader $reader |
||
| 252 | * @return AnnotationDriver |
||
| 253 | */ |
||
| 254 | 947 | public static function create($paths = array(), Reader $reader = null) |
|
| 261 | } |
||
| 262 |
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.