Test Failed
Branch master (1fbe3c)
by Aleksandr
02:14
created

SchemaHelper::truncateIndexName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
c 0
b 0
f 0
rs 10
cc 3
nc 3
nop 3
1
<?php
2
3
namespace carono\yii2migrate\helpers;
4
5
use yii\db\Connection;
6
use yii\helpers\ArrayHelper;
7
use yii\helpers\StringHelper;
8
9
class SchemaHelper
10
{
11
    /**
12
     * @param Connection $db
13
     * @param $table
14
     * @return \yii\db\IndexConstraint[]
15
     */
16
    public static function findNonUniqueIndexes($db, $table)
17
    {
18
        $indexes = self::findIndexes($db, $table);
19
        $indexes = array_filter($indexes, function ($index) {
20
            return !$index->isUnique;
21
        });
22
        return ArrayHelper::index($indexes, 'name');
23
    }
24
25
    /**
26
     * @param Connection $db
27
     * @param $table
28
     * @return \yii\db\IndexConstraint[]
29
     */
30
    public static function findUniqueIndexes($db, $table)
31
    {
32
        $indexes = self::findIndexes($db, $table);
33
        $indexes = array_filter($indexes, function ($index) {
34
            return $index->isUnique;
35
        });
36
        return ArrayHelper::index($indexes, 'name');
37
    }
38
39
    /**
40
     * @param Connection $db
41
     * @param $table
42
     * @return \yii\db\IndexConstraint[]
43
     */
44
    public static function findIndexes($db, $table)
45
    {
46
        $reflectionMethod = new \ReflectionMethod(get_class($db->schema), 'loadTableIndexes');
47
        $reflectionMethod->setAccessible(true);
48
        $indexes = $reflectionMethod->invoke($db->schema, self::expandTablePrefix($table, $db->tablePrefix));
49
        return ArrayHelper::index($indexes, 'name');
50
    }
51
52
53
    /**
54
     * @param Connection $db
55
     * @param $table
56
     * @return \yii\db\ForeignKeyConstraint[]
57
     */
58
    public static function findTableForeignKeys($db, $table)
59
    {
60
        $reflectionMethod = new \ReflectionMethod(get_class($db->schema), 'loadTableForeignKeys');
61
        $reflectionMethod->setAccessible(true);
62
        return ArrayHelper::index($reflectionMethod->invoke($db->schema, self::expandTablePrefix($table, $db->tablePrefix)), 'name');
63
    }
64
65
    /**
66
     * @param $str
67
     * @return mixed
68
     */
69
    public static function removeSchema($str)
70
    {
71
        if (strpos($str, '.') !== false) {
72
            $arr = explode('.', $str);
73
            return $arr[1];
74
        }
75
76
        return $str;
77
    }
78
79
    /**
80
     * {{%table}} to pfx_table
81
     *
82
     * @param $name
83
     * @param $prefix
84
     * @return null|string|string[]
85
     */
86
    public static function expandTablePrefix($name, $prefix)
87
    {
88
        return preg_replace('/{{%([\w\-_]+)}}/', addcslashes($prefix, "\\") . '$1', $name);
89
    }
90
91
    /**
92
     * pfx_table to {{%table}}
93
     *
94
     * @param $name
95
     * @param $prefix
96
     * @return null|string|string[]
97
     */
98
    public static function collapseTablePrefix($name, $prefix)
99
    {
100
        $prefix = addcslashes($prefix, "\\/");
101
        return preg_replace("/^$prefix(.+)$/m", '{{%$1}}', $name);
102
    }
103
104
    /**
105
     * Принудительно обрезаем названия ключей, если они получаются больше чем $length, т.к. базы могут вылететь с ошибкой
106
     *
107
     * @see https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
108
     *
109
     * @param $name
110
     * @param int $length
111
     * @param null $suffix
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $suffix is correct as it would always require null to be passed?
Loading history...
112
     * @return string
113
     */
114
    public static function truncateIndexName($name, $length = 64, $suffix = null)
115
    {
116
        if (strlen($name) > $length) {
117
            if (StringHelper::endsWith($name, $suffix)) {
118
                $name = substr($name, 0, strlen($suffix) * -1);
119
            }
120
            return dechex(crc32($name)) . $suffix;
121
        }
122
123
        return $name;
124
    }
125
}