HandlesIndexNaming::getColumnNames()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Schema\Concerns;
6
7
trait HandlesIndexNaming
8
{
9
    /**
10
     * Create a default index name for the table.
11
     *
12
     * @param  string  $type
13
     */
14 77
    public function createIndexName($type, array $columns, array $options = [], string $table = null): string
15
    {
16 77
        $nameParts = [];
17 77
        $nameParts[] = $this->prefix . ($table ?? $this->table);
18 77
        $nameParts = array_merge($nameParts, $this->getColumnNames($columns));
19 77
        $nameParts[] = $type;
20 77
        $nameParts = array_merge($nameParts, array_keys($options));
21 77
        array_filter($nameParts);
22
23 77
        $index = strtolower(implode('_', $nameParts));
24 77
        $index = preg_replace("/\[\*+\]+/", '_array', $index);
25
26 77
        return preg_replace('/[^A-Za-z0-9]+/', '_', $index);
27
    }
28
29 77
    protected function getColumnNames(array $columns): array
30
    {
31 77
        $names = [];
32 77
        foreach ($columns as $column) {
33 77
            if (is_array($column) && $column['name'] !== '') {
34 1
                $names[] = $column['name'];
35
36 1
                continue;
37
            }
38 77
            $names[] = $column;
39
        }
40
41 77
        return $names;
42
    }
43
}
44