Completed
Push — master ( cc367c...a47e46 )
by Andreas
19s
created

MappingException   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 60.87%

Importance

Changes 0
Metric Value
wmc 44
lcom 0
cbo 0
dl 0
loc 227
ccs 56
cts 92
cp 0.6087
rs 8.8798
c 0
b 0
f 0

44 Methods

Rating   Name   Duplication   Size   Complexity  
A typeExists() 0 4 1
A typeNotFound() 0 4 1
A mappingNotFound() 0 4 1
A referenceMappingNotFound() 0 4 1
A mappingNotFoundInClassNorDescendants() 0 4 1
A referenceFieldConflict() 0 4 1
A mappingNotFoundByDbName() 0 4 1
A duplicateFieldMapping() 0 4 1
A duplicateDatabaseFieldName() 0 4 1
A discriminatorFieldConflict() 0 4 1
A invalidClassInDiscriminatorMap() 0 4 1
A invalidDiscriminatorValue() 0 4 1
A missingFieldName() 0 4 1
A classIsNotAValidDocument() 0 4 1
A classCanOnlyBeMappedByOneAbstractDocument() 0 9 1
A reflectionFailure() 0 4 1
A identifierRequired() 0 4 1
A missingIdentifierField() 0 4 1
A missingIdGeneratorClass() 0 4 1
A classIsNotAValidGenerator() 0 4 1
A missingGeneratorSetter() 0 4 1
A cascadeOnEmbeddedNotAllowed() 0 4 1
A simpleReferenceRequiresTargetDocument() 0 4 1
A simpleReferenceMustNotTargetDiscriminatedDocument() 0 4 1
A atomicCollectionStrategyNotAllowed() 0 4 1
A owningAndInverseReferencesRequireTargetDocument() 0 4 1
A mustNotChangeIdentifierFieldsType() 0 4 1
A referenceManySortMustNotBeUsedWithNonSetCollectionStrategy() 0 4 1
A invalidStorageStrategy() 0 4 1
A collectionClassDoesNotImplementCommonInterface() 0 4 1
A shardKeyInSingleCollInheritanceSubclass() 0 4 1
A embeddedDocumentCantHaveShardKey() 0 4 1
A onlySetStrategyAllowedInShardKey() 0 4 1
A noMultiKeyShardKeys() 0 4 1
A cannotLookupDbRefReference() 0 4 1
A repositoryMethodLookupNotAllowed() 0 4 1
A cannotUseShardedCollectionInOutStage() 0 4 1
A cannotUseShardedCollectionInLookupStages() 0 4 1
A referencePrimersOnlySupportedForInverseReferenceMany() 0 4 1
A connectFromFieldMustReferenceSameDocument() 0 4 1
A repositoryMethodCanNotBeCombinedWithSkipLimitAndSort() 0 4 1
A xmlMappingFileInvalid() 0 4 1
A fieldNotAllowedForGridFS() 0 4 1
A discriminatorNotAllowedForGridFS() 0 4 1

How to fix   Complexity   

Complex Class

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
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Mapping;
6
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\Common\Persistence\Mapping\MappingException as BaseMappingException;
9
use Doctrine\ODM\MongoDB\Mapping\Annotations\AbstractDocument;
10
use ReflectionException;
11
use ReflectionObject;
12
use function sprintf;
13
14
/**
15
 * Class for all exceptions related to the Doctrine MongoDB ODM
16
 */
17
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
    public static function typeNotFound(string $name) : self
25
    {
26
        return new self(sprintf('Type to be overwritten %s does not exist.', $name));
27
    }
28
29 6
    public static function mappingNotFound(string $className, string $fieldName) : self
30
    {
31 6
        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
35
    {
36
        return new self(sprintf("No reference mapping found for field '%s' in class '%s'.", $fieldName, $className));
37
    }
38
39 2
    public static function mappingNotFoundInClassNorDescendants(string $className, string $fieldName) : self
40
    {
41 2
        return new self(sprintf("No mapping found for field '%s' in class '%s' nor its descendants.", $fieldName, $className));
42
    }
43
44 2
    public static function referenceFieldConflict(string $fieldName, string $className, string $className2) : self
45
    {
46 2
        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 1
    public static function mappingNotFoundByDbName(string $className, string $dbFieldName) : self
50
    {
51 1
        return new self(sprintf("No mapping found for field by DB name '%s' in class '%s'.", $dbFieldName, $className));
52
    }
53
54
    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
    }
58
59 2
    public static function duplicateDatabaseFieldName(string $document, string $offendingFieldName, string $databaseName, string $originalFieldName) : self
60
    {
61 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));
62
    }
