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 | 903 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
61 | { |
||
62 | /** @var $class ClassMetadataInfo */ |
||
63 | 903 | $reflClass = $class->getReflectionClass(); |
|
64 | |||
65 | 903 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
66 | |||
67 | 903 | $documentAnnots = array(); |
|
68 | 903 | foreach ($classAnnotations as $annot) { |
|
69 | 901 | $classAnnotations[get_class($annot)] = $annot; |
|
70 | |||
71 | 901 | foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
72 | 901 | if ($annot instanceof $annotClass) { |
|
73 | 901 | $documentAnnots[$i] = $annot; |
|
74 | 901 | continue 2; |
|
75 | } |
||
76 | } |
||
77 | |||
78 | // non-document class annotations |
||
79 | 396 | if ($annot instanceof ODM\AbstractIndex) { |
|
80 | 13 | $this->addIndex($class, $annot); |
|
81 | } |
||
82 | 396 | 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 | 385 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
87 | 313 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
88 | 383 | } 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 | @trigger_error( |
||
94 | sprintf('Specifying discriminator\'s name through "name" is deprecated - use @DiscriminatorField("%s") instead', $annot->name), |
||
95 | E_USER_DEPRECATED |
||
96 | ); |
||
97 | $class->setDiscriminatorField($annot->name); |
||
98 | } elseif (isset($annot->fieldName)) { |
||
99 | @trigger_error( |
||
100 | sprintf('Specifying discriminator\'s name through "name" is deprecated - use @DiscriminatorField("%s") instead', $annot->fieldName), |
||
101 | E_USER_DEPRECATED |
||
102 | ); |
||
103 | 121 | $class->setDiscriminatorField($annot->fieldName); |
|
104 | } |
||
105 | 382 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
106 | 121 | $class->setDiscriminatorMap($annot->value); |
|
107 | 320 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
108 | $class->setDiscriminatorValue($annot->value); |
||
109 | 320 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
110 | 57 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
111 | 317 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
112 | 396 | $class->setDefaultDiscriminatorValue($annot->value); |
|
113 | } |
||
114 | |||
115 | } |
||
116 | |||
117 | 903 | if ( ! $documentAnnots) { |
|
118 | 3 | throw MappingException::classIsNotAValidDocument($className); |
|
119 | } |
||
120 | |||
121 | // find the winning document annotation |
||
122 | 901 | ksort($documentAnnots); |
|
123 | 901 | $documentAnnot = reset($documentAnnots); |
|
124 | |||
125 | 901 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
126 | 302 | $class->isMappedSuperclass = true; |
|
127 | 900 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
128 | 298 | $class->isEmbeddedDocument = true; |
|
129 | 893 | } elseif ($documentAnnot instanceof ODM\QueryResultDocument) { |
|
130 | 52 | $class->isQueryResultDocument = true; |
|
131 | } |
||
132 | 901 | if (isset($documentAnnot->db)) { |
|
133 | 1 | $class->setDatabase($documentAnnot->db); |
|
134 | } |
||
135 | 901 | if (isset($documentAnnot->collection)) { |
|
136 | 351 | $class->setCollection($documentAnnot->collection); |
|
137 | } |
||
138 | 901 | if (isset($documentAnnot->repositoryClass)) { |
|
139 | 69 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
140 | } |
||
141 | 901 | if (isset($documentAnnot->writeConcern)) { |
|
142 | 11 | $class->setWriteConcern($documentAnnot->writeConcern); |
|
143 | } |
||
144 | 901 | if (isset($documentAnnot->indexes)) { |
|
145 | 900 | foreach ($documentAnnot->indexes as $index) { |
|
146 | $this->addIndex($class, $index); |
||
147 | } |
||
148 | } |
||
149 | 901 | if (isset($documentAnnot->requireIndexes)) { |
|
150 | 893 | $class->setRequireIndexes($documentAnnot->requireIndexes); |
|
151 | } |
||
152 | 901 | if (isset($documentAnnot->slaveOkay)) { |
|
153 | 1 | $class->setSlaveOkay($documentAnnot->slaveOkay); |
|
154 | } |
||
155 | |||
156 | 901 | foreach ($reflClass->getProperties() as $property) { |
|
157 | 900 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
158 | || |
||
159 | 900 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
160 | 347 | continue; |
|
161 | } |
||
162 | |||
163 | 899 | $indexes = array(); |
|
164 | 899 | $mapping = array('fieldName' => $property->getName()); |
|
165 | 899 | $fieldAnnot = null; |
|
166 | |||
167 | 899 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
168 | 899 | if ($annot instanceof ODM\AbstractField) { |
|
169 | 899 | $fieldAnnot = $annot; |
|
170 | 899 | if ($annot->isDeprecated()) { |
|
171 | @trigger_error($annot->getDeprecationMessage(), E_USER_DEPRECATED); |
||
172 | } |
||
173 | } |
||
174 | 899 | if ($annot instanceof ODM\AbstractIndex) { |
|
175 | 198 | $indexes[] = $annot; |
|
176 | } |
||
177 | 899 | if ($annot instanceof ODM\Indexes) { |
|
178 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
||
179 | $indexes[] = $index; |
||
180 | } |
||
181 | 899 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
182 | 15 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
183 | 899 | } elseif ($annot instanceof ODM\Version) { |
|
184 | 97 | $mapping['version'] = true; |
|
185 | 899 | } elseif ($annot instanceof ODM\Lock) { |
|
186 | 899 | $mapping['lock'] = true; |
|
187 | } |
||
188 | } |
||
189 | |||
190 | 899 | if ($fieldAnnot) { |
|
191 | 899 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
192 | 899 | $class->mapField($mapping); |
|
193 | } |
||
194 | |||
195 | 899 | if ($indexes) { |
|
196 | 198 | foreach ($indexes as $index) { |
|
197 | 198 | $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
198 | 198 | $keys = array($name => $index->order ?: 'asc'); |
|
199 | 899 | $this->addIndex($class, $index, $keys); |
|
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | // Set shard key after all fields to ensure we mapped all its keys |
||
205 | 899 | if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
206 | 62 | $this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
207 | } |
||
208 | |||
209 | /** @var $method \ReflectionMethod */ |
||
210 | 898 | foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
211 | /* Filter for the declaring class only. Callbacks from parent |
||
212 | * classes will already be registered. |
||
213 | */ |
||
214 | 634 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
215 | 314 | continue; |
|
216 | } |
||
217 | |||
218 | 634 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
219 | 307 | if ($annot instanceof ODM\AlsoLoad) { |
|
220 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
221 | } |
||
222 | |||
223 | 307 | if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
224 | 72 | continue; |
|
225 | } |
||
226 | |||
227 | 288 | if ($annot instanceof ODM\PrePersist) { |
|
228 | 267 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
229 | 86 | } elseif ($annot instanceof ODM\PostPersist) { |
|
230 | 11 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
231 | 85 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
232 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
233 | 80 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
234 | 67 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
235 | 74 | } elseif ($annot instanceof ODM\PreRemove) { |
|
236 | 72 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
237 | 74 | } elseif ($annot instanceof ODM\PostRemove) { |
|
238 | 70 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
239 | 74 | } elseif ($annot instanceof ODM\PreLoad) { |
|
240 | 71 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
241 | 73 | } elseif ($annot instanceof ODM\PostLoad) { |
|
242 | 72 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
243 | 11 | } elseif ($annot instanceof ODM\PreFlush) { |
|
244 | 634 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
245 | } |
||
246 | } |
||
247 | } |
||
248 | 898 | } |
|
249 | |||
250 | 222 | private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
266 | |||
267 | /** |
||
268 | * @param ClassMetadataInfo $class |
||
269 | * @param ODM\ShardKey $shardKey |
||
270 | * |
||
271 | * @throws MappingException |
||
272 | */ |
||
273 | 62 | private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
|
285 | |||
286 | /** |
||
287 | * Factory method for the Annotation Driver |
||
288 | * |
||
289 | * @param array|string $paths |
||
290 | * @param Reader $reader |
||
291 | * @return AnnotationDriver |
||
292 | */ |
||
293 | 1138 | public static function create($paths = array(), Reader $reader = null) |
|
300 | } |
||
301 |
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.