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

Column::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 11
dl 0
loc 26
rs 9.8333

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Column as DBALColumn;
23
24
/**
25
 * Fluent/immutable constructors for doctrine/dbal.
26
 */
27
final class Column extends DBALColumn
28
{
29
    public function __construct(
30
        private readonly string $name,
31
        private readonly ColumnType $type,
32
        private readonly int $length = 0,
33
        private readonly int $precision = 0,
34
        private readonly int $scale = 0,
35
        private readonly bool $unsigned = false,
36
        private readonly bool $fixed = false,
37
        private readonly bool $nullable = false,
38
        private readonly float|int|string|null $default = null,
39
        private readonly bool $autoincrement = false,
40
        private readonly string|null $collation = null,
41
    ) {
42
        parent::__construct(
43
            name: $name,
44
            type: ColumnType::toDBALType(column_type: $type),
45
            options: [
46
                'length'          => $length,
47
                'precision'       => $precision,
48
                'scale'           => $scale,
49
                'unsigned'        => $unsigned,
50
                'fixed'           => $fixed,
51
                'notnull'         => !$nullable,
52
                'default'         => $default,
53
                'autoincrement'   => $autoincrement,
54
                'platformOptions' => ['collation' => $collation],
55
            ],
56
        );
57
    }
58
59
    public function autoincrement(): self
60
    {
61
        return new self(
62
            name: $this->name,
63
            type: $this->type,
64
            length: $this->length,
65
            precision: $this->precision,
66
            scale: $this->scale,
67
            unsigned: $this->unsigned,
68
            fixed: $this->fixed,
69
            nullable: $this->nullable,
70
            default: $this->default,
71
            autoincrement: true,
72
            collation: $this->collation,
73
        );
74
    }
75
76
    public function default(float|int|string $default): self
77
    {
78
        return new self(
79
            name: $this->name,
80
            type: $this->type,
81
            length: $this->length,
82
            precision: $this->precision,
83
            scale: $this->scale,
84
            unsigned: $this->unsigned,
85
            fixed: $this->fixed,
86
            nullable: $this->nullable,
87
            default: $default,
88
            autoincrement: $this->autoincrement,
89
            collation: $this->collation,
90
        );
91
    }
92
93
    public function fixed(): self
94
    {
95
        return new self(
96
            name: $this->name,
97
            type: $this->type,
98
            length: $this->length,
99
            precision: $this->precision,
100
            scale: $this->scale,
101
            unsigned: $this->unsigned,
102
            fixed: true,
103
            nullable: $this->nullable,
104
            default: $this->default,
105
            autoincrement: $this->autoincrement,
106
            collation: $this->collation,
107
        );
108
    }
109
110
    public function nullable(): self
111
    {
112
        return new self(
113
            name: $this->name,
114
            type: $this->type,
115
            length: $this->length,
116
            precision: $this->precision,
117
            scale: $this->scale,
118
            unsigned: $this->unsigned,
119
            fixed: $this->fixed,
120
            nullable: true,
121
            default: $this->default,
122
            autoincrement: $this->autoincrement,
123
            collation: $this->collation,
124
        );
125
    }
126
}
127