Passed
Push — master ( 32c233...63da76 )
by Ron
02:11
created

throwMoreConcreteException()   B

Complexity

Conditions 13
Paths 44

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 7.3977
c 0
b 0
f 0
cc 13
eloc 15
nc 44
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
		/** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */
20
		$code = is_array($errorInfo) && isset($errorInfo[1]) ? ((int) $errorInfo[1]) : ((int) $exception->getCode());
21
		$message = (string) $exception->getMessage();
22
		switch($code) {
23
			case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception);
24
			case 1213: throw new SqlDeadLockException($message, $code, $exception);
25
			case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception);
26
			case 1022:
27
			case 1062:
28
			case 1169:
29
			case 1586: throw new DuplicateUniqueKeyException($message, $code, $exception);
30
			case 1216:
31
			case 1217:
32
			case 1452: throw new IntegrityConstraintViolationException($message, $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...
33
		}
34
		throw new SqlException($message, $code, $exception);
35
	}
36
}
37