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 | ); |
||
45 | |||
46 | /** |
||
47 | * Registers annotation classes to the common registry. |
||
48 | * |
||
49 | * This method should be called when bootstrapping your application. |
||
50 | */ |
||
51 | public static function registerAnnotationClasses() |
||
52 | { |
||
53 | AnnotationRegistry::registerFile(__DIR__ . '/../Annotations/DoctrineAnnotations.php'); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 852 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
60 | { |
||
61 | /** @var $class ClassMetadataInfo */ |
||
62 | 852 | $reflClass = $class->getReflectionClass(); |
|
63 | |||
64 | 852 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
65 | |||
66 | 852 | $documentAnnots = array(); |
|
67 | 852 | foreach ($classAnnotations as $annot) { |
|
68 | 850 | $classAnnotations[get_class($annot)] = $annot; |
|
69 | |||
70 | 850 | foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
71 | 850 | if ($annot instanceof $annotClass) { |
|
72 | 850 | $documentAnnots[$i] = $annot; |
|
73 | 850 | continue 2; |
|
74 | } |
||
75 | 558 | } |
|
76 | |||
77 | // non-document class annotations |
||
78 | 379 | if ($annot instanceof ODM\AbstractIndex) { |
|
79 | 13 | $this->addIndex($class, $annot); |
|
80 | 13 | } |
|
81 | 379 | if ($annot instanceof ODM\Indexes) { |
|
82 | 63 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
|
83 | 63 | $this->addIndex($class, $index); |
|
84 | 63 | } |
|
85 | 379 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
86 | 289 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
87 | 370 | } elseif ($annot instanceof ODM\DiscriminatorField) { |
|
88 | // $fieldName property is deprecated, but fall back for BC |
||
89 | 112 | if (isset($annot->value)) { |
|
90 | 54 | $class->setDiscriminatorField($annot->value); |
|
|
|||
91 | 112 | } elseif (isset($annot->name)) { |
|
92 | $class->setDiscriminatorField($annot->name); |
||
93 | 109 | } elseif (isset($annot->fieldName)) { |
|
94 | 109 | $class->setDiscriminatorField($annot->fieldName); |
|
95 | 109 | } |
|
96 | 368 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
97 | 112 | $class->setDiscriminatorMap($annot->value); |
|
98 | 367 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
99 | $class->setDiscriminatorValue($annot->value); |
||
100 | 314 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
101 | 57 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
102 | 314 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
103 | 57 | $class->setDefaultDiscriminatorValue($annot->value); |
|
104 | 57 | } |
|
105 | |||
106 | 852 | } |
|
107 | |||
108 | 852 | if ( ! $documentAnnots) { |
|
109 | 3 | throw MappingException::classIsNotAValidDocument($className); |
|
110 | } |
||
111 | |||
112 | // find the winning document annotation |
||
113 | 850 | ksort($documentAnnots); |
|
114 | 850 | $documentAnnot = reset($documentAnnots); |
|
115 | |||
116 | 850 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
117 | 287 | $class->isMappedSuperclass = true; |
|
118 | 850 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
119 | 286 | $class->isEmbeddedDocument = true; |
|
120 | 286 | } |
|
121 | 850 | if (isset($documentAnnot->db)) { |
|
122 | 1 | $class->setDatabase($documentAnnot->db); |
|
123 | 1 | } |
|
124 | 850 | if (isset($documentAnnot->collection)) { |
|
125 | 339 | $class->setCollection($documentAnnot->collection); |
|
126 | 339 | } |
|
127 | 850 | if (isset($documentAnnot->repositoryClass) && !$class->isEmbeddedDocument) { |
|
128 | 65 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
129 | 65 | } |
|
130 | 850 | if (isset($documentAnnot->indexes)) { |
|
131 | 849 | foreach ($documentAnnot->indexes as $index) { |
|
132 | $this->addIndex($class, $index); |
||
133 | 849 | } |
|
134 | 849 | } |
|
135 | 850 | if (isset($documentAnnot->requireIndexes)) { |
|
136 | 842 | $class->setRequireIndexes($documentAnnot->requireIndexes); |
|
137 | 842 | } |
|
138 | 850 | if (isset($documentAnnot->slaveOkay)) { |
|
139 | 1 | $class->setSlaveOkay($documentAnnot->slaveOkay); |
|
140 | 1 | } |
|
141 | |||
142 | 850 | foreach ($reflClass->getProperties() as $property) { |
|
143 | 849 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
144 | || |
||
145 | 849 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
146 | 324 | continue; |
|
147 | } |
||
148 | |||
149 | 848 | $indexes = array(); |
|
150 | 848 | $mapping = array('fieldName' => $property->getName()); |
|
151 | 848 | $fieldAnnot = null; |
|
152 | |||
153 | 848 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
154 | 848 | if ($annot instanceof ODM\AbstractField) { |
|
155 | 848 | $fieldAnnot = $annot; |
|
156 | 848 | } |
|
157 | 848 | if ($annot instanceof ODM\AbstractIndex) { |
|
158 | 185 | $indexes[] = $annot; |
|
159 | 185 | } |
|
160 | 848 | if ($annot instanceof ODM\Indexes) { |
|
161 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
||
162 | $indexes[] = $index; |
||
163 | } |
||
164 | 848 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
165 | 13 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
166 | 848 | } elseif ($annot instanceof ODM\Version) { |
|
167 | 97 | $mapping['version'] = true; |
|
168 | 848 | } elseif ($annot instanceof ODM\Lock) { |
|
169 | 24 | $mapping['lock'] = true; |
|
170 | 24 | } |
|
171 | 848 | } |
|
172 | |||
173 | 848 | if ($fieldAnnot) { |
|
174 | 848 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
175 | 848 | $class->mapField($mapping); |
|
176 | 848 | } |
|
177 | |||
178 | 848 | if ($indexes) { |
|
179 | 185 | foreach ($indexes as $index) { |
|
180 | 185 | $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
181 | 185 | $keys = array($name => $index->order ?: 'asc'); |
|
182 | 185 | $this->addIndex($class, $index, $keys); |
|
183 | 186 | } |
|
184 | 185 | } |
|
185 | 850 | } |
|
186 | |||
187 | // Set shard key after all fields to ensure we mapped all its keys |
||
188 | 848 | if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
189 | 71 | $this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
190 | 70 | } |
|
191 | |||
192 | /** @var $method \ReflectionMethod */ |
||
193 | 847 | 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 | 594 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
198 | 298 | continue; |
|
199 | } |
||
200 | |||
201 | 594 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
202 | 292 | if ($annot instanceof ODM\AlsoLoad) { |
|
203 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
204 | 13 | } |
|
205 | |||
206 | 292 | if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
207 | 72 | continue; |
|
208 | } |
||
209 | |||
210 | 273 | if ($annot instanceof ODM\PrePersist) { |
|
211 | 252 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
212 | 273 | } elseif ($annot instanceof ODM\PostPersist) { |
|
213 | 11 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
214 | 86 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
215 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
216 | 85 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
217 | 67 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
218 | 80 | } elseif ($annot instanceof ODM\PreRemove) { |
|
219 | 72 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
220 | 74 | } elseif ($annot instanceof ODM\PostRemove) { |
|
221 | 70 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
222 | 74 | } elseif ($annot instanceof ODM\PreLoad) { |
|
223 | 71 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
224 | 74 | } elseif ($annot instanceof ODM\PostLoad) { |
|
225 | 72 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
226 | 73 | } elseif ($annot instanceof ODM\PreFlush) { |
|
227 | 11 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
228 | 11 | } |
|
229 | 594 | } |
|
230 | 847 | } |
|
231 | 847 | } |
|
232 | |||
233 | 207 | private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
249 | |||
250 | /** |
||
251 | * @param ClassMetadataInfo $class |
||
252 | * @param ODM\ShardKey $shardKey |
||
253 | * |
||
254 | * @throws MappingException |
||
255 | */ |
||
256 | 71 | private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
|
268 | |||
269 | /** |
||
270 | * Factory method for the Annotation Driver |
||
271 | * |
||
272 | * @param array|string $paths |
||
273 | * @param Reader $reader |
||
274 | * @return AnnotationDriver |
||
275 | */ |
||
276 | 1069 | public static function create($paths = array(), Reader $reader = null) |
|
283 | } |
||
284 |
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.