Passed
Pull Request — main (#122)
by Andreas
02:00
created

ExceptionConverter::convert()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 27.1508

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 31
ccs 8
cts 18
cp 0.4444
rs 7.6666
cc 10
nc 9
nop 2
crap 27.1508

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
3
declare(strict_types=1);
4
5
namespace Crate\DBAL\Driver\PDOCrate;
6
7
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
8
use Doctrine\DBAL\Driver\Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Crate\DBAL\Driver\PDOCrate\Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Doctrine\DBAL\Exception\ConnectionException;
10
use Doctrine\DBAL\Exception\DriverException;
11
use Doctrine\DBAL\Exception\InvalidFieldNameException;
12
use Doctrine\DBAL\Exception\SchemaDoesNotExist;
13
use Doctrine\DBAL\Exception\SyntaxErrorException;
14
use Doctrine\DBAL\Exception\TableExistsException;
15
use Doctrine\DBAL\Exception\TableNotFoundException;
16
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
17
use Doctrine\DBAL\Query;
18
19
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist;
20
use function strpos;
21
22
/** @internal */
23
final class ExceptionConverter implements ExceptionConverterInterface
24
{
25
    /** @link https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-codes */
26 10
    public function convert(Exception $exception, ?Query $query): DriverException
27
    {
28 10
        switch ($exception->getCode()) {
29 10
            case '4000':
30 2
                return new SyntaxErrorException($exception, $query);
31
32 8
            case '4008':
33 8
            case '4043':
34
                return new InvalidFieldNameException($exception, $query);
35
36 8
            case '4041':
37 8
                return new TableNotFoundException($exception, $query);
38
39
            case '4045':
40
                return new SchemaDoesNotExist($exception, $query);
41
42
            case '4091':
43
                return new UniqueConstraintViolationException($exception, $query);
44
45
            case '4093':
46
                return new TableExistsException($exception, $query);
47
        }
48
49
        // Prior to fixing https://bugs.php.net/bug.php?id=64705 (PHP 7.4.10),
50
        // in some cases (mainly connection errors) the PDO exception wouldn't provide a SQLSTATE via its code.
51
        // We have to match against the SQLSTATE in the error message in these cases.
52
        if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) {
53
            return new ConnectionException($exception, $query);
54
        }
55
56
        return new DriverException($exception, $query);
57
    }
58
}
59