63
64 2
    public static function discriminatorFieldConflict(string $document, string $fieldName) : self
65
    {
66 2
        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
    }
73
74
    public static function invalidDiscriminatorValue(string $value, string $owningClass) : self
75
    {
76
        return new self(sprintf("Discriminator value '%s' used in the declaration of class '%s' does not exist.", $value, $owningClass));
77
    }
78
79
    public static function missingFieldName(string $className) : self
80
    {
81
        return new self(sprintf("The Document class '%s' field mapping misses the 'fieldName' attribute.", $className));
82
    }
83
84 3
    public static function classIsNotAValidDocument(string $className) : self
85
    {
86 3
        return new self(sprintf('Class %s is not a valid document or mapped super class.', $className));
87
    }
88
89 5
    public static function classCanOnlyBeMappedByOneAbstractDocument(string $className, AbstractDocument $mappedAs, AbstractDocument $offending) : self
90
    {
91 5
        return new self(sprintf(
92 5
            "Can not map class '%s' as %s because it was already mapped as %s.",
93 5
            $className,
94 5
            (new ReflectionObject($offending))->getShortName(),
95 5
            (new ReflectionObject($mappedAs))->getShortName()
96
        ));
97
    }
98
99
    public static function reflectionFailure(string $document, ReflectionException $previousException) : self
100
    {
101
        return new self('An error occurred in ' . $document, 0, $previousException);
102
    }
103
104
    public static function identifierRequired(string $documentName) : self
105
    {
106
        return new self(sprintf("No identifier/primary key specified for Document '%s'. Every Document must have an identifier/primary key.", $documentName));
107
    }
108
109
    public static function missingIdentifierField(string $className, string $fieldName) : self
110
    {
111
        return new self(sprintf('The identifier %s is missing for a query of %s', $fieldName, $className));
112
    }
113
114
    public static function missingIdGeneratorClass(string $className) : self
115
    {
116
        return new self(sprintf('The class-option for the custom ID generator is missing in class %s.', $className));
117
    }
118
119
    public static function classIsNotAValidGenerator(string $className) : self
120
    {
121
        return new self(sprintf('The class %s if not a valid ID generator of type AbstractIdGenerator.', $className));
122
    }
123
124
    public static function missingGeneratorSetter(string $className, string $optionName) : self
125
    {
126
        return new self(sprintf('The class %s is missing a setter for the option %s.', $className, $optionName));
127
    }
128
129 1
    public static function cascadeOnEmbeddedNotAllowed(string $className, string $fieldName) : self
130
    {
131 1
        return new self(sprintf('Cascade on %s::%s is not allowed.', $className, $fieldName));
132
    }
133
134 3
    public static function simpleReferenceRequiresTargetDocument(string $className, string $fieldName) : self
135
    {
136 3
        return new self(sprintf('Target document must be specified for identifier reference: %s::%s', $className, $fieldName));
137
    }
138
139 1
    public static function simpleReferenceMustNotTargetDiscriminatedDocument(string $targetDocument) : self
140
    {
141 1
        return new self(sprintf('Identifier reference must not target document using Single Collection Inheritance, %s targeted.', $targetDocument));
142
    }
143
144 1
    public static function atomicCollectionStrategyNotAllowed(string $strategy, string $className, string $fieldName) : self
145
    {
146 1
        return new self(sprintf('%s collection strategy can be used only in top level document, used in %s::%s', $strategy, $className, $fieldName));
147
    }
148
149 4
    public static function owningAndInverseReferencesRequireTargetDocument(string $className, string $fieldName) : self
150
    {
151 4
        return new self(sprintf('Target document must be specified for owning/inverse sides of reference: %s::%s', $className, $fieldName));
152
    }
153
154 1
    public static function mustNotChangeIdentifierFieldsType(string $className, string $fieldName) : self
155
    {
156 1
        return new self(sprintf('%s::%s was declared an identifier and must stay this way.', $className, $fieldName));
157
    }
