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 |
||
27 | class Connection extends Doctrine\DBAL\Connection |
||
28 | { |
||
29 | |||
30 | use \Kdyby\StrictObjects\Scream; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | public $throwOldKdybyExceptions = FALSE; |
||
36 | |||
37 | /** @deprecated */ |
||
38 | const MYSQL_ERR_UNIQUE = 1062; |
||
39 | /** @deprecated */ |
||
40 | const MYSQL_ERR_NOT_NULL = 1048; |
||
41 | |||
42 | /** @deprecated */ |
||
43 | const SQLITE_ERR_UNIQUE = 19; |
||
44 | |||
45 | /** @deprecated */ |
||
46 | const POSTGRE_ERR_UNIQUE = 23505; // todo: verify, source: http://www.postgresql.org/docs/8.2/static/errcodes-appendix.html |
||
47 | |||
48 | /** |
||
49 | * @var Doctrine\ORM\EntityManager |
||
50 | */ |
||
51 | private $entityManager; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $schemaTypes = []; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | private $dbalTypes = []; |
||
62 | |||
63 | |||
64 | |||
65 | /** |
||
66 | * @internal |
||
67 | * @param Doctrine\ORM\EntityManager $em |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function bindEntityManager(Doctrine\ORM\EntityManager $em) |
||
75 | |||
76 | |||
77 | |||
78 | /** |
||
79 | * Tries to autodetect, if identifier has to be quoted and quotes it. |
||
80 | * |
||
81 | * @param string $expression |
||
82 | * @return string |
||
83 | */ |
||
84 | public function quoteIdentifier($expression) |
||
93 | |||
94 | |||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function delete($tableExpression, array $identifier, array $types = []) |
||
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function update($tableExpression, array $data, array $identifier, array $types = []) |
||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function insert($tableExpression, array $data, array $types = []) |
||
143 | |||
144 | |||
145 | |||
146 | /** |
||
147 | * @param string $query |
||
148 | * @param array $params |
||
149 | * @param array $types |
||
150 | * @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp |
||
151 | * @return Driver\ResultStatement |
||
152 | * @throws DBALException |
||
153 | */ |
||
154 | public function executeQuery($query, array $params = [], $types = [], Doctrine\DBAL\Cache\QueryCacheProfile $qcp = NULL) |
||
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * @param string $query |
||
168 | * @param array $params |
||
169 | * @param array $types |
||
170 | * @return int |
||
171 | * @throws DBALException |
||
172 | */ |
||
173 | public function executeUpdate($query, array $params = [], array $types = []) |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * @param string $statement |
||
187 | * @return int |
||
188 | * @throws DBALException |
||
189 | */ |
||
190 | public function exec($statement) |
||
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * @return \Doctrine\DBAL\Driver\Statement|mixed |
||
204 | * @throws DBALException |
||
205 | */ |
||
206 | public function query() |
||
216 | |||
217 | |||
218 | |||
219 | /** |
||
220 | * Prepares an SQL statement. |
||
221 | * |
||
222 | * @param string $statement The SQL statement to prepare. |
||
223 | * @throws DBALException |
||
224 | * @return PDOStatement The prepared statement. |
||
225 | */ |
||
226 | public function prepare($statement) |
||
241 | |||
242 | |||
243 | |||
244 | /** |
||
245 | * @return Doctrine\DBAL\Query\QueryBuilder|NativeQueryBuilder |
||
246 | */ |
||
247 | public function createQueryBuilder() |
||
255 | |||
256 | |||
257 | |||
258 | /** |
||
259 | * @param array $schemaTypes |
||
260 | */ |
||
261 | public function setSchemaTypes(array $schemaTypes) |
||
265 | |||
266 | |||
267 | |||
268 | /** |
||
269 | * @param array $dbalTypes |
||
270 | */ |
||
271 | public function setDbalTypes(array $dbalTypes) |
||
275 | |||
276 | |||
277 | |||
278 | public function connect() |
||
307 | |||
308 | |||
309 | |||
310 | /** |
||
311 | * @return Doctrine\DBAL\Platforms\AbstractPlatform |
||
312 | */ |
||
313 | public function getDatabasePlatform() |
||
321 | |||
322 | |||
323 | |||
324 | public function ping() |
||
350 | |||
351 | |||
352 | |||
353 | /** |
||
354 | * @param array $params |
||
355 | * @param \Doctrine\DBAL\Configuration $config |
||
356 | * @param \Doctrine\Common\EventManager $eventManager |
||
357 | * @return \Kdyby\Doctrine\Connection |
||
358 | */ |
||
359 | public static function create(array $params, Doctrine\DBAL\Configuration $config, EventManager $eventManager) |
||
369 | |||
370 | |||
371 | |||
372 | /** |
||
373 | * @deprecated |
||
374 | * @internal |
||
375 | * @param \Exception|\Throwable $e |
||
376 | * @param string $query |
||
377 | * @param array $params |
||
378 | * @return \Kdyby\Doctrine\DBALException|\Exception|\Throwable |
||
379 | */ |
||
380 | public function resolveException($e, $query = NULL, $params = []) |
||
432 | |||
433 | |||
434 | |||
435 | /** |
||
436 | * @param \Exception|\Throwable $e |
||
437 | * @return string|NULL |
||
438 | */ |
||
439 | private static function resolveExceptionTable($e) |
||
453 | |||
454 | } |
||
455 |
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.