Completed
Push — master ( 5dd66e...5b5c2c )
by Marco
114:19 queued 111:15
created

DBAL/Driver/SQLAnywhere/SQLAnywhereException.php (8 issues)

1
<?php
2
3
namespace Doctrine\DBAL\Driver\SQLAnywhere;
4
5
use Doctrine\DBAL\Driver\AbstractDriverException;
6
use InvalidArgumentException;
7
use function sasql_error;
8
use function sasql_errorcode;
9
use function sasql_sqlstate;
10
use function sasql_stmt_errno;
11
use function sasql_stmt_error;
12
13
/**
14
 * SAP Sybase SQL Anywhere driver exception.
15
 */
16
class SQLAnywhereException extends AbstractDriverException
17
{
18
    /**
19
     * Helper method to turn SQL Anywhere error into exception.
20
     *
21
     * @param resource|null $conn The SQL Anywhere connection resource to retrieve the last error from.
22
     * @param resource|null $stmt The SQL Anywhere statement resource to retrieve the last error from.
23
     *
24
     * @return SQLAnywhereException
25
     *
26
     * @throws InvalidArgumentException
27
     */
28
    public static function fromSQLAnywhereError($conn = null, $stmt = null)
29
    {
30
        $state   = $conn ? sasql_sqlstate($conn) : sasql_sqlstate();
0 ignored issues
show
The function sasql_sqlstate was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $state   = $conn ? /** @scrutinizer ignore-call */ sasql_sqlstate($conn) : sasql_sqlstate();
Loading history...
$conn is of type null|resource, thus it always evaluated to false.
Loading history...
31
        $code    = null;
32
        $message = null;
33
34
        /**
35
         * Try retrieving the last error from statement resource if given
36
         */
37
        if ($stmt) {
0 ignored issues
show
$stmt is of type null|resource, thus it always evaluated to false.
Loading history...
38
            $code    = sasql_stmt_errno($stmt);
0 ignored issues
show
The function sasql_stmt_errno was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            $code    = /** @scrutinizer ignore-call */ sasql_stmt_errno($stmt);
Loading history...
39
            $message = sasql_stmt_error($stmt);
0 ignored issues
show
The function sasql_stmt_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            $message = /** @scrutinizer ignore-call */ sasql_stmt_error($stmt);
Loading history...
40
        }
41
42
        /**
43
         * Try retrieving the last error from the connection resource
44
         * if either the statement resource is not given or the statement
45
         * resource is given but the last error could not be retrieved from it (fallback).
46
         * Depending on the type of error, it is sometimes necessary to retrieve
47
         * it from the connection resource even though it occurred during
48
         * a prepared statement.
49
         */
50
        if ($conn && ! $code) {
0 ignored issues
show
$conn is of type null|resource, thus it always evaluated to false.
Loading history...
51
            $code    = sasql_errorcode($conn);
0 ignored issues
show
The function sasql_errorcode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            $code    = /** @scrutinizer ignore-call */ sasql_errorcode($conn);
Loading history...
52
            $message = sasql_error($conn);
0 ignored issues
show
The function sasql_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            $message = /** @scrutinizer ignore-call */ sasql_error($conn);
Loading history...
53
        }
54
55
        /**
56
         * Fallback mode if either no connection resource is given
57
         * or the last error could not be retrieved from the given
58
         * connection / statement resource.
59
         */
60
        if (! $conn || ! $code) {
61
            $code    = sasql_errorcode();
62
            $message = sasql_error();
63
        }
64
65
        if ($message) {
66
            return new self('SQLSTATE [' . $state . '] [' . $code . '] ' . $message, $state, $code);
67
        }
68
69
        return new self('SQL Anywhere error occurred but no error message was retrieved from driver.', $state, $code);
70
    }
71
}
72