Completed
Push — master ( 09b86b...ba0798 )
by Andreas
20:05 queued 12s
created

MappingException::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.032
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 Throwable;
13
use const E_USER_DEPRECATED;
14
use function sprintf;
15
use function trigger_error;
16
17
/**
18
 * Class for all exceptions related to the Doctrine MongoDB ODM
19
 *
20
 * @final
21
 */
22
class MappingException extends BaseMappingException
23
{
24 68
    public function __construct($message = '', $code = 0, ?Throwable $previous = null)
25
    {
26 68
        if (self::class !== static::class) {
27
            @trigger_error(sprintf('The class "%s" extends "%s" which will be final in MongoDB ODM 2.0.', static::class, self::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

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