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 |
||
28 | class Connection extends Doctrine\DBAL\Connection |
||
29 | { |
||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | public $throwOldKdybyExceptions = FALSE; |
||
34 | |||
35 | /** @deprecated */ |
||
36 | const MYSQL_ERR_UNIQUE = 1062; |
||
37 | /** @deprecated */ |
||
38 | const MYSQL_ERR_NOT_NULL = 1048; |
||
39 | |||
40 | /** @deprecated */ |
||
41 | const SQLITE_ERR_UNIQUE = 19; |
||
42 | |||
43 | /** @deprecated */ |
||
44 | const POSTGRE_ERR_UNIQUE = 23505; // todo: verify, source: http://www.postgresql.org/docs/8.2/static/errcodes-appendix.html |
||
45 | |||
46 | /** |
||
47 | * @var Doctrine\ORM\EntityManager |
||
48 | */ |
||
49 | private $entityManager; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $schemaTypes = []; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $dbalTypes = []; |
||
60 | |||
61 | |||
62 | |||
63 | /** |
||
64 | * @internal |
||
65 | * @param Doctrine\ORM\EntityManager $em |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function bindEntityManager(Doctrine\ORM\EntityManager $em) |
||
73 | |||
74 | |||
75 | |||
76 | /** |
||
77 | * Tries to autodetect, if identifier has to be quoted and quotes it. |
||
78 | * |
||
79 | * @param string $expression |
||
80 | * @return string |
||
81 | */ |
||
82 | public function quoteIdentifier($expression) |
||
91 | |||
92 | |||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function delete($tableExpression, array $identifier, array $types = []) |
||
98 | { |
||
99 | $fixedIdentifier = []; |
||
100 | foreach ($identifier as $columnName => $value) { |
||
101 | $fixedIdentifier[$this->quoteIdentifier($columnName)] = $value; |
||
102 | } |
||
103 | |||
104 | return parent::delete($this->quoteIdentifier($tableExpression), $fixedIdentifier, $types); |
||
105 | } |
||
106 | |||
107 | |||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function update($tableExpression, array $data, array $identifier, array $types = []) |
||
113 | { |
||
114 | $fixedData = []; |
||
115 | foreach ($data as $columnName => $value) { |
||
116 | $fixedData[$this->quoteIdentifier($columnName)] = $value; |
||
117 | } |
||
118 | |||
119 | $fixedIdentifier = []; |
||
120 | foreach ($identifier as $columnName => $value) { |
||
121 | $fixedIdentifier[$this->quoteIdentifier($columnName)] = $value; |
||
122 | } |
||
123 | |||
124 | return parent::update($this->quoteIdentifier($tableExpression), $fixedData, $fixedIdentifier, $types); |
||
125 | } |
||
126 | |||
127 | |||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function insert($tableExpression, array $data, array $types = []) |
||
133 | { |
||
134 | $fixedData = []; |
||
135 | foreach ($data as $columnName => $value) { |
||
136 | $fixedData[$this->quoteIdentifier($columnName)] = $value; |
||
137 | } |
||
138 | |||
139 | return parent::insert($this->quoteIdentifier($tableExpression), $fixedData, $types); |
||
140 | } |
||
141 | |||
142 | |||
143 | |||
144 | /** |
||
145 | * @param string $query |
||
146 | * @param array $params |
||
147 | * @param array $types |
||
148 | * @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp |
||
149 | * @return \Doctrine\DBAL\Driver\Statement |
||
150 | * @throws DBALException |
||
151 | */ |
||
152 | public function executeQuery($query, array $params = [], $types = [], Doctrine\DBAL\Cache\QueryCacheProfile $qcp = NULL) |
||
161 | |||
162 | |||
163 | |||
164 | /** |
||
165 | * @param string $query |
||
166 | * @param array $params |
||
167 | * @param array $types |
||
168 | * @return int |
||
169 | * @throws DBALException |
||
170 | */ |
||
171 | public function executeUpdate($query, array $params = [], array $types = []) |
||
180 | |||
181 | |||
182 | |||
183 | /** |
||
184 | * @param string $statement |
||
185 | * @return int |
||
186 | * @throws DBALException |
||
187 | */ |
||
188 | public function exec($statement) |
||
197 | |||
198 | |||
199 | |||
200 | /** |
||
201 | * @return \Doctrine\DBAL\Driver\Statement|mixed |
||
202 | * @throws DBALException |
||
203 | */ |
||
204 | public function query() |
||
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * Prepares an SQL statement. |
||
219 | * |
||
220 | * @param string $statement The SQL statement to prepare. |
||
221 | * @throws DBALException |
||
222 | * @return PDOStatement The prepared statement. |
||
223 | */ |
||
224 | public function prepare($statement) |
||
239 | |||
240 | |||
241 | |||
242 | /** |
||
243 | * @return Doctrine\DBAL\Query\QueryBuilder|NativeQueryBuilder |
||
244 | */ |
||
245 | public function createQueryBuilder() |
||
253 | |||
254 | |||
255 | |||
256 | /** |
||
257 | * @param array $schemaTypes |
||
258 | */ |
||
259 | public function setSchemaTypes(array $schemaTypes) |
||
263 | |||
264 | |||
265 | |||
266 | /** |
||
267 | * @param array $dbalTypes |
||
268 | */ |
||
269 | public function setDbalTypes(array $dbalTypes) |
||
273 | |||
274 | |||
275 | |||
276 | public function connect() |
||
277 | { |
||
278 | if ($this->isConnected()) { |
||
279 | return FALSE; |
||
280 | } |
||
281 | |||
282 | foreach ($this->dbalTypes as $name => $className) { |
||
283 | if (Type::hasType($name)) { |
||
284 | Type::overrideType($name, $className); |
||
285 | |||
286 | } else { |
||
287 | Type::addType($name, $className); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | parent::connect(); |
||
292 | |||
293 | $platform = $this->getDatabasePlatform(); |
||
294 | |||
295 | foreach ($this->schemaTypes as $dbType => $doctrineType) { |
||
296 | $platform->registerDoctrineTypeMapping($dbType, $doctrineType); |
||
297 | } |
||
298 | |||
299 | foreach ($this->dbalTypes as $type => $className) { |
||
300 | $platform->markDoctrineTypeCommented(Type::getType($type)); |
||
301 | } |
||
302 | |||
303 | return TRUE; |
||
304 | } |
||
305 | |||
306 | |||
307 | |||
308 | /** |
||
309 | * @return Doctrine\DBAL\Platforms\AbstractPlatform |
||
310 | */ |
||
311 | public function getDatabasePlatform() |
||
312 | { |
||
313 | if (!$this->isConnected()) { |
||
314 | $this->connect(); |
||
315 | } |
||
316 | |||
317 | return parent::getDatabasePlatform(); |
||
318 | } |
||
319 | |||
320 | |||
321 | |||
322 | public function ping() |
||
323 | { |
||
324 | $conn = $this->getWrappedConnection(); |
||
325 | if ($conn instanceof Driver\PingableConnection) { |
||
326 | return $conn->ping(); |
||
327 | } |
||
328 | |||
329 | set_error_handler(function ($severity, $message) { |
||
330 | throw new \PDOException($message, $severity); |
||
331 | }); |
||
332 | |||
333 | try { |
||
334 | $this->query($this->getDatabasePlatform()->getDummySelectSQL()); |
||
335 | restore_error_handler(); |
||
336 | |||
337 | return TRUE; |
||
338 | |||
339 | } catch (Doctrine\DBAL\DBALException $e) { |
||
340 | restore_error_handler(); |
||
341 | return FALSE; |
||
342 | |||
343 | } catch (\Exception $e) { |
||
344 | restore_error_handler(); |
||
345 | throw $e; |
||
346 | } |
||
347 | } |
||
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * @param array $params |
||
353 | * @param \Doctrine\DBAL\Configuration $config |
||
354 | * @param \Doctrine\Common\EventManager $eventManager |
||
355 | * @return Connection |
||
356 | */ |
||
357 | public static function create(array $params, Doctrine\DBAL\Configuration $config, EventManager $eventManager) |
||
365 | |||
366 | |||
367 | |||
368 | /** |
||
369 | * @deprecated |
||
370 | * @internal |
||
371 | * @param \Exception|\Throwable $e |
||
372 | * @param string $query |
||
373 | * @param array $params |
||
374 | * @return DBALException |
||
375 | */ |
||
376 | public function resolveException($e, $query = NULL, $params = []) |
||
377 | { |
||
378 | if ($this->throwOldKdybyExceptions !== TRUE) { |
||
379 | return $e; |
||
380 | } |
||
381 | |||
382 | if ($e instanceof Doctrine\DBAL\DBALException && ($pe = $e->getPrevious()) instanceof \PDOException) { |
||
383 | $info = $pe->errorInfo; |
||
384 | |||
385 | } elseif ($e instanceof \PDOException) { |
||
386 | $info = $e->errorInfo; |
||
387 | |||
388 | } else { |
||
389 | return new DBALException($e, $query, $params, $this); |
||
390 | } |
||
391 | |||
392 | if ($this->getDriver() instanceof Doctrine\DBAL\Driver\PDOMySql\Driver) { |
||
393 | if ($info[0] == 23000 && $info[1] == self::MYSQL_ERR_UNIQUE) { // unique fail |
||
394 | $columns = []; |
||
395 | |||
396 | try { |
||
397 | if (preg_match('~Duplicate entry .*? for key \'([^\']+)\'~', $info[2], $m) |
||
398 | && ($table = self::resolveExceptionTable($e)) |
||
399 | && ($indexes = $this->getSchemaManager()->listTableIndexes($table)) |
||
400 | && isset($indexes[$m[1]]) |
||
401 | ) { |
||
402 | $columns[$m[1]] = $indexes[$m[1]]->getColumns(); |
||
403 | } |
||
404 | |||
405 | } catch (\Exception $e) { } |
||
406 | |||
407 | return new DuplicateEntryException($e, $columns, $query, $params, $this); |
||
408 | |||
409 | } elseif ($info[0] == 23000 && $info[1] == self::MYSQL_ERR_NOT_NULL) { // notnull fail |
||
410 | $column = NULL; |
||
411 | if (preg_match('~Column \'([^\']+)\'~', $info[2], $m)) { |
||
412 | $column = $m[1]; |
||
413 | } |
||
414 | |||
415 | return new EmptyValueException($e, $column, $query, $params, $this); |
||
416 | } |
||
417 | } |
||
418 | |||
419 | $raw = $e; |
||
420 | do { |
||
421 | $raw = $raw->getPrevious(); |
||
422 | } while ($raw && !$raw instanceof \PDOException); |
||
423 | |||
424 | return new DBALException($e, $query, $params, $this, $raw ? $raw->getMessage() : $e->getMessage()); |
||
425 | } |
||
426 | |||
427 | |||
428 | |||
429 | /** |
||
430 | * @param \Exception|\Throwable $e |
||
431 | * @return string|NULL |
||
432 | */ |
||
433 | private static function resolveExceptionTable($e) |
||
447 | |||
448 | |||
449 | |||
450 | /*************************** Nette\Object ***************************/ |
||
451 | |||
452 | |||
453 | |||
454 | /** |
||
455 | * Access to reflection. |
||
456 | * @return \Nette\Reflection\ClassType |
||
457 | */ |
||
458 | public static function getReflection() |
||
462 | |||
463 | |||
464 | |||
465 | /** |
||
466 | * Call to undefined method. |
||
467 | * |
||
468 | * @param string $name |
||
469 | * @param array $args |
||
470 | * |
||
471 | * @throws \Nette\MemberAccessException |
||
472 | * @return mixed |
||
473 | */ |
||
474 | public function __call($name, $args) |
||
478 | |||
479 | |||
480 | |||
481 | /** |
||
482 | * Call to undefined static method. |
||
483 | * |
||
484 | * @param string $name |
||
485 | * @param array $args |
||
486 | * |
||
487 | * @throws \Nette\MemberAccessException |
||
488 | * @return mixed |
||
489 | */ |
||
490 | public static function __callStatic($name, $args) |
||
494 | |||
495 | |||
496 | |||
497 | /** |
||
498 | * Adding method to class. |
||
499 | * |
||
500 | * @param $name |
||
501 | * @param null $callback |
||
502 | * |
||
503 | * @throws \Nette\MemberAccessException |
||
504 | * @return callable|null |
||
505 | */ |
||
506 | public static function extensionMethod($name, $callback = NULL) |
||
519 | |||
520 | |||
521 | |||
522 | /** |
||
523 | * Returns property value. Do not call directly. |
||
524 | * |
||
525 | * @param string $name |
||
526 | * |
||
527 | * @throws \Nette\MemberAccessException |
||
528 | * @return mixed |
||
529 | */ |
||
530 | public function &__get($name) |
||
534 | |||
535 | |||
536 | |||
537 | /** |
||
538 | * Sets value of a property. Do not call directly. |
||
539 | * |
||
540 | * @param string $name |
||
541 | * @param mixed $value |
||
542 | * |
||
543 | * @throws \Nette\MemberAccessException |
||
544 | * @return void |
||
545 | */ |
||
546 | public function __set($name, $value) |
||
555 | |||
556 | |||
557 | |||
558 | /** |
||
559 | * Is property defined? |
||
560 | * |
||
561 | * @param string $name |
||
562 | * |
||
563 | * @return bool |
||
564 | */ |
||
565 | public function __isset($name) |
||
569 | |||
570 | |||
571 | |||
572 | /** |
||
573 | * Access to undeclared property. |
||
574 | * |
||
575 | * @param string $name |
||
576 | * |
||
577 | * @throws \Nette\MemberAccessException |
||
578 | * @return void |
||
579 | */ |
||
580 | public function __unset($name) |
||
589 | |||
590 | } |
||
591 |
This method has been deprecated.