Complex classes like MappingException 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 MappingException, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class MappingException extends BaseMappingException |
||
18 | { |
||
19 | public static function typeExists(string $name) : self |
||
20 | { |
||
21 | return new self(sprintf('Type %s already exists.', $name)); |
||
22 | } |
||
23 | |||
24 | 68 | public static function typeNotFound(string $name) : self |
|
25 | { |
||
26 | 68 | return new self(sprintf('Type to be overwritten %s does not exist.', $name)); |
|
27 | } |
||
28 | |||
29 | 68 | public static function mappingNotFound(string $className, string $fieldName) : self |
|
30 | 68 | { |
|
31 | return new self(sprintf("No mapping found for field '%s' in class '%s'.", $fieldName, $className)); |
||
32 | } |
||
33 | |||
34 | public static function referenceMappingNotFound(string $className, string $fieldName) : self |
||
38 | |||
39 | public static function mappingNotFoundInClassNorDescendants(string $className, string $fieldName) : self |
||
40 | { |
||
41 | return new self(sprintf("No mapping found for field '%s' in class '%s' nor its descendants.", $fieldName, $className)); |
||
42 | 6 | } |
|
43 | |||
44 | 6 | public static function referenceFieldConflict(string $fieldName, string $className, string $className2) : self |
|
45 | { |
||
46 | return new self(sprintf("Reference mapping for field '%s' in class '%s' conflicts with one mapped in class '%s'.", $fieldName, $className, $className2)); |
||
47 | } |
||
48 | |||
49 | public static function mappingNotFoundByDbName(string $className, string $dbFieldName) : self |
||
50 | { |
||
51 | return new self(sprintf("No mapping found for field by DB name '%s' in class '%s'.", $dbFieldName, $className)); |
||
52 | 2 | } |
|
53 | |||
54 | 2 | public static function duplicateFieldMapping(string $document, string $fieldName) : self |
|
55 | { |
||
56 | return new self(sprintf('Property "%s" in "%s" was already declared, but it must be declared only once', $fieldName, $document)); |
||
57 | 2 | } |
|
58 | |||
59 | 2 | public static function duplicateDatabaseFieldName(string $document, string $offendingFieldName, string $databaseName, string $originalFieldName) : self |
|
63 | |||
64 | 2 | public static function discriminatorFieldConflict(string $document, string $fieldName) : self |
|
65 | { |
||
66 | return new self(sprintf('Discriminator field "%s" in "%s" conflicts with a mapped field\'s "name" attribute.', $fieldName, $document)); |
||
67 | } |
||
68 | |||
69 | public static function invalidClassInDiscriminatorMap(string $className, string $owningClass) : self |
||
70 | { |
||
71 | return new self(sprintf("Document class '%s' used in the discriminator map of class '%s' does not exist.", $className, $owningClass)); |
||
72 | 2 | } |
|
73 | |||
74 | 2 | public static function unlistedClassInDiscriminatorMap(string $className) : self |
|
78 | |||
79 | 2 | public static function invalidDiscriminatorValue(string $value, string $owningClass) : self |
|
80 | { |
||
81 | return new self(sprintf("Discriminator value '%s' used in the declaration of class '%s' does not exist.", $value, $owningClass)); |
||
82 | } |
||
83 | |||
84 | public static function missingFieldName(string $className) : self |
||
85 | { |
||
86 | return new self(sprintf("The Document class '%s' field mapping misses the 'fieldName' attribute.", $className)); |
||
87 | 14 | } |
|
88 | |||
89 | 14 | public static function classIsNotAValidDocument(string $className) : self |
|
90 | { |
||
91 | return new self(sprintf('Class %s is not a valid document or mapped super class.', $className)); |
||
92 | } |
||
93 | |||
94 | public static function classCanOnlyBeMappedByOneAbstractDocument(string $className, AbstractDocument $mappedAs, AbstractDocument $offending) : self |
||
95 | { |
||
96 | return new self(sprintf( |
||
97 | "Can not map class '%s' as %s because it was already mapped as %s.", |
||
98 | $className, |
||
99 | (new ReflectionObject($offending))->getShortName(), |
||
100 | (new ReflectionObject($mappedAs))->getShortName() |
||
101 | )); |
||
102 | 5 | } |
|
103 | |||
104 | 5 | public static function reflectionFailure(string $document, ReflectionException $previousException) : self |
|
105 | { |
||
106 | return new self('An error occurred in ' . $document, 0, $previousException); |
||
107 | 5 | } |
|
108 | |||
109 | 5 | public static function identifierRequired(string $documentName) : self |
|
110 | 5 | { |
|
111 | 5 | return new self(sprintf("No identifier/primary key specified for Document '%s'. Every Document must have an identifier/primary key.", $documentName)); |
|
112 | 5 | } |
|
113 | 5 | ||
114 | public static function missingIdentifierField(string $className, string $fieldName) : self |
||
115 | { |
||
116 | return new self(sprintf('The identifier %s is missing for a query of %s', $fieldName, $className)); |
||
117 | } |
||
118 | |||
119 | public static function missingIdGeneratorClass(string $className) : self |
||
123 | |||
124 | public static function classIsNotAValidGenerator(string $className) : self |
||
128 | |||
129 | public static function missingGeneratorSetter(string $className, string $optionName) : self |
||
133 | |||
134 | public static function cascadeOnEmbeddedNotAllowed(string $className, string $fieldName) : self |
||
135 | { |
||
136 | return new self(sprintf('Cascade on %s::%s is not allowed.', $className, $fieldName)); |
||
137 | } |
||
138 | |||
139 | public static function simpleReferenceRequiresTargetDocument(string $className, string $fieldName) : self |
||
140 | { |
||
141 | return new self(sprintf('Target document must be specified for identifier reference: %s::%s', $className, $fieldName)); |
||
142 | } |
||
143 | |||
144 | public static function simpleReferenceMustNotTargetDiscriminatedDocument(string $targetDocument) : self |
||
145 | { |
||
146 | return new self(sprintf('Identifier reference must not target document using Single Collection Inheritance, %s targeted.', $targetDocument)); |
||
147 | 1 | } |
|
148 | |||
149 | 1 | public static function atomicCollectionStrategyNotAllowed(string $strategy, string $className, string $fieldName) : self |
|
153 | |||
154 | 3 | public static function owningAndInverseReferencesRequireTargetDocument(string $className, string $fieldName) : self |
|
158 | |||
159 | 1 | public static function mustNotChangeIdentifierFieldsType(string $className, string $fieldName) : self |
|
163 | |||
164 | 1 | public static function referenceManySortMustNotBeUsedWithNonSetCollectionStrategy(string $className, string $fieldName, string $strategy) : self |
|
168 | |||
169 | 4 | public static function invalidStorageStrategy(string $className, string $fieldName, string $type, string $strategy) : self |
|
170 | { |
||
171 | return new self(sprintf('Invalid strategy %s used in %s::%s with type %s', $strategy, $className, $fieldName, $type)); |
||
172 | 1 | } |
|
173 | |||
174 | 1 | public static function collectionClassDoesNotImplementCommonInterface(string $className, string $fieldName, string $collectionClass) : self |
|
178 | |||
179 | 1 | public static function shardKeyInSingleCollInheritanceSubclass(string $subclassName) : self |
|
180 | { |
||
181 | return new self(sprintf('Shard key overriding in subclass is forbidden for single collection inheritance: %s', $subclassName)); |
||
182 | } |
||
183 | |||
184 | public static function embeddedDocumentCantHaveShardKey(string $className) : self |
||
185 | { |
||
186 | return new self(sprintf("Embedded document can't have shard key: %s", $className)); |
||
187 | 1 | } |
|
188 | |||
189 | 1 | public static function onlySetStrategyAllowedInShardKey(string $className, string $fieldName) : self |
|
193 | |||
194 | 2 | public static function noMultiKeyShardKeys(string $className, string $fieldName) : self |
|
198 | |||
199 | 2 | public static function cannotLookupDbRefReference(string $className, string $fieldName) : self |
|
200 | { |
||
201 | return new self(sprintf("Cannot use reference '%s' in class '%s' for lookup or graphLookup: dbRef references are not supported.", $fieldName, $className)); |
||
202 | 1 | } |
|
203 | |||
204 | 1 | public static function repositoryMethodLookupNotAllowed(string $className, string $fieldName) : self |
|
205 | { |
||
206 | return new self(sprintf("Cannot use reference '%s' in class '%s' for lookup or graphLookup. repositoryMethod is not supported in \$lookup and \$graphLookup stages.", $fieldName, $className)); |
||
207 | 3 | } |
|
208 | |||
209 | 3 | public static function cannotUseShardedCollectionInOutStage(string $className) : self |
|
210 | { |
||
211 | return new self(sprintf("Cannot use class '%s' as collection for out stage. Sharded collections are not allowed.", $className)); |
||
212 | } |
||
213 | |||
214 | public static function cannotUseShardedCollectionInLookupStages(string $className) : self |
||
215 | { |
||
216 | return new self(sprintf("Cannot use class '%s' as collection for lookup or graphLookup stage. Sharded collections are not allowed.", $className)); |
||
217 | } |
||
218 | |||
219 | public static function referencePrimersOnlySupportedForInverseReferenceMany(string $className, string $fieldName) : self |
||
220 | { |
||
221 | return new self(sprintf("Cannot use reference priming on '%s' in class '%s'. Reference priming is only supported for inverse references", $fieldName, $className)); |
||
222 | 1 | } |
|
223 | |||
224 | 1 | public static function connectFromFieldMustReferenceSameDocument(string $fieldName) : self |
|
228 | |||
229 | 3 | public static function repositoryMethodCanNotBeCombinedWithSkipLimitAndSort(string $className, string $fieldName) : self |
|
230 | { |
||
231 | return new self(sprintf("'repositoryMethod' used on '%s' in class '%s' can not be combined with skip, limit or sort.", $fieldName, $className)); |
||
232 | } |
||
233 | |||
234 | public static function xmlMappingFileInvalid(string $filename, string $errorDetails) : self |
||
235 | { |
||
236 | return new self(sprintf("The mapping file %s is invalid: \n%s", $filename, $errorDetails)); |
||
237 | 1 | } |
|
238 | |||
239 | 1 | public static function fieldNotAllowedForGridFS(string $className, string $fieldName) : self |
|
243 | |||
244 | 3 | public static function discriminatorNotAllowedForGridFS(string $className) : self |
|
245 | { |
||
246 | return new self(sprintf("Class '%s' cannot be discriminated because it is marked as a GridFS file", $className)); |
||
247 | 2 | } |
|
248 | } |
||
249 |