Passed
Push — 2.10.x ( 03f139...aab745 )
by Sergei
25:22
created

SQLSrvException::fromSqlSrvErrors()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.7873

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 25
ccs 13
cts 19
cp 0.6842
rs 9.5222
c 0
b 0
f 0
cc 5
nc 10
nop 0
crap 5.7873
1
<?php
2
3
namespace Doctrine\DBAL\Driver\SQLSrv;
4
5
use Doctrine\DBAL\Driver\AbstractDriverException;
6
use const SQLSRV_ERR_ERRORS;
7
use function rtrim;
8
use function sqlsrv_errors;
9
10
/**
11
 * @psalm-immutable
12
 */
13
class SQLSrvException extends AbstractDriverException
14
{
15
    /**
16
     * Helper method to turn sql server errors into exception.
17
     *
18
     * @return \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException
19
     */
20 89
    public static function fromSqlSrvErrors()
21
    {
22 89
        $message   = '';
23 89
        $sqlState  = null;
24 89
        $errorCode = null;
25
26 89
        foreach ((array) sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error) {
27 89
            $message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
28
29 89
            if ($sqlState === null) {
30 89
                $sqlState = $error['SQLSTATE'];
31
            }
32
33 89
            if ($errorCode !== null) {
34 88
                continue;
35
            }
36
37 89
            $errorCode = $error['code'];
38
        }
39
40 89
        if (! $message) {
41
            $message = 'SQL Server error occurred but no error message was retrieved from driver.';
42
        }
43
44 89
        return new self(rtrim($message), $sqlState, $errorCode);
45
    }
46
}
47