Passed
Pull Request — next (#153)
by Bas
04:20 queued 58s
created

HandlesIndexNaming   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 35
ccs 18
cts 18
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnNames() 0 13 4
A createIndexName() 0 13 1
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 66
    public function createIndexName($type, array $columns, array $options = [], string $table = null): string
15
    {
16 66
        $nameParts = [];
17 66
        $nameParts[] = $this->prefix . ($table ?? $this->table);
18 66
        $nameParts = array_merge($nameParts, $this->getColumnNames($columns));
19 66
        $nameParts[] = $type;
20 66
        $nameParts = array_merge($nameParts, array_keys($options));
21 66
        array_filter($nameParts);
22
23 66
        $index = strtolower(implode('_', $nameParts));
24 66
        $index = preg_replace("/\[\*+\]+/", '_array', $index);
25
26 66
        return preg_replace('/[^A-Za-z0-9]+/', '_', $index);
27
    }
28
29 66
    protected function getColumnNames(array $columns): array
30
    {
31 66
        $names = [];
32 66
        foreach ($columns as $column) {
33 66
            if (is_array($column) && $column['name'] !== '') {
34 1
                $names[] = $column['name'];
35
36 1
                continue;
37
            }
38 66
            $names[] = $column;
39
        }
40
41 66
        return $names;
42
    }
43
}
44