Completed
Pull Request — develop (#3570)
by Jonathan
155:39 queued 152:58
created

SQLSrvException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 30
ccs 13
cts 19
cp 0.6842
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromSqlSrvErrors() 0 25 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\SQLSrv;
6
7
use Doctrine\DBAL\Driver\AbstractDriverException;
8
use const SQLSRV_ERR_ERRORS;
9
use function rtrim;
10
use function sqlsrv_errors;
11
12
class SQLSrvException extends AbstractDriverException
13
{
14
    /**
15
     * Helper method to turn sql server errors into exception.
16
     */
17 86
    public static function fromSqlSrvErrors() : self
18
    {
19 86
        $message  = '';
20 86
        $sqlState = null;
21 86
        $code     = null;
22
23 86
        foreach ((array) sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error) {
24 86
            $message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
25
26 86
            if ($sqlState === null) {
27 86
                $sqlState = $error['SQLSTATE'];
28
            }
29
30 86
            if ($code !== null) {
31 85
                continue;
32
            }
33
34 86
            $code = $error['code'];
35
        }
36
37 86
        if (! $message) {
38
            $message = 'SQL Server error occurred but no error message was retrieved from driver.';
39
        }
40
41 86
        return new self(rtrim($message), $sqlState, $code);
42
    }
43
}
44