Passed
Pull Request — master (#7)
by Damien
03:55
created

PlatformHelper::isIndexLengthLimited()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 7.6666
cc 10
nc 5
nop 2

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
namespace DH\Auditor\Provider\Doctrine\Persistence\Helper;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
7
8
abstract class PlatformHelper
9
{
10
    /**
11
     * MySQL < 5.7.7 and MariaDb < 10.2.2 index length requirements.
12
     *
13
     * @see https://github.com/doctrine/dbal/issues/3419
14
     */
15
    public static function isIndexLengthLimited(string $name, Connection $connection): bool
16
    {
17
        $columns = SchemaHelper::getAuditTableColumns();
18
        if (
19
            !isset($columns[$name])
20
            || $columns[$name]['type'] !== DoctrineHelper::getDoctrineType('STRING')
21
            || (
22
                isset($columns[$name]['options'], $columns[$name]['options']['length'])
23
                && $columns[$name]['options']['length'] < 191
24
            )
25
        ) {
26
            return false;
27
        }
28
29
        $version = self::getServerVersion($connection);
30
31
        if (null === $version) {
32
            return false;
33
        }
34
35
        $mariadb = false !== mb_stripos($version, 'mariadb');
36
        if ($mariadb && version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.2', '<')) {
37
            return true;
38
        }
39
40
        if (!$mariadb && version_compare(self::getOracleMysqlVersionNumber($version), '5.7.7', '<')) {
41
            return true;
42
        }
43
44
        return false;
45
    }
46
47
    public static function getServerVersion(Connection $connection): ?string
48
    {
49
        $wrappedConnection = $connection->getWrappedConnection();
50
51
        if ($wrappedConnection instanceof ServerInfoAwareConnection) {
52
            return $wrappedConnection->getServerVersion();
53
        }
54
55
        return null;
56
    }
57
58
    public static function isJsonSupported(Connection $connection): bool
59
    {
60
        $version = self::getServerVersion($connection);
61
        if (null === $version) {
62
            return true;
63
        }
64
65
        $mariadb = false !== mb_stripos($version, 'mariadb');
66
        if ($mariadb && version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.7', '<')) {
67
            // JSON wasn't supported on MariaDB before 10.2.7
68
            // @see https://mariadb.com/kb/en/json-data-type/
69
            return false;
70
        }
71
72
        // Assume JSON is supported
73
        return true;
74
    }
75
76
    /**
77
     * Get a normalized 'version number' from the server string
78
     * returned by Oracle MySQL servers.
79
     *
80
     * @param string $versionString Version string returned by the driver, i.e. '5.7.10'
81
     *
82
     * @copyright Doctrine team
83
     */
84
    public static function getOracleMysqlVersionNumber(string $versionString): string
85
    {
86
        preg_match(
87
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
88
            $versionString,
89
            $versionParts
90
        );
91
92
        $majorVersion = $versionParts['major'];
93
        $minorVersion = $versionParts['minor'] ?? 0;
94
        $patchVersion = $versionParts['patch'] ?? null;
95
96
        if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) {
97
            $patchVersion = '9';
98
        }
99
100
        return $majorVersion.'.'.$minorVersion.'.'.$patchVersion;
101
    }
102
103
    /**
104
     * Detect MariaDB server version, including hack for some mariadb distributions
105
     * that starts with the prefix '5.5.5-'.
106
     *
107
     * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
108
     *
109
     * @copyright Doctrine team
110
     */
111
    public static function getMariaDbMysqlVersionNumber(string $versionString): string
112
    {
113
        preg_match(
114
            '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
115
            $versionString,
116
            $versionParts
117
        );
118
119
        return $versionParts['major'].'.'.$versionParts['minor'].'.'.$versionParts['patch'];
120
    }
121
}
122