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 |
||
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) |
|
203 | |||
204 | /** |
||
205 | * @return ORMException |
||
206 | */ |
||
207 | public static function invalidResultCacheDriver() |
||
211 | |||
212 | /** |
||
213 | * @return ORMException |
||
214 | */ |
||
215 | public static function notSupported() |
||
219 | |||
220 | /** |
||
221 | * @return ORMException |
||
222 | */ |
||
223 | 1 | public static function queryCacheNotConfigured() |
|
227 | |||
228 | /** |
||
229 | * @return ORMException |
||
230 | */ |
||
231 | 1 | public static function metadataCacheNotConfigured() |
|
235 | |||
236 | /** |
||
237 | * @param \Doctrine\Common\Cache\Cache $cache |
||
238 | * |
||
239 | * @return ORMException |
||
240 | */ |
||
241 | 1 | public static function queryCacheUsesNonPersistentCache(CacheDriver $cache) |
|
245 | |||
246 | /** |
||
247 | * @param \Doctrine\Common\Cache\Cache $cache |
||
248 | * |
||
249 | * @return ORMException |
||
250 | */ |
||
251 | 1 | public static function metadataCacheUsesNonPersistentCache(CacheDriver $cache) |
|
255 | |||
256 | /** |
||
257 | * @return ORMException |
||
258 | */ |
||
259 | 3 | public static function proxyClassesAlwaysRegenerating() |
|
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) |
||
284 | |||
285 | /** |
||
286 | * @param string $className |
||
287 | * @param string[] $fieldNames |
||
288 | * |
||
289 | * @return ORMException |
||
290 | */ |
||
291 | public static function unrecognizedIdentifierFields($className, $fieldNames) |
||
298 | |||
299 | /** |
||
300 | * @return ORMException |
||
301 | */ |
||
302 | public static function cantUseInOperatorOnCompositeKeys() |
||
306 | } |
||
307 |