Completed
Push — master ( 5a0aba...a14f38 )
by Ron
01:58
created

throwMoreConcreteException()   C

Complexity

Conditions 15
Paths 48

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
c 8
b 1
f 0
dl 0
loc 22
rs 5.8335
cc 15
eloc 18
nc 48
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Kir\MySQL\Databases\MySQL;
3
4
use Kir\MySQL\Exceptions\DatabaseHasGoneAwayException;
5
use Kir\MySQL\Exceptions\IntegrityConstraintViolationException;
6
use Kir\MySQL\Exceptions\SqlException;
7
use PDOException;
8
use Kir\MySQL\Exceptions\SqlDeadLockException;
9
use Kir\MySQL\Exceptions\DuplicateUniqueKeyException;
10
use Kir\MySQL\Exceptions\LockWaitTimeoutExceededException;
11
12
class MySQLExceptionInterpreter {
13
	/**
14
	 * @param PDOException $exception
15
	 * @throw PDOException
16
	 */
17
	public function throwMoreConcreteException(PDOException $exception) {
18
		$errorInfo = $exception->errorInfo;
19
		$code = is_array($errorInfo) && isset($errorInfo[1]) ? ((int) $errorInfo[1]) : ((int) $exception->getCode());
20
		$message = (string) $exception->getMessage();
21
		switch($code) {
22
			case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception);
23
			case 1213: throw new SqlDeadLockException($message, $code, $exception);
24
			case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception);
25
			case 1022:
26
			case 1062:
27
			case 1169:
28
			case 1586: throw new DuplicateUniqueKeyException($message, $code, $exception);
29
			case 1216:
30
			case 1217:
31
			case 1452: throw new IntegrityConstraintViolationException($message, (int) $code, $exception);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
32
		}
33
		/** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */
34
		if(!is_string($message) || !is_int($code)) {
35
			throw new SqlException((string) $message, (int) $code, $exception);
36
		}
37
		throw $exception;
38
	}
39
}
40