Failed Conditions
Pull Request — develop (#6935)
by Michael
65:23
created

ORMException   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 291
Duplicated Lines 4.81 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 64.18%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 14
loc 291
ccs 43
cts 67
cp 0.6418
rs 10
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidFindByInverseAssociation() 0 7 1
A invalidResultCacheDriver() 0 4 1
A notSupported() 0 4 1
A queryCacheNotConfigured() 0 4 1
A metadataCacheNotConfigured() 0 4 1
A queryCacheUsesNonPersistentCache() 0 4 1
A metadataCacheUsesNonPersistentCache() 0 4 1
A proxyClassesAlwaysRegenerating() 0 4 1
A missingMappingDriverImpl() 0 5 1
A namedQueryNotFound() 0 4 1
A namedNativeQueryNotFound() 0 4 1
A entityMissingForeignAssignedId() 0 10 1
A entityMissingAssignedIdForField() 0 8 1
A unrecognizedField() 0 4 1
A unexpectedAssociationValue() 0 4 1
A invalidOrientation() 0 4 1
A invalidFlushMode() 0 4 1
A entityManagerClosed() 0 4 1
A invalidHydrationMode() 0 4 1
A mismatchedEventManager() 0 4 1
A findByRequiresParameter() 0 4 1
A invalidFindByCall() 7 7 1
A invalidMagicCall() 7 7 1
A invalidEntityRepository() 0 4 1
A missingIdentifierField() 0 4 1
A unrecognizedIdentifierFields() 0 7 1
A cantUseInOperatorOnCompositeKeys() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM;
6
7
use Doctrine\Common\Cache\Cache as CacheDriver;
8
use Exception;
9
10
/**
11
 * Base exception class for all ORM exceptions.
12
 *
13
 * @author Roman Borschel <[email protected]>
14
 * @since 2.0
15
 */
16
class ORMException extends Exception
17
{
18
    /**
19
     * @return ORMException
20
     */
21
    public static function missingMappingDriverImpl()
22
    {
23
        return new self("It's a requirement to specify a Metadata Driver and pass it ".
24
            "to Doctrine\\ORM\\Configuration::setMetadataDriverImpl().");
25
    }
26
27
    /**
28
     * @param string $queryName
29
     *
30
     * @return ORMException
31
     */
32
    public static function namedQueryNotFound($queryName)
33
    {
34
        return new self('Could not find a named query by the name "' . $queryName . '"');
35
    }
36
37
    /**
38
     * @param string $nativeQueryName
39
     *
40
     * @return ORMException
41
     */
42
    public static function namedNativeQueryNotFound($nativeQueryName)
43
    {
44
        return new self('Could not find a named native query by the name "' . $nativeQueryName . '"');
45
    }
46
47 2
    /**
48
     * @param object $entity
49 2
     * @param object $relatedEntity
50
     *
51
     * @return ORMException
52
     */
53
    public static function entityMissingForeignAssignedId($entity, $relatedEntity)
54
    {
55
        return new self(
56
            "Entity of type " . get_class($entity) . " has identity through a foreign entity " . get_class($relatedEntity) . ", " .
57
            "however this entity has no identity itself. You have to call EntityManager#persist() on the related entity " .
58
            "and make sure that an identifier was generated before trying to persist '" . get_class($entity) . "'. In case " .
59
            "of Post Insert ID Generation (such as MySQL Auto-Increment) this means you have to call " .
60
            "EntityManager#flush() between both persist operations."
61
        );
62
    }
63
64
    /**
65
     * @param object $entity
66
     * @param string $field
67
     *
68
     * @return ORMException
69
     */
70
    public static function entityMissingAssignedIdForField($entity, $field)
71
    {
72
        return new self("Entity of type " . get_class($entity) . " is missing an assigned ID for field  '" . $field . "'. " .
73
            "The identifier generation strategy for this entity requires the ID field to be populated before ".
74
            "EntityManager#persist() is called. If you want automatically generated identifiers instead " .
75
            "you need to adjust the metadata mapping accordingly."
76
        );
77
    }
78
79
    /**
80
     * @param string $field
81
     *
82
     * @return ORMException
83
     */
84
    public static function unrecognizedField($field)
85 1
    {
86
        return new self("Unrecognized field: $field");
87 1
    }
88 1
89 1
    /**
90 1
     *
91
     * @param string $class
92
     * @param string $association
93
     * @param string $given
94
     * @param string $expected
95
     *
96
     * @return \Doctrine\ORM\ORMInvalidArgumentException
97
     */
98
    public static function unexpectedAssociationValue($class, $association, $given, $expected)
99 3
    {
100
        return new self(sprintf('Found entity of type %s on association %s#%s, but expecting %s', $given, $class, $association, $expected));
101 3
    }
102
103
    /**
104
     * @param string $className
105
     * @param string $field
106
     *
107
     * @return ORMException
108
     */
109
    public static function invalidOrientation($className, $field)
110
    {
111
        return new self("Invalid order by orientation specified for " . $className . "#" . $field);
112
    }
113
114
    /**
115
     * @param string $mode
116
     *
117
     * @return ORMException
118
     */
119
    public static function invalidFlushMode($mode)
120
    {
121
        return new self("'$mode' is an invalid flush mode.");
122
    }
123
124 1
    /**
125
     * @return ORMException
126 1
     */
127
    public static function entityManagerClosed()
128
    {
129
        return new self("The EntityManager is closed.");
130
    }
131
132
    /**
133
     * @param string $mode
134
     *
135
     * @return ORMException
136
     */
137
    public static function invalidHydrationMode($mode)
138
    {
139
        return new self("'$mode' is an invalid hydration mode.");
140
    }
141
142 5
    /**
143
     * @return ORMException
144 5
     */
145
    public static function mismatchedEventManager()
146
    {
147
        return new self("Cannot use different EventManager instances for EntityManager and Connection.");
148
    }
149
150
    /**
151
     * @param string $methodName
152
     *
153
     * @return ORMException
154
     */
155
    public static function findByRequiresParameter($methodName)
156
    {
157
        return new self("You need to pass a parameter to '".$methodName."'");
158
    }
159
160
    /**
161
     * @param string $entityName
162
     * @param string $fieldName
163
     * @param string $method
164
     *
165
     * @return ORMException
166
     */
167 View Code Duplication
    public static function invalidFindByCall($entityName, $fieldName, $method)
168
    {
169
        return new self(
170 1
            "Entity '".$entityName."' has no field '".$fieldName."'. ".
171
            "You can therefore not call '".$method."' on the entities' repository"
172 1
        );
173
    }
174
175
    /**
176
     * @param string $entityName
177
     * @param string $fieldName
178
     * @param string $method
179
     *
180
     * @return ORMException
181
     */
182 1 View Code Duplication
    public static function invalidMagicCall($entityName, $fieldName, $method)
183
    {
184 1
        return new self(
185 1
            "Entity '".$entityName."' has no field '".$fieldName."'. ".
186 1
            "You can therefore not call '".$method."' on the entities' repository"
187
        );
188
    }
189
190
    /**
191
     * @param string $entityName
192
     * @param string $associationFieldName
193
     *
194
     * @return ORMException
195
     */
196 2
    public static function invalidFindByInverseAssociation($entityName, $associationFieldName)
197
    {
198 2
        return new self(
199 2
            "You cannot search for the association field '".$entityName."#".$associationFieldName."', ".
200 2
            "because it is the inverse side of an association. Find methods only work on owning side associations."
201
        );
202
    }
203
204
    /**
205
     * @return ORMException
206
     */
207
    public static function invalidResultCacheDriver()
208
    {
209
        return new self("Invalid result cache driver; it must implement Doctrine\\Common\\Cache\\Cache.");
210
    }
211
212
    /**
213
     * @return ORMException
214
     */
215
    public static function notSupported()
216
    {
217
        return new self("This behaviour is (currently) not supported by Doctrine 2");
218
    }
219
220
    /**
221
     * @return ORMException
222
     */
223 1
    public static function queryCacheNotConfigured()
224
    {
225 1
        return new self('Query Cache is not configured.');
226
    }
227
228
    /**
229
     * @return ORMException
230
     */
231 1
    public static function metadataCacheNotConfigured()
232
    {
233 1
        return new self('Class Metadata Cache is not configured.');
234
    }
235
236
    /**
237
     * @param \Doctrine\Common\Cache\Cache $cache
238
     *
239
     * @return ORMException
240
     */
241 1
    public static function queryCacheUsesNonPersistentCache(CacheDriver $cache)
242
    {
243 1
        return new self('Query Cache uses a non-persistent cache driver, ' . get_class($cache) . '.');
244
    }
245
246
    /**
247
     * @param \Doctrine\Common\Cache\Cache $cache
248
     *
249
     * @return ORMException
250
     */
251 1
    public static function metadataCacheUsesNonPersistentCache(CacheDriver $cache)
252
    {
253 1
        return new self('Metadata Cache uses a non-persistent cache driver, ' . get_class($cache) . '.');
254
    }
255
256
    /**
257
     * @return ORMException
258
     */
259 3
    public static function proxyClassesAlwaysRegenerating()
260
    {
261 3
        return new self('Proxy Classes are always regenerating.');
262
    }
263
264
    /**
265
     * @param string $className
266
     *
267
     * @return ORMException
268
     */
269 1
    public static function invalidEntityRepository($className)
270
    {
271 1
        return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository.");
272 1
    }
273
274
    /**
275
     * @param string $className
276
     * @param string $fieldName
277
     *
278
     * @return ORMException
279
     */
280
    public static function missingIdentifierField($className, $fieldName)
281 1
    {
282
        return new self("The identifier $fieldName is missing for a query of " . $className);
283 1
    }
284
285
    /**
286
     * @param string $className
287
     * @param string[] $fieldNames
288
     *
289
     * @return ORMException
290
     */
291
    public static function unrecognizedIdentifierFields($className, $fieldNames)
292 1
    {
293
        return new self(
294 1
            "Unrecognized identifier fields: '" . implode("', '", $fieldNames) . "' " .
295
            "are not present on class '" . $className . "'."
296
        );
297
    }
298
299
    /**
300
     * @return ORMException
301
     */
302
    public static function cantUseInOperatorOnCompositeKeys()
303 2
    {
304
        return new self("Can't use IN operator on entities that have composite keys.");
305 2
    }
306
}
307