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 |
||
| 23 | |||
| 24 | public static function typeNotFound(string $name) : self |
||
| 28 | |||
| 29 | 6 | public static function typeRequirementsNotFulfilled(string $className, string $fieldName, string $type, string $reason) : self |
|
| 30 | { |
||
| 31 | 6 | return new self(sprintf("Can not use '%s' type for field '%s' in class '%s' as its requirements are not met: %s.", $fieldName, $className, $type, $reason)); |
|
| 32 | } |
||
| 33 | |||
| 34 | public static function mappingNotFound(string $className, string $fieldName) : self |
||
| 35 | { |
||
| 36 | return new self(sprintf("No mapping found for field '%s' in class '%s'.", $fieldName, $className)); |
||
| 37 | } |
||
| 38 | |||
| 39 | 2 | public static function referenceMappingNotFound(string $className, string $fieldName) : self |
|
| 40 | { |
||
| 41 | 2 | return new self(sprintf("No reference mapping found for field '%s' in class '%s'.", $fieldName, $className)); |
|
| 42 | } |
||
| 43 | |||
| 44 | 2 | public static function mappingNotFoundInClassNorDescendants(string $className, string $fieldName) : self |
|
| 45 | { |
||
| 46 | 2 | return new self(sprintf("No mapping found for field '%s' in class '%s' nor its descendants.", $fieldName, $className)); |
|
| 47 | } |
||
| 48 | |||
| 49 | 2 | public static function referenceFieldConflict(string $fieldName, string $className, string $className2) : self |
|
| 50 | { |
||
| 51 | 2 | return new self(sprintf("Reference mapping for field '%s' in class '%s' conflicts with one mapped in class '%s'.", $fieldName, $className, $className2)); |
|
| 52 | } |
||
| 53 | |||
| 54 | public static function mappingNotFoundByDbName(string $className, string $dbFieldName) : self |
||
| 55 | { |
||
| 56 | return new self(sprintf("No mapping found for field by DB name '%s' in class '%s'.", $dbFieldName, $className)); |
||
| 57 | } |
||
| 58 | |||
| 59 | 2 | public static function duplicateFieldMapping(string $document, string $fieldName) : self |
|
| 60 | { |
||
| 61 | 2 | return new self(sprintf('Property "%s" in "%s" was already declared, but it must be declared only once', $fieldName, $document)); |
|
| 62 | } |
||
| 63 | |||
| 64 | 2 | public static function duplicateDatabaseFieldName(string $document, string $offendingFieldName, string $databaseName, string $originalFieldName) : self |
|
| 65 | { |
||
| 66 | 2 | return new self(sprintf('Field "%s" in class "%s" is mapped to field "%s" in the database, but that name is already in use by field "%s".', $offendingFieldName, $document, $databaseName, $originalFieldName)); |
|
| 67 | } |
||
| 68 | |||
| 69 | public static function discriminatorFieldConflict(string $document, string $fieldName) : self |
||
| 70 | { |
||
| 71 | return new self(sprintf('Discriminator field "%s" in "%s" conflicts with a mapped field\'s "name" attribute.', $fieldName, $document)); |
||
| 72 | } |
||
| 73 | |||
| 74 | 16 | public static function invalidClassInDiscriminatorMap(string $className, string $owningClass) : self |
|
| 75 | { |
||
| 76 | 16 | return new self(sprintf("Document class '%s' used in the discriminator map of class '%s' does not exist.", $className, $owningClass)); |
|
| 77 | } |
||
| 78 | |||
| 79 | public static function unlistedClassInDiscriminatorMap(string $className) : self |
||
| 80 | { |
||
| 81 | return new self(sprintf('Document class "%s" is unlisted in the discriminator map.', $className)); |
||
| 82 | } |
||
| 83 | |||
| 84 | public static function invalidDiscriminatorValue(string $value, string $owningClass) : self |
||
| 85 | { |
||
| 86 | return new self(sprintf("Discriminator value '%s' used in the declaration of class '%s' does not exist.", $value, $owningClass)); |
||
| 87 | } |
||
| 88 | |||
| 89 | 4 | public static function missingFieldName(string $className) : self |
|
| 90 | { |
||
| 91 | 4 | return new self(sprintf("The Document class '%s' field mapping misses the 'fieldName' attribute.", $className)); |
|
| 92 | } |
||
| 93 | |||
| 94 | 6 | public static function classIsNotAValidDocument(string $className) : self |
|
| 95 | { |
||
| 96 | 6 | return new self(sprintf('Class %s is not a valid document or mapped super class.', $className)); |
|
| 97 | 6 | } |
|
| 98 | 6 | ||
| 99 | 6 | public static function classCanOnlyBeMappedByOneAbstractDocument(string $className, AbstractDocument $mappedAs, AbstractDocument $offending) : self |
|
| 100 | 6 | { |
|
| 101 | return new self(sprintf( |
||
| 102 | "Can not map class '%s' as %s because it was already mapped as %s.", |
||
| 103 | $className, |
||
| 104 | (new ReflectionObject($offending))->getShortName(), |
||
| 105 | (new ReflectionObject($mappedAs))->getShortName() |
||
| 106 | )); |
||
| 107 | } |
||
| 108 | |||
| 109 | public static function reflectionFailure(string $document, ReflectionException $previousException) : self |
||
| 110 | { |
||
| 111 | return new self('An error occurred in ' . $document, 0, $previousException); |
||
| 112 | } |
||
| 113 | |||
| 114 | public static function identifierRequired(string $documentName) : self |
||
| 115 | { |
||
| 116 | return new self(sprintf("No identifier/primary key specified for Document '%s'. Every Document must have an identifier/primary key.", $documentName)); |
||
| 117 | } |
||
| 118 | |||
| 119 | public static function missingIdentifierField(string $className, string $fieldName) : self |
||
| 120 | { |
||
| 121 | return new self(sprintf('The identifier %s is missing for a query of %s', $fieldName, $className)); |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function missingIdGeneratorClass(string $className) : self |
||
| 125 | { |
||
| 126 | return new self(sprintf('The class-option for the custom ID generator is missing in class %s.', $className)); |
||
| 127 | } |
||
| 128 | |||
| 129 | public static function classIsNotAValidGenerator(string $className) : self |
||
| 130 | { |
||
| 131 | return new self(sprintf('The class %s if not a valid ID generator of type AbstractIdGenerator.', $className)); |
||
| 132 | } |
||
| 133 | |||
| 134 | 1 | public static function missingGeneratorSetter(string $className, string $optionName) : self |
|
| 135 | { |
||
| 136 | 1 | return new self(sprintf('The class %s is missing a setter for the option %s.', $className, $optionName)); |
|
| 137 | } |
||
| 138 | |||
| 139 | 3 | public static function cascadeOnEmbeddedNotAllowed(string $className, string $fieldName) : self |
|
| 140 | { |
||
| 141 | 3 | return new self(sprintf('Cascade on %s::%s is not allowed.', $className, $fieldName)); |
|
| 142 | } |
||
| 143 | |||
| 144 | 1 | public static function simpleReferenceRequiresTargetDocument(string $className, string $fieldName) : self |
|
| 145 | { |
||
| 146 | 1 | return new self(sprintf('Target document must be specified for identifier reference: %s::%s', $className, $fieldName)); |
|
| 147 | } |
||
| 148 | |||
| 149 | 1 | public static function simpleReferenceMustNotTargetDiscriminatedDocument(string $targetDocument) : self |
|
| 150 | { |
||
| 151 | 1 | return new self(sprintf('Identifier reference must not target document using Single Collection Inheritance, %s targeted.', $targetDocument)); |
|
| 152 | } |
||
| 153 | |||
| 154 | 4 | public static function atomicCollectionStrategyNotAllowed(string $strategy, string $className, string $fieldName) : self |
|
| 155 | { |
||
| 156 | 4 | return new self(sprintf('%s collection strategy can be used only in top level document, used in %s::%s', $strategy, $className, $fieldName)); |
|
| 157 | } |
||
| 158 | |||
| 159 | 1 | public static function owningAndInverseReferencesRequireTargetDocument(string $className, string $fieldName) : self |
|
| 160 | { |
||
| 161 | 1 | return new self(sprintf('Target document must be specified for owning/inverse sides of reference: %s::%s', $className, $fieldName)); |
|
| 162 | } |
||
| 163 | |||
| 164 | 1 | public static function mustNotChangeIdentifierFieldsType(string $className, string $fieldName) : self |
|
| 165 | { |
||
| 166 | 1 | return new self(sprintf('%s::%s was declared an identifier and must stay this way.', $className, $fieldName)); |
|
| 167 | } |
||
| 168 | |||
| 169 | public static function referenceManySortMustNotBeUsedWithNonSetCollectionStrategy(string $className, string $fieldName, string $strategy) : self |
||
| 170 | { |
||
| 171 | return new self(sprintf("ReferenceMany's sort can not be used with addToSet and pushAll strategies, %s used in %s::%s", $strategy, $className, $fieldName)); |
||
| 172 | } |
||
| 173 | |||
| 174 | 1 | public static function invalidStorageStrategy(string $className, string $fieldName, string $type, string $strategy) : self |
|
| 175 | { |
||
| 176 | 1 | return new self(sprintf('Invalid strategy %s used in %s::%s with type %s', $strategy, $className, $fieldName, $type)); |
|
| 177 | } |
||
| 178 | |||
| 179 | 2 | public static function collectionClassDoesNotImplementCommonInterface(string $className, string $fieldName, string $collectionClass) : self |
|
| 180 | { |
||
| 181 | 2 | return new self(sprintf('%s used as custom collection class for %s::%s has to implement %s interface.', $collectionClass, $className, $fieldName, Collection::class)); |
|
| 182 | } |
||
| 183 | |||
| 184 | 2 | public static function shardKeyInSingleCollInheritanceSubclass(string $subclassName) : self |
|
| 185 | { |
||
| 186 | 2 | return new self(sprintf('Shard key overriding in subclass is forbidden for single collection inheritance: %s', $subclassName)); |
|
| 187 | } |
||
| 188 | |||
| 189 | 1 | public static function embeddedDocumentCantHaveShardKey(string $className) : self |
|
| 190 | { |
||
| 191 | 1 | return new self(sprintf("Embedded document can't have shard key: %s", $className)); |
|
| 192 | } |
||
| 193 | |||
| 194 | 3 | public static function onlySetStrategyAllowedInShardKey(string $className, string $fieldName) : self |
|
| 195 | { |
||
| 196 | 3 | return new self(sprintf('Only fields using the SET strategy can be used in the shard key: %s::%s', $className, $fieldName)); |
|
| 197 | } |
||
| 198 | |||
| 199 | public static function noMultiKeyShardKeys(string $className, string $fieldName) : self |
||
| 200 | { |
||
| 201 | return new self(sprintf('No multikey indexes are allowed in the shard key: %s::%s', $className, $fieldName)); |
||
| 202 | } |
||
| 203 | |||
| 204 | public static function cannotLookupDbRefReference(string $className, string $fieldName) : self |
||
| 205 | { |
||
| 206 | return new self(sprintf("Cannot use reference '%s' in class '%s' for lookup or graphLookup: dbRef references are not supported.", $fieldName, $className)); |
||
| 207 | } |
||
| 208 | |||
| 209 | 1 | public static function repositoryMethodLookupNotAllowed(string $className, string $fieldName) : self |
|
| 210 | { |
||
| 211 | 1 | 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)); |
|
| 212 | } |
||
| 213 | |||
| 214 | 3 | public static function cannotUseShardedCollectionInOutStage(string $className) : self |
|
| 215 | { |
||
| 216 | 3 | return new self(sprintf("Cannot use class '%s' as collection for out stage. Sharded collections are not allowed.", $className)); |
|
| 217 | } |
||
| 218 | |||
| 219 | public static function cannotUseShardedCollectionInLookupStages(string $className) : self |
||
| 220 | { |
||
| 221 | return new self(sprintf("Cannot use class '%s' as collection for lookup or graphLookup stage. Sharded collections are not allowed.", $className)); |
||
| 222 | } |
||
| 223 | |||
| 224 | 1 | public static function referencePrimersOnlySupportedForInverseReferenceMany(string $className, string $fieldName) : self |
|
| 228 | |||
| 229 | 3 | public static function connectFromFieldMustReferenceSameDocument(string $fieldName) : self |
|
| 233 | |||
| 234 | 2 | public static function repositoryMethodCanNotBeCombinedWithSkipLimitAndSort(string $className, string $fieldName) : self |
|
| 238 | |||
| 239 | 1 | public static function xmlMappingFileInvalid(string $filename, string $errorDetails) : self |
|
| 243 | |||
| 244 | public static function fieldNotAllowedForGridFS(string $className, string $fieldName) : self |
||
| 248 | |||
| 249 | 4 | public static function discriminatorNotAllowedForGridFS(string $className) : self |
|
| 250 | { |
||
| 251 | 4 | return new self(sprintf("Class '%s' cannot be discriminated because it is marked as a GridFS file", $className)); |
|
| 252 | 4 | } |
|
| 253 | 4 | ||
| 254 | 4 | public static function invalidRepositoryClass(string $className, string $repositoryClass, string $expectedRepositoryClass) : self |
|
| 255 | 4 | { |
|
| 256 | return new self(sprintf( |
||
| 257 | 'Invalid repository class "%s" for mapped class "%s". It must be an instance of "%s".', |
||
| 258 | $repositoryClass, |
||
| 259 | 2 | $className, |
|
| 260 | $expectedRepositoryClass |
||
| 261 | 2 | )); |
|
| 262 | } |
||
| 263 | |||
| 264 | 2 | public static function viewWithoutRootClass(string $className) : self |
|
| 265 | { |
||
| 266 | 2 | return new self(sprintf('Class "%s" mapped as view without must have a root class.', $className)); |
|
| 267 | } |
||
| 268 | |||
| 269 | public static function viewRootClassNotFound(string $className, string $rootClass) : self |
||
| 270 | { |
||
| 271 | return new self(sprintf('Root class "%s" for view "%s" could not be found.', $rootClass, $className)); |
||
| 272 | } |
||
| 273 | } |
||
| 274 |