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

Doctrine/DBAL/Driver/AbstractSQLServerDriver.php (2 issues)

1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
0 ignored issues
show
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...
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Driver;
8
use Doctrine\DBAL\Platforms\SQLServer2005Platform;
9
use Doctrine\DBAL\Platforms\SQLServer2008Platform;
10
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
11
use Doctrine\DBAL\Platforms\SQLServerPlatform;
12
use Doctrine\DBAL\Schema\SQLServerSchemaManager;
13
use Doctrine\DBAL\VersionAwarePlatformDriver;
14
use function preg_match;
15
use function version_compare;
16
17
/**
18
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for Microsoft SQL Server based drivers.
19
 */
20
abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDriver
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 531
    public function createDatabasePlatformForVersion($version)
26
    {
27 531
        if (! preg_match(
28 12
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
29 531
            $version,
30 531
            $versionParts
31
        )) {
32 500
            throw DBALException::invalidPlatformVersionSpecified(
33 500
                $version,
34 500
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
35
            );
36
        }
37
38 525
        $majorVersion = $versionParts['major'];
39 525
        $minorVersion = $versionParts['minor'] ?? 0;
40 525
        $patchVersion = $versionParts['patch'] ?? 0;
41 525
        $buildVersion = $versionParts['build'] ?? 0;
42 525
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
43
44
        switch (true) {
45 525
            case version_compare($version, '11.00.2100', '>='):
46 525
                return new SQLServer2012Platform();
47 525
            case version_compare($version, '10.00.1600', '>='):
48 525
                return new SQLServer2008Platform();
49 525
            case version_compare($version, '9.00.1399', '>='):
50 525
                return new SQLServer2005Platform();
51
            default:
52 525
                return new SQLServerPlatform();
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 475
    public function getDatabase(Connection $conn)
60
    {
61 475
        $params = $conn->getParams();
62
63 475
        return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $params['dbname']...NAME()')->fetchColumn() also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver::getDatabase() of string.
Loading history...
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 450
    public function getDatabasePlatform()
70
    {
71 450
        return new SQLServer2008Platform();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 425
    public function getSchemaManager(Connection $conn)
78
    {
79 425
        return new SQLServerSchemaManager($conn);
80
    }
81
}
82