Completed
Push — develop ( dcb0ff...425513 )
by Sergei
23s queued 13s
created

AbstractSQLServerDriver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 46
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createDatabasePlatformForVersion() 0 24 3
A getDatabasePlatform() 0 3 1
A getSchemaManager() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\Connection. 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...
8
use Doctrine\DBAL\Driver;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
11
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
12
use Doctrine\DBAL\Platforms\SQLServerPlatform;
13
use Doctrine\DBAL\Schema\AbstractSchemaManager;
14
use Doctrine\DBAL\Schema\SQLServerSchemaManager;
15
use Doctrine\DBAL\VersionAwarePlatformDriver;
16
use function preg_match;
17
use function version_compare;
18
19
/**
20
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for Microsoft SQL Server based drivers.
21
 */
22
abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDriver
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 274
    public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
28
    {
29 274
        if (! preg_match(
30 2
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
31 274
            $version,
32 274
            $versionParts
33
        )) {
34 248
            throw InvalidPlatformVersion::new(
35 248
                $version,
36 248
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
37
            );
38
        }
39
40 273
        $majorVersion = $versionParts['major'];
41 273
        $minorVersion = $versionParts['minor'] ?? 0;
42 273
        $patchVersion = $versionParts['patch'] ?? 0;
43 273
        $buildVersion = $versionParts['build'] ?? 0;
44 273
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
45
46
        switch (true) {
47 273
            case version_compare($version, '11.00.2100', '>='):
48 273
                return new SQLServer2012Platform();
49
            default:
50 273
                return new SQLServerPlatform();
51
        }
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 223
    public function getDatabasePlatform() : AbstractPlatform
58
    {
59 223
        return new SQLServerPlatform();
60
    }
61 223
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getSchemaManager(Connection $conn) : AbstractSchemaManager
66
    {
67 198
        return new SQLServerSchemaManager($conn);
68
    }
69
}
70