Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Connection extends Doctrine\DBAL\Connection |
||
30 | { |
||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | public $throwOldKdybyExceptions = FALSE; |
||
35 | |||
36 | /** @deprecated */ |
||
37 | const MYSQL_ERR_UNIQUE = 1062; |
||
38 | /** @deprecated */ |
||
39 | const MYSQL_ERR_NOT_NULL = 1048; |
||
40 | |||
41 | /** @deprecated */ |
||
42 | const SQLITE_ERR_UNIQUE = 19; |
||
43 | |||
44 | /** @deprecated */ |
||
45 | const POSTGRE_ERR_UNIQUE = 23505; // todo: verify, source: http://www.postgresql.org/docs/8.2/static/errcodes-appendix.html |
||
46 | |||
47 | /** |
||
48 | * @var Doctrine\ORM\EntityManager |
||
49 | */ |
||
50 | private $entityManager; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $schemaTypes = []; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $dbalTypes = []; |
||
61 | |||
62 | |||
63 | |||
64 | /** |
||
65 | * @internal |
||
66 | * @param Doctrine\ORM\EntityManager $em |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function bindEntityManager(Doctrine\ORM\EntityManager $em) |
||
74 | |||
75 | |||
76 | |||
77 | /** |
||
78 | * Tries to autodetect, if identifier has to be quoted and quotes it. |
||
79 | * |
||
80 | * @param string $expression |
||
81 | * @return string |
||
82 | */ |
||
83 | public function quoteIdentifier($expression) |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function delete($tableExpression, array $identifier, array $types = []) |
||
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function update($tableExpression, array $data, array $identifier, array $types = []) |
||
127 | |||
128 | |||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function insert($tableExpression, array $data, array $types = []) |
||
142 | |||
143 | |||
144 | |||
145 | /** |
||
146 | * Prepares an SQL statement. |
||
147 | * |
||
148 | * @param string $statement The SQL statement to prepare. |
||
149 | * @throws DBALException |
||
150 | * @return Statement The prepared statement. |
||
151 | */ |
||
152 | public function prepare($statement) |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | * @return Doctrine\DBAL\Query\QueryBuilder|NativeQueryBuilder |
||
172 | */ |
||
173 | public function createQueryBuilder() |
||
181 | |||
182 | |||
183 | |||
184 | /** |
||
185 | * @param array $schemaTypes |
||
186 | */ |
||
187 | public function setSchemaTypes(array $schemaTypes) |
||
191 | |||
192 | |||
193 | |||
194 | /** |
||
195 | * @param array $dbalTypes |
||
196 | */ |
||
197 | public function setDbalTypes(array $dbalTypes) |
||
201 | |||
202 | |||
203 | |||
204 | public function connect() |
||
233 | |||
234 | |||
235 | |||
236 | /** |
||
237 | * @return Doctrine\DBAL\Platforms\AbstractPlatform |
||
238 | */ |
||
239 | public function getDatabasePlatform() |
||
247 | |||
248 | |||
249 | |||
250 | public function ping() |
||
276 | |||
277 | |||
278 | |||
279 | /** |
||
280 | * @param array $params |
||
281 | * @param \Doctrine\DBAL\Configuration $config |
||
282 | * @param \Doctrine\Common\EventManager $eventManager |
||
283 | * @param array $dbalTypes |
||
284 | * @param array $schemaTypes |
||
285 | * @return Connection |
||
286 | */ |
||
287 | public static function create(array $params, Doctrine\DBAL\Configuration $config, EventManager $eventManager) |
||
295 | |||
296 | |||
297 | |||
298 | /*************************** Nette\Object ***************************/ |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * Access to reflection. |
||
304 | * @return \Nette\Reflection\ClassType |
||
305 | */ |
||
306 | public static function getReflection() |
||
310 | |||
311 | |||
312 | |||
313 | /** |
||
314 | * Call to undefined method. |
||
315 | * |
||
316 | * @param string $name |
||
317 | * @param array $args |
||
318 | * |
||
319 | * @throws \Nette\MemberAccessException |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function __call($name, $args) |
||
326 | |||
327 | |||
328 | |||
329 | /** |
||
330 | * Call to undefined static method. |
||
331 | * |
||
332 | * @param string $name |
||
333 | * @param array $args |
||
334 | * |
||
335 | * @throws \Nette\MemberAccessException |
||
336 | * @return mixed |
||
337 | */ |
||
338 | public static function __callStatic($name, $args) |
||
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * Adding method to class. |
||
347 | * |
||
348 | * @param $name |
||
349 | * @param null $callback |
||
350 | * |
||
351 | * @throws \Nette\MemberAccessException |
||
352 | * @return callable|null |
||
353 | */ |
||
354 | public static function extensionMethod($name, $callback = NULL) |
||
367 | |||
368 | |||
369 | |||
370 | /** |
||
371 | * Returns property value. Do not call directly. |
||
372 | * |
||
373 | * @param string $name |
||
374 | * |
||
375 | * @throws \Nette\MemberAccessException |
||
376 | * @return mixed |
||
377 | */ |
||
378 | public function &__get($name) |
||
382 | |||
383 | |||
384 | |||
385 | /** |
||
386 | * Sets value of a property. Do not call directly. |
||
387 | * |
||
388 | * @param string $name |
||
389 | * @param mixed $value |
||
390 | * |
||
391 | * @throws \Nette\MemberAccessException |
||
392 | * @return void |
||
393 | */ |
||
394 | public function __set($name, $value) |
||
403 | |||
404 | |||
405 | |||
406 | /** |
||
407 | * Is property defined? |
||
408 | * |
||
409 | * @param string $name |
||
410 | * |
||
411 | * @return bool |
||
412 | */ |
||
413 | public function __isset($name) |
||
417 | |||
418 | |||
419 | |||
420 | /** |
||
421 | * Access to undeclared property. |
||
422 | * |
||
423 | * @param string $name |
||
424 | * |
||
425 | * @throws \Nette\MemberAccessException |
||
426 | * @return void |
||
427 | */ |
||
428 | public function __unset($name) |
||
437 | |||
438 | } |
||
439 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.