Passed
Push — 3.x ( 288983...69f0d1 )
by Aleksei
10:54
created

Column::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 9
dl 0
loc 26
ccs 10
cts 10
cp 1
crap 2
rs 10
c 1
b 0
f 0

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
declare(strict_types=1);
4
5
namespace Cycle\Annotated\Annotation;
6
7
use Cycle\ORM\Parser\Typecast;
8
use Doctrine\Common\Annotations\Annotation\Target;
9
use JetBrains\PhpStorm\ExpectedValues;
10
use Spiral\Attributes\NamedArgumentConstructor;
11
12
/**
13
 * @Annotation
14
 * @NamedArgumentConstructor
15
 * @Target({"PROPERTY", "ANNOTATION", "CLASS"})
16
 */
17
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
18
#[NamedArgumentConstructor]
19
final class Column
20
{
21
    private bool $hasDefault = false;
22
    /**
23
     * @var array<non-empty-string, mixed> Other database specific attributes.
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
24
     */
25
    private array $attributes;
26
27
    /**
28
     * @param non-empty-string $type Column type. {@see \Cycle\Database\Schema\AbstractColumn::$mapping}
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
29
     *        Column types `smallPrimary`, `timetz`, `timestamptz`, `interval`, `bitVarying`, `int4range`, `int8range`,
30
     *        `numrange`, `tsrange`, `tstzrange`, `daterange`, `jsonb`, `point`, `line`, `lseg`, `box`, `path`,
31
     *        `polygon`, `circle`, `cidr`, `inet`, `macaddr`, `macaddr8`, `tsvector`, `tsquery` are related
32
     *         to the PostgreSQL only {@see \Cycle\Database\Driver\Postgres\Schema\PostgresColumn::$mapping}
33
     *        Column type `datetime2` is related to the SQL Server only
34
     *        {@see \Cycle\Database\Driver\SQLServer\Schema\SQLServerColumn::$mapping}
35
     * @param non-empty-string|null $name Column name. Defaults to the property name.
36
     * @param non-empty-string|null $property Property that belongs to column. For virtual columns.
37
     * @param bool $primary Explicitly set column as a primary key.
38
     * @param bool $nullable Set column as nullable.
39 1032
     * @param mixed|null $default Default column value.
40
     * @param callable|non-empty-string|null $typecast Typecast rule name.
41
     *        Regarding the default Typecast handler {@see Typecast} the value can be `callable` or
42
     *        one of ("int"|"float"|"bool"|"datetime") based on column type.
43
     *        If you want to use another rule you should add in the `typecast` argument of the {@see Entity} attribute
44
     *        a relevant Typecast handler that supports the rule.
45
     * @param bool $castDefault
46
     * @param mixed ...$attributes Other database specific attributes. Use named notation to define them.
47
     *        For example: #[Column('smallInt', unsigned: true, zerofill: true)]
48
     */
49
    public function __construct(
50
        #[ExpectedValues(values: ['primary', 'bigPrimary', 'enum', 'boolean',
51
            'integer', 'tinyInteger', 'smallInteger', 'bigInteger', 'string', 'text', 'tinyText', 'longText', 'double',
52
            'float', 'decimal', 'datetime', 'date', 'time', 'timestamp', 'binary', 'tinyBinary', 'longBinary', 'json',
53 1032
            'uuid', 'bit',
54 496
            // PostgreSQL
55
            'smallPrimary', 'timetz', 'timestamptz', 'interval', 'bitVarying', 'int4range', 'int8range', 'numrange',
56 1032
            'tsrange', 'tstzrange', 'daterange', 'jsonb', 'point', 'line', 'lseg', 'box', 'path', 'polygon', 'circle',
57
            'cidr', 'inet', 'macaddr', 'macaddr8', 'tsvector', 'tsquery',
58 1032
            // SQL Server
59
            'datetime2',
60 1032
        ])]
61
        private string $type,
62
        private ?string $name = null,
63 1032
        private ?string $property = null,
64
        private bool $primary = false,
65 1032
        private bool $nullable = false,
66
        private mixed $default = null,
67
        private mixed $typecast = null,
68 472
        private bool $castDefault = false,
69
        mixed ...$attributes,
70 472
    ) {
71
        if ($default !== null) {
72
            $this->hasDefault = true;
73 1032
        }
74
        $this->attributes = $attributes;
75 1032
    }
76
77
    public function getType(): ?string
78 1032
    {
79
        return $this->type;
80 1032
    }
81
82
    public function getColumn(): ?string
83 1032
    {
84
        return $this->name;
85 1032
    }
86
87
    public function getProperty(): ?string
88 496
    {
89
        return $this->property;
90 496
    }
91
92
    public function isNullable(): bool
93 1032
    {
94
        return $this->nullable;
95 1032
    }
96
97
    public function isPrimary(): bool
98 1032
    {
99
        return $this->primary;
100 1032
    }
101
102
    public function hasDefault(): bool
103
    {
104
        return $this->hasDefault;
105
    }
106
107
    public function getDefault(): mixed
108
    {
109
        return $this->default;
110
    }
111
112
    public function castDefault(): bool
113
    {
114
        return $this->castDefault;
115
    }
116
117
    public function getTypecast(): mixed
118
    {
119
        return $this->typecast;
120
    }
121
122
    /**
123
     * @return array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
124
     */
125
    public function getAttributes(): array
126
    {
127
        return $this->attributes;
128
    }
129
}
130