158
159 1
    public static function referenceManySortMustNotBeUsedWithNonSetCollectionStrategy(string $className, string $fieldName, string $strategy) : self
160
    {
161 1
        return new self(sprintf("ReferenceMany's sort can not be used with addToSet and pushAll strategies, %s used in %s::%s", $strategy, $className, $fieldName));
162
    }
163
164
    public static function invalidStorageStrategy(string $className, string $fieldName, string $type, string $strategy) : self
165
    {
166
        return new self(sprintf('Invalid strategy %s used in %s::%s with type %s', $strategy, $className, $fieldName, $type));
167
    }
168
169 1
    public static function collectionClassDoesNotImplementCommonInterface(string $className, string $fieldName, string $collectionClass) : self
170
    {
171 1
        return new self(sprintf('%s used as custom collection class for %s::%s has to implement %s interface.', $collectionClass, $className, $fieldName, Collection::class));
172
    }
173
174 2
    public static function shardKeyInSingleCollInheritanceSubclass(string $subclassName) : self
175
    {
176 2
        return new self(sprintf('Shard key overriding in subclass is forbidden for single collection inheritance: %s', $subclassName));
177
    }
178
179 2
    public static function embeddedDocumentCantHaveShardKey(string $className) : self
180
    {
181 2
        return new self(sprintf("Embedded document can't have shard key: %s", $className));
182
    }
183
184 1
    public static function onlySetStrategyAllowedInShardKey(string $className, string $fieldName) : self
185
    {
186 1
        return new self(sprintf('Only fields using the SET strategy can be used in the shard key: %s::%s', $className, $fieldName));
187
    }
188
189 3
    public static function noMultiKeyShardKeys(string $className, string $fieldName) : self
190
    {
191 3
        return new self(sprintf('No multikey indexes are allowed in the shard key: %s::%s', $className, $fieldName));
192
    }
193
194
    public static function cannotLookupDbRefReference(string $className, string $fieldName) : self
195
    {
196
        return new self(sprintf("Cannot use reference '%s' in class '%s' for lookup or graphLookup: dbRef references are not supported.", $fieldName, $className));
197
    }
198
199
    public static function repositoryMethodLookupNotAllowed(string $className, string $fieldName) : self
200
    {
201
        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));
202
    }
203
204 1
    public static function cannotUseShardedCollectionInOutStage(string $className) : self
205
    {
206 1
        return new self(sprintf("Cannot use class '%s' as collection for out stage. Sharded collections are not allowed.", $className));
207
    }
208
209 3
    public static function cannotUseShardedCollectionInLookupStages(string $className) : self
210
    {
211 3
        return new self(sprintf("Cannot use class '%s' as collection for lookup or graphLookup stage. Sharded collections are not allowed.", $className));
212
    }
213
214
    public static function referencePrimersOnlySupportedForInverseReferenceMany(string $className, string $fieldName) : self
215
    {
216
        return new self(sprintf("Cannot use reference priming on '%s' in class '%s'. Reference priming is only supported for inverse references", $fieldName, $className));
217
    }
218
219 1
    public static function connectFromFieldMustReferenceSameDocument(string $fieldName) : self
220
    {
221 1
        return new self(sprintf("Cannot use field '%s' as connectFromField in a \$graphLookup stage. Reference must target the document itself.", $fieldName));
222
    }
223
224 3
    public static function repositoryMethodCanNotBeCombinedWithSkipLimitAndSort(string $className, string $fieldName) : self
225
    {
226 3
        return new self(sprintf("'repositoryMethod' used on '%s' in class '%s' can not be combined with skip, limit or sort.", $fieldName, $className));
227
    }
228
229 2
    public static function xmlMappingFileInvalid(string $filename, string $errorDetails) : self
230
    {
231 2
        return new self(sprintf("The mapping file %s is invalid: \n%s", $filename, $errorDetails));
232
    }
233
234 1
    public static function fieldNotAllowedForGridFS(string $className, string $fieldName) : self
235
    {
236 1
        return new self(sprintf("Field '%s' in class '%s' is not a valid field for GridFS documents. You should move it to an embedded metadata document.", $fieldName, $className));
237
    }
238
239
    public static function discriminatorNotAllowedForGridFS(string $className) : self
240
    {
241
        return new self(sprintf("Class '%s' cannot be discriminated because it is marked as a GridFS file", $className));
242
    }
243
}
244