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 |
||
29 | class AnnotationDriver extends AbstractAnnotationDriver |
||
30 | { |
||
31 | 1019 | public function isTransient($className) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 1555 | public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Mapping\ClassMetadata $class) : void |
|
47 | { |
||
48 | 1555 | assert($class instanceof ClassMetadata); |
|
49 | 1555 | $reflClass = $class->getReflectionClass(); |
|
50 | |||
51 | 1555 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
52 | |||
53 | 1555 | $documentAnnot = null; |
|
54 | 1555 | foreach ($classAnnotations as $annot) { |
|
55 | 1553 | $classAnnotations[get_class($annot)] = $annot; |
|
56 | |||
57 | 1553 | if ($annot instanceof ODM\AbstractDocument) { |
|
58 | 1553 | if ($documentAnnot !== null) { |
|
59 | 5 | throw MappingException::classCanOnlyBeMappedByOneAbstractDocument($className, $documentAnnot, $annot); |
|
60 | } |
||
61 | 1553 | $documentAnnot = $annot; |
|
62 | } |
||
63 | |||
64 | // non-document class annotations |
||
65 | 1553 | if ($annot instanceof ODM\AbstractIndex) { |
|
66 | 8 | $this->addIndex($class, $annot); |
|
67 | } |
||
68 | 1553 | if ($annot instanceof ODM\Indexes) { |
|
69 | // Setting the type to mixed is a workaround until https://github.com/doctrine/annotations/pull/209 is released. |
||
70 | /** @var mixed $value */ |
||
71 | 143 | $value = $annot->value; |
|
72 | 143 | foreach (is_array($value) ? $value : [$value] as $index) { |
|
73 | 143 | $this->addIndex($class, $index); |
|
74 | } |
||
75 | 1553 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
76 | 962 | $class->setInheritanceType(constant(ClassMetadata::class . '::INHERITANCE_TYPE_' . $annot->value)); |
|
77 | 1553 | } elseif ($annot instanceof ODM\DiscriminatorField) { |
|
78 | 191 | $class->setDiscriminatorField($annot->value); |
|
79 | 1553 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
80 | /** @var array $value */ |
||
81 | 183 | $value = $annot->value; |
|
82 | 183 | $class->setDiscriminatorMap($value); |
|
83 | 1553 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
84 | 1 | $class->setDiscriminatorValue($annot->value); |
|
85 | 1553 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
86 | 114 | $class->setChangeTrackingPolicy(constant(ClassMetadata::class . '::CHANGETRACKING_' . $annot->value)); |
|
87 | 1553 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
88 | 116 | $class->setDefaultDiscriminatorValue($annot->value); |
|
89 | 1553 | } elseif ($annot instanceof ODM\ReadPreference) { |
|
90 | 6 | $class->setReadPreference($annot->value, $annot->tags ?? []); |
|
91 | } |
||
92 | } |
||
93 | |||
94 | 1550 | if ($documentAnnot === null) { |
|
95 | 4 | throw MappingException::classIsNotAValidDocument($className); |
|
96 | } |
||
97 | |||
98 | 1548 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
99 | 945 | $class->isMappedSuperclass = true; |
|
100 | 1545 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
101 | 337 | $class->isEmbeddedDocument = true; |
|
102 | 1537 | } elseif ($documentAnnot instanceof ODM\QueryResultDocument) { |
|
103 | 115 | $class->isQueryResultDocument = true; |
|
104 | 1537 | } elseif ($documentAnnot instanceof ODM\File) { |
|
105 | 128 | $class->isFile = true; |
|
106 | |||
107 | 128 | if ($documentAnnot->chunkSizeBytes !== null) { |
|
108 | 126 | $class->setChunkSizeBytes($documentAnnot->chunkSizeBytes); |
|
109 | } |
||
110 | } |
||
111 | |||
112 | 1548 | if (isset($documentAnnot->db)) { |
|
113 | 1 | $class->setDatabase($documentAnnot->db); |
|
114 | } |
||
115 | 1548 | if (isset($documentAnnot->collection)) { |
|
116 | 1004 | $class->setCollection($documentAnnot->collection); |
|
117 | } |
||
118 | // Store bucketName as collection name for GridFS files |
||
119 | 1548 | if (isset($documentAnnot->bucketName)) { |
|
120 | 1 | $class->setBucketName($documentAnnot->bucketName); |
|
121 | } |
||
122 | 1548 | if (isset($documentAnnot->repositoryClass)) { |
|
123 | 125 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
124 | } |
||
125 | 1548 | if (isset($documentAnnot->writeConcern)) { |
|
126 | 10 | $class->setWriteConcern($documentAnnot->writeConcern); |
|
127 | } |
||
128 | 1548 | if (isset($documentAnnot->indexes)) { |
|
129 | 1545 | foreach ($documentAnnot->indexes as $index) { |
|
130 | $this->addIndex($class, $index); |
||
131 | } |
||
132 | } |
||
133 | 1548 | if (! empty($documentAnnot->readOnly)) { |
|
134 | 5 | $class->markReadOnly(); |
|
135 | } |
||
136 | |||
137 | 1548 | foreach ($reflClass->getProperties() as $property) { |
|
138 | 1547 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
139 | || |
||
140 | 1547 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
141 | 990 | continue; |
|
142 | } |
||
143 | |||
144 | 1544 | $indexes = []; |
|
145 | 1544 | $mapping = ['fieldName' => $property->getName()]; |
|
146 | 1544 | $fieldAnnot = null; |
|
147 | |||
148 | 1544 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
149 | 1544 | if ($annot instanceof ODM\AbstractField) { |
|
150 | 1544 | $fieldAnnot = $annot; |
|
151 | 1544 | if ($annot->isDeprecated()) { |
|
152 | @trigger_error($annot->getDeprecationMessage(), E_USER_DEPRECATED); |
||
|
|||
153 | } |
||
154 | } |
||
155 | 1544 | if ($annot instanceof ODM\AbstractIndex) { |
|
156 | 230 | $indexes[] = $annot; |
|
157 | } |
||
158 | 1544 | if ($annot instanceof ODM\Indexes) { |
|
159 | // Setting the type to mixed is a workaround until https://github.com/doctrine/annotations/pull/209 is released. |
||
160 | /** @var mixed $value */ |
||
161 | $value = $annot->value; |
||
162 | foreach (is_array($value) ? $value : [$value] as $index) { |
||
163 | $indexes[] = $index; |
||
164 | } |
||
165 | 1544 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
166 | 15 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
167 | 1544 | } elseif ($annot instanceof ODM\Version) { |
|
168 | 156 | $mapping['version'] = true; |
|
169 | 1544 | } elseif ($annot instanceof ODM\Lock) { |
|
170 | 22 | $mapping['lock'] = true; |
|
171 | } |
||
172 | } |
||
173 | |||
174 | 1544 | if ($fieldAnnot) { |
|
175 | 1544 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
176 | 1544 | $class->mapField($mapping); |
|
177 | } |
||
178 | |||
179 | 1544 | if (! $indexes) { |
|
180 | 1544 | continue; |
|
181 | } |
||
182 | |||
183 | 230 | foreach ($indexes as $index) { |
|
184 | 230 | $name = $mapping['name'] ?? $mapping['fieldName']; |
|
185 | 230 | $keys = [$name => $index->order ?: 'asc']; |
|
186 | 230 | $this->addIndex($class, $index, $keys); |
|
187 | } |
||
188 | } |
||
189 | |||
190 | // Set shard key after all fields to ensure we mapped all its keys |
||
191 | 1545 | if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
192 | 137 | $this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
193 | } |
||
194 | |||
195 | /** @var ReflectionMethod $method */ |
||
196 | 1544 | foreach ($reflClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
197 | /* Filter for the declaring class only. Callbacks from parent |
||
198 | * classes will already be registered. |
||
199 | */ |
||
200 | 1283 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
201 | 951 | continue; |
|
202 | } |
||
203 | |||
204 | 1283 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
205 | 942 | if ($annot instanceof ODM\AlsoLoad) { |
|
206 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
207 | } |
||
208 | |||
209 | 942 | if (! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
210 | 132 | continue; |
|
211 | } |
||
212 | |||
213 | 923 | if ($annot instanceof ODM\PrePersist) { |
|
214 | 904 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
215 | 143 | } elseif ($annot instanceof ODM\PostPersist) { |
|
216 | 10 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
217 | 143 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
218 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
219 | 138 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
220 | 127 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
221 | 132 | } elseif ($annot instanceof ODM\PreRemove) { |
|
222 | 130 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
223 | 132 | } elseif ($annot instanceof ODM\PostRemove) { |
|
224 | 130 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
225 | 132 | } elseif ($annot instanceof ODM\PreLoad) { |
|
226 | 131 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
227 | 131 | } elseif ($annot instanceof ODM\PostLoad) { |
|
228 | 130 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
229 | 11 | } elseif ($annot instanceof ODM\PreFlush) { |
|
230 | 11 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
231 | } |
||
232 | } |
||
233 | } |
||
234 | 1544 | } |
|
235 | |||
236 | 265 | private function addIndex(ClassMetadata $class, AbstractIndex $index, array $keys = []) : void |
|
254 | |||
255 | /** |
||
256 | * @throws MappingException |
||
257 | */ |
||
258 | 137 | private function setShardKey(ClassMetadata $class, ODM\ShardKey $shardKey) : void |
|
272 | |||
273 | /** |
||
274 | * Factory method for the Annotation Driver |
||
275 | * |
||
276 | * @param string[]|string $paths |
||
277 | */ |
||
278 | 1771 | public static function create($paths = [], ?Reader $reader = null) : AnnotationDriver |
|
285 | } |
||
286 |
If you suppress an error, we recommend checking for the error condition explicitly: