Passed
Push — dbal ( 92f26f...aec129 )
by Greg
06:59
created

Table::namedUniqueIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2024 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\DB;
21
22
use Doctrine\DBAL\Schema\Table as DBALTable;
23
use Fisharebest\Webtrees\DB;
24
25
use function array_filter;
26
use function array_values;
27
28
/**
29
 * Fluent/immutable constructors for doctrine/dbal.
30
 */
31
final class Table extends DBALTable
32
{
33
    /** @var array<int,Column> */
34
    private array $columns;
35
36
    /** @var array<int,Index> */
37
    private array $indexes;
38
39
    /** @var array<int,PrimaryKey> */
40
    private array $primary_keys;
41
42
    /** @var array<int,UniqueIndex> */
43
    private array $unique_indexes;
44
45
    /** @var array<int,ForeignKey> */
46
    private array $foreign_keys;
47
48
    public function __construct(private readonly string $name, Column|Index|UniqueIndex|ForeignKey|PrimaryKey ...$components)
49
    {
50
        $column_filter       = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof Column;
51
        $foreign_key_filter  = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof ForeignKey;
52
        $index_filter        = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof Index;
53
        $primary_key_filter  = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof PrimaryKey;
54
        $unique_index_filter = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof UniqueIndex;
55
56
        $this->columns        = array_values(array: array_filter(array: $components, callback: $column_filter));
57
        $this->foreign_keys   = array_values(array: array_filter(array: $components, callback: $foreign_key_filter));
58
        $this->indexes        = array_values(array: array_filter(array: $components, callback: $index_filter));
59
        $this->primary_keys   = array_values(array: array_filter(array: $components, callback: $primary_key_filter));
60
        $this->unique_indexes = array_values(array: array_filter(array: $components, callback: $unique_index_filter));
61
62
        array_walk(array: $this->indexes, callback: self::namedIndex(...));
63
        array_walk(array: $this->unique_indexes, callback: self::namedUniqueIndex(...));
64
        array_walk(array: $this->foreign_keys, callback: self::namedForeignKey(...));
65
66
        parent::__construct(
67
            name: DB::prefix($name),
68
            columns: $this->columns,
69
            indexes: [...$this->primary_keys, ...$this->unique_indexes, ...$this->indexes],
70
            uniqueConstraints: [],
71
            fkConstraints: $this->foreign_keys,
72
            options: [],
73
        );
74
    }
75
76
    private function namedIndex(Index &$index, int|string $n): void {
0 ignored issues
show
Unused Code introduced by
The method namedIndex() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
77
        $n     = 1 + (int) $n;
78
        $name  = DB::prefix($this->name . '_ix' . $n);
79
        $index = $index->name($name);
80
    }
81
82
    private function namedUniqueIndex(UniqueIndex &$unique_index, int|string $n): void {
0 ignored issues
show
Unused Code introduced by
The method namedUniqueIndex() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
83
        $n            = 1 + (int) $n;
84
        $name         = DB::prefix($this->name . '_ux' . $n);
85
        $unique_index = $unique_index->name($name);
86
    }
87
88
    private function namedForeignKey(ForeignKey &$foreign_key, int|string $n): void {
0 ignored issues
show
Unused Code introduced by
The method namedForeignKey() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
89
        $n           = 1 + (int) $n;
90
        $name        = DB::prefix($this->name . '_fk' . $n);
91
        $foreign_key = $foreign_key->name($name);
92
    }
93
94
    public function dropColumn(string $name): self
95
    {
96
        $columns = array_filter(array: $this->columns, callback: static fn (Column $column): bool => $column->getName() !== $name);
97
98
        return new self($this->name, ...$columns, ...$this->indexes, ...$this->primary_keys, ...$this->unique_keys, ...$this->foreign_keys);
0 ignored issues
show
Bug introduced by
The property unique_keys does not exist on Fisharebest\Webtrees\DB\Table. Did you mean unique_indexes?
Loading history...
99
    }
100
}
101